Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
10   95   2   10
0   38   0.2   1
1     2  
1    
 
  XWikiXMLReaderFactory       Line # 51 10 0% 2 1 90.9% 0.90909094
 
  (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.internal.parser.xhtml.wikimodel;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25    import javax.xml.parsers.ParserConfigurationException;
26   
27    import org.xwiki.rendering.wikimodel.xhtml.filter.AccumulationXMLFilter;
28    import org.xwiki.rendering.wikimodel.xhtml.filter.DTDXMLFilter;
29    import org.xml.sax.SAXException;
30    import org.xml.sax.XMLReader;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.xml.EntityResolver;
33    import org.xwiki.xml.XMLReaderFactory;
34   
35    /**
36    * Creates XML Readers that have the following characteristics:
37    * <ul>
38    * <li>Use DTD caching when the underlying XML parser is Xerces</li>
39    * <li>Ignore SAX callbacks when the parser parses the DTD</li>
40    * <li>Accumulate onCharacters() calls since SAX parser may normally call this event several times.</li>
41    * <li>Remove non-semantic white spaces where needed</li>
42    * <li>Resolve DTDs locally to speed DTD loading/validation</li>
43    * </ul>
44    *
45    * @version $Id: 64f8a03e37c2675c5fcadb1a563e826ecf433a21 $
46    * @since 2.1RC1
47    */
48    @Component
49    @Named("xwiki")
50    @Singleton
 
51    public class XWikiXMLReaderFactory implements XMLReaderFactory
52    {
53    /**
54    * Used to create an optimized SAX XML Reader. In general SAX parsers don't cache DTD grammars and as a consequence
55    * parsing a document with a grammar such as the XHTML DTD takes a lot more time than required.
56    */
57    @Inject
58    private XMLReaderFactory xmlReaderFactory;
59   
60    /**
61    * In order to speed up DTD loading/validation we use an entity resolver that can resolve DTDs locally.
62    */
63    @Inject
64    protected EntityResolver entityResolver;
65   
 
66  219 toggle @Override
67    public XMLReader createXMLReader() throws SAXException, ParserConfigurationException
68    {
69  219 XMLReader xmlReader;
70   
71  219 try {
72    // Use a performant XML Reader (which does DTD caching for Xerces)
73  219 XMLReader xr = this.xmlReaderFactory.createXMLReader();
74   
75    // Ignore SAX callbacks when the parser parses the DTD
76  219 DTDXMLFilter dtdFilter = new DTDXMLFilter(xr);
77   
78    // Add a XML Filter to accumulate onCharacters() calls since SAX
79    // parser may call it several times.
80  219 AccumulationXMLFilter accumulationFilter = new AccumulationXMLFilter(dtdFilter);
81   
82    // Add a XML Filter to remove non-semantic white spaces. We need to do that since all WikiModel
83    // events contain only semantic information.
84  219 XWikiXHTMLWhitespaceXMLFilter whitespaceFilter = new XWikiXHTMLWhitespaceXMLFilter(accumulationFilter);
85   
86  219 whitespaceFilter.setEntityResolver(this.entityResolver);
87   
88  219 xmlReader = whitespaceFilter;
89    } catch (Exception e) {
90  0 throw new SAXException("Failed to create XML reader", e);
91    }
92   
93  219 return xmlReader;
94    }
95    }