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  import java.util.List;
21  
22  import javax.annotation.concurrent.NotThreadSafe;
23  
24  import org.jdom.Document;
25  import org.jdom.Element;
26  
27  import de.smartics.properties.api.core.domain.DocumentMetaData;
28  import de.smartics.properties.api.core.domain.PropertyConstraint;
29  import de.smartics.properties.api.core.domain.PropertyDescriptor;
30  import de.smartics.properties.api.core.domain.PropertyKey;
31  import de.smartics.properties.api.core.domain.PropertyType;
32  import de.smartics.properties.api.core.domain.PropertyValueRange;
33  import de.smartics.properties.report.data.PropertyReportItem;
34  import de.smartics.properties.report.data.SourceInfo;
35  import de.smartics.properties.report.data.ValueComment;
36  import de.smartics.properties.utils.HtmlUtils;
37  
38  /**
39   * Helper to construct a projectdoc property document.
40   */
41  @NotThreadSafe
42  class PropertyReporter extends AbstractReporter
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    /**
49     * The namespace of the document that is created.
50     */
51    private static final String NAMESPACE =
52        "http://www.smartics.de/schema/projectdoc/doctype/property/1";
53  
54    // --- members --------------------------------------------------------------
55  
56    /**
57     * The information to be set to the report.
58     */
59    private final PropertyReportItem reportItem;
60  
61    /**
62     * The helper to cleanup HTML fragments.
63     */
64    private final HtmlUtils htmlUtils = new HtmlUtils("UTF-8"); // TODO encoding
65  
66    // ****************************** Initializer *******************************
67  
68    // ****************************** Constructors ******************************
69  
70    PropertyReporter(final PropertyReportItem reportItem)
71    {
72      super(NAMESPACE);
73      this.reportItem = reportItem;
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    // --- get&set --------------------------------------------------------------
83  
84    // --- business -------------------------------------------------------------
85  
86    void write(final OutputStream out) throws IOException
87    {
88      final Document document = buildDocument("property");
89  
90      renderPropertyMetadata();
91      renderPropertyDescriptor();
92      renderImplementation();
93  
94      writeXml(out, document);
95    }
96  
97    private void renderPropertyMetadata()
98    {
99      final String propertySet = reportItem.getPropertySet();
100     addElement(rootElement, "propertySet", propertySet);
101   }
102 
103   private void renderPropertyDescriptor()
104   {
105     final PropertyDescriptor descriptor = reportItem.getDescriptor();
106     if (descriptor != null)
107     {
108       final PropertyKey key = descriptor.getKey();
109       addElement(rootElement, "name", key);
110       final PropertyType type = descriptor.getType();
111       addElement(rootElement, "type", type.toString()); // NOPMD
112 
113       final String cleanComment = htmlUtils.clean(getComment());
114       writeSpecification(cleanComment);
115 
116       final String expression =
117           descriptor.getDefaultExpression().getExpression();
118       if (expression != null)
119       {
120         if (!expression.contains("${"))
121         {
122           addElement(rootElement, "defaultValue", expression);
123         }
124         else
125         {
126           addElement(rootElement, "defaultExpression", expression);
127         }
128       }
129 
130       addRanges(descriptor);
131 
132       addRestrictions(descriptor);
133     }
134   }
135 
136   private void addRanges(final PropertyDescriptor descriptor)
137   {
138     final PropertyValueRange<?> range = descriptor.getValueRange();
139     if (range != null)
140     {
141       final Element valueRangeElement = new Element("valueRange", ns);
142       final ValueComment valueComment = reportItem.getValueComment();
143       final String summary = valueComment.getSummary();
144       addElement(valueRangeElement, "summary", summary);
145 
146       final List<?> values = range.getValues();
147       for (final Object value : values)
148       {
149         final String description = valueComment.getValueComment(value);
150         final String cleanDescription = htmlUtils.clean(description);
151         final Element element = new Element("element", ns);
152 
153         addElement(element, "value", value);
154         addElement(element, "description", cleanDescription);
155 
156         valueRangeElement.addContent(element);
157       }
158 
159       rootElement.addContent(valueRangeElement);
160     }
161   }
162 
163   private void addRestrictions(final PropertyDescriptor descriptor)
164   {
165     final List<? extends PropertyConstraint<?>> constraints =
166         descriptor.getConstraints();
167     if (!constraints.isEmpty())
168     {
169       final Element valueContraintsElement =
170           new Element("valueConstraints", ns);
171 
172       for (final PropertyConstraint<?> constraint : constraints)
173       {
174         final Element element = new Element("constraint", ns);
175 
176         final String type = constraint.getClass().getName();
177         addElement(element, "type", type);
178 
179         final String description = constraint.getDescription();
180         final String cleanDescription = htmlUtils.clean(description);
181 
182         addElement(element, "description", cleanDescription);
183 
184         valueContraintsElement.addContent(element);
185       }
186 
187       rootElement.addContent(valueContraintsElement);
188     }
189   }
190 
191   private void addElement(final Element rootElement, final String gi,
192       final Object value)
193   {
194     if (value != null)
195     {
196       addElement(rootElement, gi, String.valueOf(value));
197     }
198   }
199 
200   private void renderImplementation()
201   {
202     final SourceInfo sourceInfo = reportItem.getSourceInfo();
203     if (sourceInfo != null)
204     {
205       final Element implementationElement = new Element("implementation", ns);
206 
207       final String type = sourceInfo.getElementTypeName();
208       addElement(implementationElement, "type", type);
209 
210       final String field = sourceInfo.getElementName();
211       addElement(implementationElement, "field", field);
212 
213       final int lineNumber = sourceInfo.getLineNumber();
214       addElement(implementationElement, "lineNumber",
215           String.valueOf(lineNumber));
216 
217       rootElement.addContent(implementationElement);
218     }
219   }
220 
221   /**
222    * {@inheritDoc}
223    *
224    * @see de.smartics.properties.reports.AbstractReporter#getMetaData()
225    */
226   @Override
227   protected DocumentMetaData getMetaData()
228   {
229     return reportItem.getMetaData();
230   }
231 
232   /**
233    * {@inheritDoc}
234    *
235    * @see de.smartics.properties.reports.AbstractReporter#getComment()
236    */
237   @Override
238   protected String getComment()
239   {
240     final String comment = reportItem.getComment();
241     final String cleanComment = htmlUtils.clean(comment);
242     return cleanComment;
243   }
244 
245   /**
246    * {@inheritDoc}
247    *
248    * @see de.smartics.properties.reports.AbstractReporter#getName()
249    */
250   @Override
251   protected String getName()
252   {
253     return reportItem.getName();
254   }
255 
256   /**
257    * {@inheritDoc}
258    *
259    * @see de.smartics.properties.reports.AbstractReporter#getSpace()
260    */
261   @Override
262   protected String getSpace()
263   {
264     return reportItem.getSpace();
265   }
266 
267   /**
268    * {@inheritDoc}
269    *
270    * @see de.smartics.properties.reports.AbstractReporter#getTitle()
271    */
272   @Override
273   protected String getTitle()
274   {
275     return reportItem.getTitle();
276   }
277 
278   // --- object basics --------------------------------------------------------
279 
280 }