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

File AbstractStreamParser.java

 

Coverage histogram

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

Code metrics

0
15
4
1
112
61
6
0.4
3.75
4
1.5

Classes

Class Line # Actions
AbstractStreamParser 49 15 0% 6 2
0.894736889.5%
 

Contributing tests

This file is covered by 764 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.xml.internal.parser;
21   
22    import java.io.IOException;
23    import java.io.Reader;
24   
25    import javax.inject.Inject;
26    import javax.xml.parsers.ParserConfigurationException;
27    import javax.xml.parsers.SAXParser;
28    import javax.xml.parsers.SAXParserFactory;
29   
30    import org.xml.sax.InputSource;
31    import org.xml.sax.SAXException;
32    import org.xml.sax.XMLReader;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.phase.Initializable;
36    import org.xwiki.component.phase.InitializationException;
37    import org.xwiki.rendering.listener.Listener;
38    import org.xwiki.rendering.parser.ParseException;
39    import org.xwiki.rendering.parser.StreamParser;
40    import org.xwiki.rendering.parser.xml.ContentHandlerStreamParser;
41    import org.xwiki.rendering.parser.xml.ContentHandlerStreamParserFactory;
42   
43    /**
44    * Base class for XML based syntax stream parsers.
45    *
46    * @version $Id: 9d398ac64434f9c545d9372898b26912f21494c9 $
47    * @since 5.2M1
48    */
 
49    public abstract class AbstractStreamParser implements ContentHandlerStreamParserFactory, StreamParser, Initializable
50    {
51    /**
52    * Used to lookup parser instances.
53    */
54    @Inject
55    protected ComponentManager componentManager;
56   
57    /**
58    * Reuse the same factory.
59    */
60    private SAXParserFactory parserFactory;
61   
 
62  791 toggle @Override
63    public void initialize() throws InitializationException
64    {
65  791 this.parserFactory = SAXParserFactory.newInstance();
66    }
67   
 
68  607 toggle @Override
69    public ContentHandlerStreamParser createParser(Listener listener)
70    {
71  607 ContentHandlerStreamParser parser;
72  607 try {
73  607 parser = this.componentManager.getInstance(ContentHandlerStreamParser.class, getSyntax().toIdString());
74    } catch (ComponentLookupException e) {
75  0 throw new RuntimeException(
76    "Failed to create [" + getSyntax().toString() + "] ContentHandler stream parser", e);
77    }
78   
79  607 parser.setListener(listener);
80   
81  607 return parser;
82    }
83   
 
84  607 toggle @Override
85    public void parse(Reader source, Listener listener) throws ParseException
86    {
87  607 try {
88  607 parseXML(source, listener);
89    } catch (Exception e) {
90  0 throw new ParseException("Failed to parse input source", e);
91    }
92    }
93   
94    /**
95    * @param source the content to parse
96    * @param listener receive event for each element
97    * @throws ParserConfigurationException error when rendering
98    * @throws SAXException error when rendering
99    * @throws IOException error when rendering
100    */
 
101  607 toggle public void parseXML(Reader source, Listener listener) throws ParserConfigurationException, SAXException,
102    IOException
103    {
104  607 SAXParser saxParser = this.parserFactory.newSAXParser();
105  607 XMLReader xmlReader = saxParser.getXMLReader();
106   
107  607 ContentHandlerStreamParser parser = createParser(listener);
108  607 xmlReader.setContentHandler(parser);
109   
110  607 xmlReader.parse(new InputSource(source));
111    }
112    }