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

File AbstractXMLOutputFilterStream.java

 

Coverage histogram

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

Code metrics

12
25
4
1
120
75
11
0.44
6.25
4
2.75

Classes

Class Line # Actions
AbstractXMLOutputFilterStream 47 25 0% 11 8
0.8048780680.5%
 

Contributing tests

This file is covered by 24 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.output;
21   
22    import java.io.Closeable;
23    import java.io.IOException;
24   
25    import javax.xml.stream.FactoryConfigurationError;
26    import javax.xml.stream.XMLOutputFactory;
27    import javax.xml.stream.XMLStreamException;
28    import javax.xml.stream.XMLStreamWriter;
29    import javax.xml.transform.Result;
30    import javax.xml.transform.stax.StAXResult;
31   
32    import org.xwiki.filter.FilterException;
33    import org.xwiki.filter.output.OutputFilterStream;
34    import org.xwiki.filter.output.OutputStreamOutputTarget;
35    import org.xwiki.filter.output.OutputTarget;
36    import org.xwiki.filter.output.WriterOutputTarget;
37    import org.xwiki.filter.xml.output.ResultOutputTarget;
38    import org.xwiki.filter.xml.output.XMLOutputProperties;
39   
40    import javanet.staxutils.IndentingXMLStreamWriter;
41   
42    /**
43    * @param <P>
44    * @version $Id: e68c101dee2e49806c4075b644ae0adcea33203e $
45    * @since 6.2M1
46    */
 
47    public abstract class AbstractXMLOutputFilterStream<P extends XMLOutputProperties> implements OutputFilterStream
48    {
49    protected final P properties;
50   
51    protected final Result result;
52   
53    protected Object filter;
54   
 
55  25 toggle public AbstractXMLOutputFilterStream(P properties) throws FilterException, XMLStreamException, IOException
56    {
57  25 this.properties = properties;
58  25 this.result = createResult(this.properties);
59    }
60   
 
61  25 toggle protected Result createResult(P properties) throws FilterException, XMLStreamException, IOException
62    {
63  25 OutputTarget target = properties.getTarget();
64   
65  25 Result result;
66   
67  25 if (target instanceof ResultOutputTarget) {
68  0 result = ((ResultOutputTarget) target).getResult();
69    } else {
70  25 XMLOutputFactory factory = XMLOutputFactory.newInstance();
71   
72  25 XMLStreamWriter xmlStreamWriter;
73   
74  25 if (target instanceof WriterOutputTarget) {
75  24 xmlStreamWriter = factory.createXMLStreamWriter(((WriterOutputTarget) target).getWriter());
76  1 } else if (target instanceof OutputStreamOutputTarget) {
77  1 xmlStreamWriter =
78    factory.createXMLStreamWriter(((OutputStreamOutputTarget) target).getOutputStream(),
79    properties.getEncoding());
80    } else {
81  0 throw new FilterException("Unknown target type [" + target.getClass() + "]");
82    }
83   
84  25 if (properties.isFormat()) {
85  25 xmlStreamWriter = new IndentingXMLStreamWriter(xmlStreamWriter);
86    }
87   
88  25 result = new StAXResult(xmlStreamWriter);
89    }
90   
91  25 return result;
92    }
93   
 
94  25 toggle @Override
95    public Object getFilter() throws FilterException
96    {
97  25 if (this.filter == null) {
98  25 try {
99  25 this.filter = createFilter(this.properties);
100    } catch (Exception e) {
101  0 throw new FilterException("Failed to create filter", e);
102    }
103    }
104   
105  25 return this.filter;
106    }
107   
108    protected abstract Object createFilter(P parameters) throws XMLStreamException, FactoryConfigurationError,
109    FilterException;
110   
 
111  25 toggle @Override
112    public void close() throws IOException
113    {
114  25 if (this.filter instanceof Closeable) {
115  25 ((Closeable) this.filter).close();
116    }
117   
118  25 this.properties.getTarget().close();
119    }
120    }