View Javadoc

1   /*
2    * Copyright 2012-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.properties.reports;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  
21  import org.apache.commons.io.IOUtils;
22  
23  import de.smartics.properties.report.app.ReportException;
24  import de.smartics.properties.report.data.PropertyReportItem;
25  import de.smartics.properties.report.data.PropertyReportSet;
26  import de.smartics.util.io.StreamHandler;
27  import de.smartics.util.lang.Arg;
28  
29  /**
30   * Writes each report to a file.
31   */
32  public final class XmlPropertyReport extends AbstractPropertyReport
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The encoding used to write to the stream.
42     */
43    private final String encoding;
44  
45    /**
46     * The location for property set reports being written to.
47     */
48    private final StreamHandler propertySetReportsRootDir;
49  
50    /**
51     * The location for property reports being written to.
52     */
53    private final StreamHandler propertyReportsRootDir;
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Constructor using UTF-8 as encoding.
61     *
62     * @param propertySetReportsRootDir the location for property set reports
63     *          being written to.
64     * @param propertyReportsRootDir the location for property reports being
65     *          written to.
66     * @throws NullPointerException if {@code propertySetReportsRootDir} or
67     *           {@code propertyReportsRootDir} is <code>null</code>.
68     */
69    public XmlPropertyReport(final StreamHandler propertySetReportsRootDir,
70        final StreamHandler propertyReportsRootDir) throws NullPointerException
71    {
72      this("UTF-8", propertySetReportsRootDir, propertyReportsRootDir);
73    }
74  
75    /**
76     * Default constructor.
77     *
78     * @param encoding the encoding used to write to the stream.
79     * @param propertySetReportsRootDir the location for property set reports
80     *          being written to.
81     * @param propertyReportsRootDir the location for property reports being
82     *          written to.
83     * @throws NullPointerException if {@code encoding},
84     *           {@code propertySetReportsRootDir} or
85     *           {@code propertyReportsRootDir} is <code>null</code>.
86     * @throws IllegalArgumentException if {@code encoding} is blank.
87     */
88    public XmlPropertyReport(final String encoding,
89        final StreamHandler propertySetReportsRootDir,
90        final StreamHandler propertyReportsRootDir) throws NullPointerException,
91      IllegalArgumentException
92    {
93      this.encoding = Arg.checkNotBlank("encoding", encoding);
94      this.propertySetReportsRootDir =
95          Arg.checkNotNull("propertySetReportsRootDir", propertySetReportsRootDir);
96      this.propertyReportsRootDir =
97          Arg.checkNotNull("propertyReportsRootDir", propertyReportsRootDir);
98    }
99  
100   // ****************************** Inner Classes *****************************
101 
102   // ********************************* Methods ********************************
103 
104   // --- init -----------------------------------------------------------------
105 
106   // --- get&set --------------------------------------------------------------
107 
108   // --- business -------------------------------------------------------------
109 
110   @Override
111   public void handle(final PropertyReportSet reportSet) throws ReportException
112   {
113     final String resourceId = reportSet.getName() + ".xml";
114     OutputStream out = null;
115     try
116     {
117       out = propertySetReportsRootDir.openToWrite(resourceId);
118       final XmlPropertySetReporter reporter =
119           new XmlPropertySetReporter(encoding, reportSet);
120       reporter.write(out);
121     }
122     catch (final IOException e)
123     {
124       throw new ReportException("Cannot write report '" + resourceId + "'.", e);
125     }
126     finally
127     {
128       IOUtils.closeQuietly(out);
129     }
130   }
131 
132   @Override
133   public void handle(final PropertyReportItem item) throws ReportException
134   {
135     super.handle(item);
136 
137     final String resourceId = item.getName() + ".xml";
138     OutputStream out = null;
139     try
140     {
141       out = propertyReportsRootDir.openToWrite(resourceId);
142       final XmlPropertyReporter reporter =
143           new XmlPropertyReporter(encoding, item);
144       reporter.write(out);
145     }
146     catch (final IOException e)
147     {
148       throw new ReportException("Cannot write report '" + resourceId + "'.", e);
149     }
150     finally
151     {
152       IOUtils.closeQuietly(out);
153     }
154   }
155 
156   // --- object basics --------------------------------------------------------
157 
158 }