1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.xhtml

File XhtmlParser.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
31
9
1
168
98
11
0.35
3.44
9
1.22

Classes

Class Line # Actions
XhtmlParser 48 31 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 322 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.wikimodel.xhtml;
21   
22    import java.io.Reader;
23    import java.util.Collections;
24    import java.util.Map;
25   
26    import javax.xml.parsers.SAXParser;
27    import javax.xml.parsers.SAXParserFactory;
28   
29    import org.xml.sax.InputSource;
30    import org.xml.sax.XMLReader;
31    import org.xml.sax.helpers.DefaultHandler;
32    import org.xwiki.rendering.wikimodel.IWemListener;
33    import org.xwiki.rendering.wikimodel.IWikiParser;
34    import org.xwiki.rendering.wikimodel.WikiParserException;
35    import org.xwiki.rendering.wikimodel.impl.WikiScannerContext;
36    import org.xwiki.rendering.wikimodel.xhtml.filter.AccumulationXMLFilter;
37    import org.xwiki.rendering.wikimodel.xhtml.filter.DTDXMLFilter;
38    import org.xwiki.rendering.wikimodel.xhtml.filter.XHTMLWhitespaceXMLFilter;
39    import org.xwiki.rendering.wikimodel.xhtml.handler.CommentHandler;
40    import org.xwiki.rendering.wikimodel.xhtml.handler.TagHandler;
41    import org.xwiki.rendering.wikimodel.xhtml.impl.XhtmlHandler;
42    import org.xwiki.xml.internal.LocalEntityResolver;
43   
44    /**
45    * @version $Id: 601b6d7ff669c1cef1fdd1f201e6fd4c1cbbb2c7 $
46    * @since 4.0M1
47    */
 
48    public class XhtmlParser implements IWikiParser
49    {
50    private Map<String, TagHandler> fExtraHandlers;
51   
52    private CommentHandler fCommentHandler;
53   
54    /**
55    * Optional XML Reader that can be specified. This is the solution for
56    * setting up custom XML filters.
57    */
58    private XMLReader fXmlReader;
59   
60    private boolean namespacesEnabled = true;
61   
 
62  437 toggle public XhtmlParser()
63    {
64  437 fExtraHandlers = Collections.<String, TagHandler>emptyMap();
65  437 fCommentHandler = new CommentHandler();
66    }
67   
 
68  437 toggle public boolean isNamespacesEnabled()
69    {
70  437 return this.namespacesEnabled;
71    }
72   
 
73  44 toggle public void setNamespacesEnabled(boolean namespacesEnabled)
74    {
75  44 this.namespacesEnabled = namespacesEnabled;
76    }
77   
 
78  338 toggle public void setExtraHandlers(Map<String, TagHandler> extraHandlers)
79    {
80  338 fExtraHandlers = extraHandlers;
81    }
82   
 
83  294 toggle public void setCommentHandler(CommentHandler commentHandler)
84    {
85  294 fCommentHandler = commentHandler;
86    }
87   
 
88  294 toggle public void setXmlReader(XMLReader xmlReader)
89    {
90  294 fXmlReader = xmlReader;
91    }
92   
93    /**
94    * @param listener the listener object wich will be used to report about all
95    * structural elements on the wiki page.
96    * @return a XHTML SAX handler wich can be used to generate well-formed
97    * sequence of WEM events; all events will be reported to the given
98    * listener object.
99    */
 
100  437 toggle public DefaultHandler getHandler(IWemListener listener)
101    {
102  437 WikiScannerContext context = new WikiScannerContext(listener);
103  437 XhtmlHandler handler = new XhtmlHandler(
104    context,
105    fExtraHandlers,
106    fCommentHandler);
107  437 return handler;
108    }
109   
110    /**
111    * @see org.xwiki.rendering.wikimodel.IWikiParser#parse(java.io.Reader,
112    * org.xwiki.rendering.wikimodel.IWemListener)
113    */
 
114  437 toggle public void parse(Reader reader, IWemListener listener)
115    throws WikiParserException
116    {
117  437 try {
118  437 XMLReader xmlReader = getXMLReader();
119   
120    // The WikiModel-specific handler
121  437 DefaultHandler handler = getHandler(listener);
122   
123  437 xmlReader
124    .setFeature("http://xml.org/sax/features/namespaces", isNamespacesEnabled());
125  437 xmlReader.setEntityResolver(new LocalEntityResolver());
126  437 xmlReader.setContentHandler(handler);
127  437 xmlReader.setProperty(
128    "http://xml.org/sax/properties/lexical-handler",
129    handler);
130   
131  437 InputSource source = new InputSource(reader);
132  437 xmlReader.parse(source);
133    } catch (Exception e) {
134  2 throw new WikiParserException(e);
135    }
136    }
137   
 
138  437 toggle private XMLReader getXMLReader() throws Exception
139    {
140  437 XMLReader reader;
141   
142  437 if (fXmlReader != null) {
143  294 reader = fXmlReader;
144    } else {
145  143 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
146  143 SAXParser parser = parserFactory.newSAXParser();
147  143 XMLReader xmlReader = parser.getXMLReader();
148   
149    // Ignore SAX callbacks when the parser parses the DTD
150  143 DTDXMLFilter dtdFilter = new DTDXMLFilter(xmlReader);
151   
152    // Add a XML Filter to accumulate onCharacters() calls since SAX
153    // parser may call it several times.
154  143 AccumulationXMLFilter accumulationFilter = new AccumulationXMLFilter(
155    dtdFilter);
156   
157    // Add a XML Filter to remove non-semantic white spaces. We need to
158    // do that since all WikiModel
159    // events contain only semantic information.
160  143 XHTMLWhitespaceXMLFilter whitespaceFilter = new XHTMLWhitespaceXMLFilter(
161    accumulationFilter);
162   
163  143 reader = whitespaceFilter;
164    }
165   
166  437 return reader;
167    }
168    }