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.maven.export.xml;
17  
18  import static de.smartics.testdoc.maven.export.xml.XmlExporter.TESTDOC_NS;
19  import java.io.IOException;
20  import java.util.List;
21  
22  import javax.xml.stream.XMLStreamException;
23  import javax.xml.stream.XMLStreamWriter;
24  
25  import de.smartics.maven.util.report.MessageHelper;
26  import de.smartics.testdoc.maven.export.AbstractOutputManager;
27  import de.smartics.testdoc.core.doc.UnitTestDoc;
28  import de.smartics.testdoc.report.index.ExportIndex;
29  
30  /**
31   * Manages the export of the report to XML.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  public class XmlOutputManager extends AbstractOutputManager
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The output stream to write to.
46     */
47    private final XMLStreamWriter xmlWriter;
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Default constructor.
55     *
56     * @param reportName the name of the report to export.
57     * @param messages the resource bundles with messages to display as labels in
58     *          the generated report.
59     * @param exporter the exporter that knows the format of the file to be
60     *          written.
61     * @param exportIndices the indices to created.
62     * @see de.smartics.testdoc.maven.export.AbstractOutputManager#
63     *      AbstractOutputManager(java.lang.String,
64     *      de.smartics.maven.util.report.MessageHelper,
65     *      de.smartics.testdoc.maven.export.xml.XmlExporter,
66     *      java.util.List<de.smartics.testdoc.report.index.ExportIndex>)
67     */
68    public XmlOutputManager(final String reportName,
69        final MessageHelper messages, final XmlExporter exporter,
70        final List<ExportIndex> exportIndices)
71    {
72      super(reportName, messages, exporter, exportIndices);
73      this.xmlWriter = exporter.getXmlWriter();
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    // --- get&set --------------------------------------------------------------
83  
84    // --- business -------------------------------------------------------------
85  
86    /**
87     * {@inheritDoc}
88     *
89     * @see de.smartics.testdoc.report.doc.OutputManager#finish()
90     */
91    @Override
92    public void finish() throws IOException
93    {
94      try
95      {
96        xmlWriter.writeStartDocument("UTF-8", "1.0");
97        xmlWriter.setPrefix("td", TESTDOC_NS);
98        xmlWriter.writeStartElement(TESTDOC_NS, "testdoc");
99        xmlWriter.writeNamespace("td", TESTDOC_NS);
100 
101       for (final UnitTestDoc testDoc : testDocs)
102       {
103         ((XmlExporter) exporter).export(testDoc);
104       }
105 
106       xmlWriter.writeEndElement();
107       xmlWriter.writeEndDocument();
108       xmlWriter.flush();
109     }
110     catch (final XMLStreamException e)
111     {
112       throw new IOException("Cannot create XML document.", e);
113     }
114     finally
115     {
116       if (xmlWriter != null)
117       {
118         try
119         {
120           xmlWriter.close();
121         }
122         catch (final XMLStreamException e)
123         {
124           // Close silently.
125         }
126       }
127     }
128   }
129 
130   // --- object basics --------------------------------------------------------
131 
132 }