1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.xml.internal

File DefaultXMLReaderFactory.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
12
2
1
92
46
4
0.33
6
2
2

Classes

Class Line # Actions
DefaultXMLReaderFactory 46 12 0% 4 3
0.7857142778.6%
 

Contributing tests

This file is covered by 248 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.xml.internal;
21   
22    import java.lang.reflect.Method;
23   
24    import javax.inject.Singleton;
25    import javax.xml.parsers.ParserConfigurationException;
26    import javax.xml.parsers.SAXParser;
27    import javax.xml.parsers.SAXParserFactory;
28   
29    import org.xml.sax.SAXException;
30    import org.xml.sax.XMLReader;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.phase.Initializable;
33    import org.xwiki.component.phase.InitializationException;
34    import org.xwiki.xml.XMLReaderFactory;
35   
36    /**
37    * Verifies if Xerces is on the classpath and if so configures it to cache DTD grammars instead of reparsing it for
38    * every documents, thus greatly improving performances since most XML content handled in XWiki is XHTML using the XHTML
39    * DTD.
40    *
41    * @version $Id: 05f7e6ad97ae5882b95307c4515a83c8e921ef97 $
42    * @since 1.7.1
43    */
44    @Component
45    @Singleton
 
46    public class DefaultXMLReaderFactory implements XMLReaderFactory, Initializable
47    {
48    /**
49    * We cache the Xerces Grammar Pool (when Xerces has been discovered in the classpath) so that we can reuse it
50    * across creation of XML Readers. Note that don't cache the XML Reader itself since it can contain chained XML
51    * Filters which are usually not stateless.
52    */
53    private Object xercesGrammarPool;
54   
 
55  307 toggle @Override
56    public void initialize() throws InitializationException
57    {
58  307 try {
59  307 this.xercesGrammarPool = Class.forName("org.apache.xerces.util.XMLGrammarPoolImpl").newInstance();
60    } catch (Exception e) {
61    // There's no Xerces JAR in the classpath, don't do grammar caching for Xerces.
62    }
63    }
64   
 
65  295 toggle @Override
66    public XMLReader createXMLReader() throws SAXException, ParserConfigurationException
67    {
68  295 XMLReader xmlReader;
69   
70    // Try to optimize speed by caching the DTD parsing for Xerces
71    // (i.e. if Xerces is available on the classpath).
72  295 try {
73    // See http://xerces.apache.org/xerces2-j/faq-grammars.html#faq-1
74  295 Object xercesConfiguration =
75    Class.forName("org.apache.xerces.parsers.XML11NonValidatingConfiguration").newInstance();
76  295 Method setPropertyMethod = xercesConfiguration.getClass().getMethod(
77    "setProperty", String.class, Object.class);
78  295 setPropertyMethod.invoke(xercesConfiguration, "http://apache.org/xml/properties/internal/grammar-pool",
79    this.xercesGrammarPool);
80  295 xmlReader = (XMLReader) Class.forName("org.apache.xerces.parsers.SAXParser").getConstructor(
81    Class.forName("org.apache.xerces.xni.parser.XMLParserConfiguration")).newInstance(xercesConfiguration);
82    } catch (Exception e) {
83    // There's no Xerces JAR in the classpath, don't do grammar caching for Xerces.
84    // Default to standard SAX parsing which will be slower.
85  0 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
86  0 SAXParser parser = parserFactory.newSAXParser();
87  0 xmlReader = parser.getXMLReader();
88    }
89   
90  295 return xmlReader;
91    }
92    }