1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter.xml.internal.output

File XMLOutputFilterStreamUtils.java

 

Coverage histogram

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

Code metrics

10
14
3
1
86
47
8
0.57
4.67
3
2.67

Classes

Class Line # Actions
XMLOutputFilterStreamUtils 42 14 0% 8 3
0.888888988.9%
 

Contributing tests

This file is covered by 60 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.IOException;
23   
24    import javax.xml.stream.XMLOutputFactory;
25    import javax.xml.stream.XMLStreamException;
26    import javax.xml.stream.XMLStreamWriter;
27   
28    import org.xwiki.filter.FilterException;
29    import org.xwiki.filter.output.OutputStreamOutputTarget;
30    import org.xwiki.filter.output.OutputTarget;
31    import org.xwiki.filter.output.WriterOutputTarget;
32    import org.xwiki.filter.xml.output.ResultOutputTarget;
33    import org.xwiki.filter.xml.output.XMLOutputProperties;
34    import org.xwiki.xml.stax.StAXUtils;
35   
36    import javanet.staxutils.IndentingXMLStreamWriter;
37   
38    /**
39    * @version $Id: b751fc10b8b70ca5a1fb0d111bf53fe71d985e90 $
40    * @since 6.2M1
41    */
 
42    public final class XMLOutputFilterStreamUtils
43    {
44    private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
45   
46    /**
47    * @since 9.5.2
48    * @since 9.6RC1
49    */
 
50  15166 toggle public static XMLStreamWriter createXMLStreamWriter(XMLOutputProperties properties)
51    throws XMLStreamException, IOException, FilterException
52    {
53  15166 return createXMLStreamWriter(XML_OUTPUT_FACTORY, properties);
54    }
55   
 
56  15196 toggle public static XMLStreamWriter createXMLStreamWriter(XMLOutputFactory factory, XMLOutputProperties properties)
57    throws XMLStreamException, IOException, FilterException
58    {
59  15196 XMLStreamWriter xmlStreamWriter;
60   
61  15196 OutputTarget target = properties.getTarget();
62   
63  15196 if (target instanceof WriterOutputTarget) {
64  12121 xmlStreamWriter =
65    getXMLOutputFactory(factory).createXMLStreamWriter(((WriterOutputTarget) target).getWriter());
66  3075 } else if (target instanceof OutputStreamOutputTarget) {
67  3074 xmlStreamWriter = getXMLOutputFactory(factory)
68    .createXMLStreamWriter(((OutputStreamOutputTarget) target).getOutputStream(), properties.getEncoding());
69  1 } else if (target instanceof ResultOutputTarget) {
70  1 xmlStreamWriter = StAXUtils.getXMLStreamWriter(((ResultOutputTarget) target).getResult());
71    } else {
72  0 throw new FilterException("Unknown target type [" + target.getClass() + "]");
73    }
74   
75  15196 if (properties.isFormat()) {
76  13311 xmlStreamWriter = new IndentingXMLStreamWriter(xmlStreamWriter);
77    }
78   
79  15196 return xmlStreamWriter;
80    }
81   
 
82  15195 toggle private static XMLOutputFactory getXMLOutputFactory(XMLOutputFactory factory)
83    {
84  15195 return factory != null ? factory : XML_OUTPUT_FACTORY;
85    }
86    }