1. Project Clover database Sat Feb 2 2019 06:45:20 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 3
0.928571492.9%
 

Contributing tests

This file is covered by 351 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  433 toggle public XhtmlParser()
63    {
64  433 fExtraHandlers = Collections.<String, TagHandler>emptyMap();
65  433 fCommentHandler = new CommentHandler();
66    }
67   
 
68  433 toggle public boolean isNamespacesEnabled()
69    {
70  433 return this.namespacesEnabled;
71    }
72   
 
73  0 toggle public void setNamespacesEnabled(boolean namespacesEnabled)
74    {
75  0 this.namespacesEnabled = namespacesEnabled;
76    }
77   
 
78  334 toggle public void setExtraHandlers(Map<String, TagHandler> extraHandlers)
79    {
80  334 fExtraHandlers = extraHandlers;
81    }
82   
 
83  334 toggle public void setCommentHandler(CommentHandler commentHandler)
84    {
85  334 fCommentHandler = commentHandler;
86    }
87   
 
88  334 toggle public void setXmlReader(XMLReader xmlReader)
89    {
90  334 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  433 toggle public DefaultHandler getHandler(IWemListener listener)
101    {
102  433 WikiScannerContext context = new WikiScannerContext(listener);
103  433 XhtmlHandler handler = new XhtmlHandler(
104    context,
105    fExtraHandlers,
106    fCommentHandler);
107  433 return handler;
108    }
109   
110    /**
111    * @see org.xwiki.rendering.wikimodel.IWikiParser#parse(java.io.Reader,
112    * org.xwiki.rendering.wikimodel.IWemListener)
113    */
 
114  433 toggle public void parse(Reader reader, IWemListener listener)
115    throws WikiParserException
116    {
117  433 try {
118  433 XMLReader xmlReader = getXMLReader();
119   
120    // The WikiModel-specific handler
121  433 DefaultHandler handler = getHandler(listener);
122   
123  433 xmlReader
124    .setFeature("http://xml.org/sax/features/namespaces", isNamespacesEnabled());
125  433 xmlReader.setEntityResolver(new LocalEntityResolver());
126  433 xmlReader.setContentHandler(handler);
127  433 xmlReader.setProperty(
128    "http://xml.org/sax/properties/lexical-handler",
129    handler);
130   
131  433 InputSource source = new InputSource(reader);
132  433 xmlReader.parse(source);
133    } catch (Exception e) {
134  0 throw new WikiParserException(e);
135    }
136    }
137   
 
138  433 toggle private XMLReader getXMLReader() throws Exception
139    {
140  433 XMLReader reader;
141   
142  433 if (fXmlReader != null) {
143  334 reader = fXmlReader;
144    } else {
145  99 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
146  99 SAXParser parser = parserFactory.newSAXParser();
147  99 XMLReader xmlReader = parser.getXMLReader();
148   
149    // Ignore SAX callbacks when the parser parses the DTD
150  99 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  99 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  99 XHTMLWhitespaceXMLFilter whitespaceFilter = new XHTMLWhitespaceXMLFilter(
161    accumulationFilter);
162   
163  99 reader = whitespaceFilter;
164    }
165   
166  433 return reader;
167    }
168    }