Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
29   156   9   4.14
2   89   0.31   7
7     1.29  
1    
 
  XhtmlParser       Line # 48 29 0% 9 1 97.4% 0.9736842
 
  (219)
 
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.LocalEntityResolver;
42    import org.xwiki.rendering.wikimodel.xhtml.impl.XhtmlHandler;
43   
44    /**
45    * @version $Id: 27e143fb6bca628adf4ee2d4360761d85e2dc74f $
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  309 toggle public XhtmlParser()
61    {
62  309 fExtraHandlers = Collections.<String, TagHandler>emptyMap();
63  309 fCommentHandler = new CommentHandler();
64    }
65   
 
66  219 toggle public void setExtraHandlers(Map<String, TagHandler> extraHandlers)
67    {
68  219 fExtraHandlers = extraHandlers;
69    }
70   
 
71  219 toggle public void setCommentHandler(CommentHandler commentHandler)
72    {
73  219 fCommentHandler = commentHandler;
74    }
75   
 
76  219 toggle public void setXmlReader(XMLReader xmlReader)
77    {
78  219 fXmlReader = xmlReader;
79    }
80   
81    /**
82    * @param listener the listener object wich will be used to report about all
83    * structural elements on the wiki page.
84    * @return a XHTML SAX handler wich can be used to generate well-formed
85    * sequence of WEM events; all events will be reported to the given
86    * listener object.
87    */
 
88  309 toggle public DefaultHandler getHandler(IWemListener listener)
89    {
90  309 WikiScannerContext context = new WikiScannerContext(listener);
91  309 XhtmlHandler handler = new XhtmlHandler(
92    context,
93    fExtraHandlers,
94    fCommentHandler);
95  309 return handler;
96    }
97   
98    /**
99    * @see org.xwiki.rendering.wikimodel.IWikiParser#parse(java.io.Reader,
100    * org.xwiki.rendering.wikimodel.IWemListener)
101    */
 
102  309 toggle public void parse(Reader reader, IWemListener listener)
103    throws WikiParserException
104    {
105  309 try {
106  309 XMLReader xmlReader = getXMLReader();
107   
108    // The WikiModel-specific handler
109  309 DefaultHandler handler = getHandler(listener);
110   
111  309 xmlReader
112    .setFeature("http://xml.org/sax/features/namespaces", true);
113  309 xmlReader.setEntityResolver(new LocalEntityResolver());
114  309 xmlReader.setContentHandler(handler);
115  309 xmlReader.setProperty(
116    "http://xml.org/sax/properties/lexical-handler",
117    handler);
118   
119  309 InputSource source = new InputSource(reader);
120  309 xmlReader.parse(source);
121    } catch (Exception e) {
122  0 throw new WikiParserException(e);
123    }
124    }
125   
 
126  309 toggle private XMLReader getXMLReader() throws Exception
127    {
128  309 XMLReader reader;
129   
130  309 if (fXmlReader != null) {
131  219 reader = fXmlReader;
132    } else {
133  90 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
134  90 SAXParser parser = parserFactory.newSAXParser();
135  90 XMLReader xmlReader = parser.getXMLReader();
136   
137    // Ignore SAX callbacks when the parser parses the DTD
138  90 DTDXMLFilter dtdFilter = new DTDXMLFilter(xmlReader);
139   
140    // Add a XML Filter to accumulate onCharacters() calls since SAX
141    // parser may call it several times.
142  90 AccumulationXMLFilter accumulationFilter = new AccumulationXMLFilter(
143    dtdFilter);
144   
145    // Add a XML Filter to remove non-semantic white spaces. We need to
146    // do that since all WikiModel
147    // events contain only semantic information.
148  90 XHTMLWhitespaceXMLFilter whitespaceFilter = new XHTMLWhitespaceXMLFilter(
149    accumulationFilter);
150   
151  90 reader = whitespaceFilter;
152    }
153   
154  309 return reader;
155    }
156    }