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.Arg;
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      this.output = Arg.checkNotNull("output", output);
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    @Override
83    public void handle(final PropertyReportSet reportSet) throws ReportException
84    {
85      flush(reportSet.getName());
86  
87      final StringBuilder buffer = new StringBuilder(256);
88      buffer.append(reportSet.getName()).append('\n')
89          .append(reportSet.getComment());
90      currentComment = buffer.toString();
91    }
92  
93    private void flush(final String name) throws ReportException
94    {
95      if (!currentProperties.isEmpty())
96      {
97        try
98        {
99          currentProperties.setHeader(currentComment);
100         currentProperties.save(output, "ISO8859-1");
101       }
102       catch (final ConfigurationException e)
103       {
104         final String message =
105             name != null ? "Cannot write report set '" + name + "'."
106                 : "Cannot flush.";
107         throw new ReportException(message, e);
108       }
109 
110       currentProperties.clear();
111     }
112   }
113 
114   @Override
115   public void handle(final PropertyReportItem item) throws ReportException
116   {
117     super.handle(item);
118 
119     final PropertyDescriptor descriptor = item.getDescriptor();
120     final String key = descriptor.getKey().toString();
121 
122     final PropertyExpression expression = descriptor.getDefaultExpression();
123     final String value =
124         expression != null && expression.getExpression() != null ? expression
125             .getExpression() : "";
126 
127     currentProperties.setProperty(key, value);
128   }
129 
130   /**
131    * Flushes the latest report set and items.
132    */
133   public void flush()
134   {
135     flush(null);
136   }
137 
138   // --- object basics --------------------------------------------------------
139 
140 }