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