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.OutputStream;
19  
20  import org.apache.commons.configuration.ConfigurationException;
21  import org.apache.commons.configuration.PropertiesConfiguration;
22  
23  import de.smartics.properties.api.core.domain.PropertyDescriptor;
24  import de.smartics.properties.api.core.domain.PropertyExpression;
25  import de.smartics.properties.report.app.ReportException;
26  import de.smartics.properties.report.data.PropertyReportItem;
27  import de.smartics.properties.report.data.PropertyReportSet;
28  import de.smartics.util.lang.Arguments;
29  
30  /**
31   * Stores every report item in a property set.
32   */
33  public final class PropertiesPropertyReport extends AbstractPropertyReport
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The current comment to prepend to the collected properties.
43     */
44    private String currentComment;
45  
46    /**
47     * The currently collected properties.
48     */
49    private final PropertiesConfiguration currentProperties =
50        new PropertiesConfiguration();
51  
52    /**
53     * The stream to write to.
54     */
55    private final OutputStream output;
56  
57    // ****************************** Initializer *******************************
58  
59    // ****************************** Constructors ******************************
60  
61    /**
62     * Constructor.
63     *
64     * @param output the stream to write to.
65     * @see de.smartics.properties.reports.AbstractPropertyReport#AbstractPropertyReport()
66     */
67    public PropertiesPropertyReport(final OutputStream output)
68    {
69      Arguments.checkNotNull("output", output);
70      this.output = output;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    // --- get&set --------------------------------------------------------------
80  
81    // --- business -------------------------------------------------------------
82  
83    /**
84     * {@inheritDoc}
85     *
86     * @see de.smartics.properties.report.data.PropertyReport#handle(de.smartics.properties.report.data.PropertyReportSet)
87     */
88    @Override
89    public void handle(final PropertyReportSet reportSet) throws ReportException
90    {
91      flush(reportSet.getName());
92  
93      final StringBuilder buffer = new StringBuilder(256);
94      buffer.append(reportSet.getName()).append('\n')
95          .append(reportSet.getComment());
96      currentComment = buffer.toString();
97    }
98  
99    private void flush(final String name) throws ReportException
100   {
101     if (!currentProperties.isEmpty())
102     {
103       try
104       {
105         currentProperties.setHeader(currentComment);
106         currentProperties.save(output, "ISO8859-1");
107       }
108       catch (final ConfigurationException e)
109       {
110         final String message =
111             name != null ? "Cannot write report set '" + name + "'."
112                 : "Cannot flush.";
113         throw new ReportException(message, e);
114       }
115 
116       currentProperties.clear();
117     }
118   }
119 
120   /**
121    * {@inheritDoc}
122    *
123    * @see de.smartics.properties.report.data.PropertyReport#handle(de.smartics.properties.report.data.PropertyReportItem)
124    */
125   @Override
126   public void handle(final PropertyReportItem item) throws ReportException
127   {
128     super.handle(item);
129 
130     final PropertyDescriptor descriptor = item.getDescriptor();
131     final String key = descriptor.getKey().toString();
132 
133     final PropertyExpression expression = descriptor.getDefaultExpression();
134     final String value =
135         expression != null && expression.getExpression() != null ? expression
136             .getExpression() : "";
137 
138     currentProperties.setProperty(key, value);
139   }
140 
141   /**
142    * Flushes the latest report set and items.
143    */
144   public void flush()
145   {
146     flush(null);
147   }
148 
149   // --- object basics --------------------------------------------------------
150 
151 }