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

File AbstractXMLInputFilterStream.java

 

Coverage histogram

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

Code metrics

4
12
3
1
82
45
6
0.5
4
3
2

Classes

Class Line # Actions
AbstractXMLInputFilterStream 42 12 0% 6 3
0.8421052784.2%
 

Contributing tests

This file is covered by 25 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.filter.xml.internal.input;
21   
22    import java.io.IOException;
23   
24    import javax.xml.stream.XMLEventReader;
25    import javax.xml.stream.XMLEventWriter;
26    import javax.xml.stream.XMLInputFactory;
27   
28    import org.xwiki.filter.FilterException;
29    import org.xwiki.filter.input.InputFilterStream;
30    import org.xwiki.filter.input.InputSource;
31    import org.xwiki.filter.input.InputStreamInputSource;
32    import org.xwiki.filter.input.ReaderInputSource;
33    import org.xwiki.filter.xml.input.XMLInputProperties;
34   
35    import javanet.staxutils.XMLStreamUtils;
36   
37    /**
38    * @param <P>
39    * @version $Id: d9861c63d78b2d6b3d1c7f93fa367c93e61d8311 $
40    * @since 6.2M1
41    */
 
42    public abstract class AbstractXMLInputFilterStream<P extends XMLInputProperties> implements InputFilterStream
43    {
44    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
45   
46    protected P parameters;
47   
 
48  26 toggle public AbstractXMLInputFilterStream(P parameters)
49    {
50  26 this.parameters = parameters;
51    }
52   
 
53  26 toggle @Override
54    public void read(Object listener) throws FilterException
55    {
56  26 try {
57  26 InputSource source = this.parameters.getSource();
58   
59  26 XMLEventReader xmlEventReader;
60   
61  26 if (source instanceof ReaderInputSource) {
62  13 xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(((ReaderInputSource) source).getReader());
63  13 } else if (source instanceof InputStreamInputSource) {
64  13 xmlEventReader = XML_INPUT_FACTORY.createXMLEventReader(((InputStreamInputSource) source).getInputStream());
65    } else {
66  0 throw new FilterException("Unknown source type [" + source.getClass() + "]");
67    }
68   
69  26 XMLStreamUtils.copy(xmlEventReader, createXMLEventWriter(listener, this.parameters));
70    } catch (Exception e) {
71  0 throw new FilterException("Faild to parse XML source", e);
72    }
73    }
74   
75    protected abstract XMLEventWriter createXMLEventWriter(Object listener, P parameters);
76   
 
77  26 toggle @Override
78    public void close() throws IOException
79    {
80  26 this.parameters.getSource().close();
81    }
82    }