View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.export;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.parsers.ParserConfigurationException;
24  import javax.xml.transform.OutputKeys;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.stream.StreamResult;
30  
31  import org.w3c.dom.Document;
32  
33  import de.smartics.testdoc.core.doc.UnitTestDoc;
34  
35  /**
36   * Exports the gathered test documentation to XML.
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public class XmlExporter implements UnitTestDocExporter
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The builder for XML documents.
51     */
52    private final DocumentBuilder builder; // NOPMD
53  
54    /**
55     * The flag determines whether or not the XML file should be pretty-printed (
56     * <code>true</code>) or not (<code>false</code>).
57     */
58    private final boolean prettyPrint;
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Convenience constructor with pretty print turned on.
66     *
67     * @throws ParserConfigurationException if the XML infrastructure cannot be
68     *           established.
69     */
70    public XmlExporter() throws ParserConfigurationException
71    {
72      this(true);
73    }
74  
75    /**
76     * Default constructor.
77     *
78     * @param prettyPrint the flag determines whether or not the XML file should
79     *          be pretty-printed ( <code>true</code>) or not (<code>false</code>
80     *          ).
81     * @throws ParserConfigurationException if the XML infrastructure cannot be
82     *           established.
83     */
84    public XmlExporter(final boolean prettyPrint)
85      throws ParserConfigurationException
86    {
87      final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
88      this.builder = factory.newDocumentBuilder();
89      this.prettyPrint = prettyPrint;
90    }
91  
92    // ****************************** Inner Classes *****************************
93  
94    // ********************************* Methods ********************************
95  
96    // --- init -----------------------------------------------------------------
97  
98    // --- get&set --------------------------------------------------------------
99  
100   /**
101    * {@inheritDoc}
102    *
103    * @return <code>xml</code>.
104    * @see de.smartics.testdoc.core.export.UnitTestDocExporter#getFileExtension()
105    */
106   @Override
107   public String getFileExtension()
108   {
109     return "xml";
110   }
111 
112   // --- business -------------------------------------------------------------
113 
114   /**
115    * {@inheritDoc}
116    *
117    * @see de.smartics.testdoc.core.export.UnitTestDocExporter#export(de.smartics.testdoc.core.doc.UnitTestDoc,
118    *      java.io.OutputStream)
119    */
120   @Override
121   public void export(final UnitTestDoc testDoc, final OutputStream output)
122     throws IOException, NullPointerException
123   {
124     if (testDoc == null)
125     {
126       throw new NullPointerException("The test doc instance must not be 'null'");
127     }
128     if (output == null)
129     {
130       throw new NullPointerException("The test doc instance must not be 'null'");
131     }
132 
133     final XmlDocumentBuilder docBuilder = createBuilder();
134     final Document document = docBuilder.build(testDoc);
135     serialize(document, output);
136   }
137 
138   private XmlDocumentBuilder createBuilder()
139   {
140     final Document document = builder.newDocument();
141     final XmlDocumentBuilder builder = new XmlDocumentBuilder(document);
142     return builder;
143   }
144 
145   /**
146    * Writes the DOM document to the given stream.
147    *
148    * @param document the document to write.
149    * @param out the stream to write to.
150    * @throws IOException on any problem writing to the stream.
151    */
152   private void serialize(final Document document, final OutputStream out)
153     throws IOException
154   {
155     try
156     {
157       final TransformerFactory factory = TransformerFactory.newInstance();
158       final Transformer serializer = factory.newTransformer();
159       if (prettyPrint)
160       {
161         serializer.setOutputProperty(OutputKeys.INDENT, "yes");
162         serializer.setOutputProperty(
163             "{http://xml.apache.org/xslt}indent-amount", "2");
164       }
165 
166       serializer.transform(new DOMSource(document), new StreamResult(out));
167     }
168     catch (final TransformerException e)
169     {
170       throw new IOException("Cannot write XML document.", e);
171     }
172   }
173 
174   // --- object basics --------------------------------------------------------
175 
176 }