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

File XWikiXMLReaderFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

0
10
1
1
95
38
2
0.2
10
1
2

Classes

Class Line # Actions
XWikiXMLReaderFactory 51 10 0% 2 1
0.9090909490.9%
 

Contributing tests

This file is covered by 282 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.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.xml.sax.SAXException;
28    import org.xml.sax.XMLReader;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.rendering.wikimodel.xhtml.filter.AccumulationXMLFilter;
31    import org.xwiki.rendering.wikimodel.xhtml.filter.DTDXMLFilter;
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: d0bfd987d0214567555d9ee9cf40b07116b6e23a $
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  294 toggle @Override
67    public XMLReader createXMLReader() throws SAXException, ParserConfigurationException
68    {
69  294 XMLReader xmlReader;
70   
71  294 try {
72    // Use a performant XML Reader (which does DTD caching for Xerces)
73  294 XMLReader xr = this.xmlReaderFactory.createXMLReader();
74   
75    // Ignore SAX callbacks when the parser parses the DTD
76  294 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  294 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  294 XWikiXHTMLWhitespaceXMLFilter whitespaceFilter = new XWikiXHTMLWhitespaceXMLFilter(accumulationFilter);
85   
86  294 whitespaceFilter.setEntityResolver(this.entityResolver);
87   
88  294 xmlReader = whitespaceFilter;
89    } catch (Exception e) {
90  0 throw new SAXException("Failed to create XML reader", e);
91    }
92   
93  294 return xmlReader;
94    }
95    }