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.apache.commons.lang.StringUtils;
25  import org.jdom.Document;
26  import org.jdom.Element;
27  import org.jdom.Namespace;
28  import org.jdom.output.XMLOutputter;
29  
30  import de.smartics.properties.api.core.domain.DocumentMetaData;
31  
32  /**
33   * Base implementation for reporter. A reporter is a helper to render commonly
34   * (i.e. by {@link XmlPropertyReporter} and {@link PropertySet Reporter}) used meta
35   * data. The report concept is not part of the public API.
36   */
37  @NotThreadSafe
38  abstract class AbstractReporter
39  { // NOPMD
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The XHTML namespace.
46     */
47    protected static final String XHTML = "http://www.w3.org/1999/xhtml";
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The namespace of the document that is created.
53     */
54    protected final Namespace ns;
55  
56    /**
57     * The XHTML namespace.
58     */
59    protected final Namespace nsXhtml;
60  
61    /**
62     * The root element of the created document.
63     */
64    protected Element rootElement;
65  
66    // ****************************** Initializer *******************************
67  
68    // ****************************** Constructors ******************************
69  
70    protected AbstractReporter(final String namespace)
71    {
72      ns = Namespace.getNamespace(namespace);
73      nsXhtml = Namespace.getNamespace(XHTML);
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    // --- get&set --------------------------------------------------------------
83  
84    // --- business -------------------------------------------------------------
85  
86    protected final Document buildDocument(final String rootGi)
87    {
88      final String name = getName();
89      rootElement = new Element(rootGi, ns);
90      rootElement.setAttribute("name", name);
91  
92      writeMetaData();
93  
94      final Document document = new Document(rootElement);
95      return document;
96    }
97  
98    /**
99     * Returns the unique identifier of the report to be created.
100    *
101    * @return the unique identifier of the report to be created.
102    */
103   protected abstract String getName();
104 
105   protected abstract String getSpace();
106 
107   protected abstract String getTitle();
108 
109   protected abstract DocumentMetaData getMetaData();
110 
111   protected abstract String getComment();
112 
113   private void writeMetaData()
114   {
115     writeIdentification();
116     writeFiling();
117     writeDescription();
118   }
119 
120   private void writeIdentification()
121   {
122     final Element identificationElement = new Element("identification", ns);
123 
124     final String space = getSpace();
125     addElement(identificationElement, "space", space);
126 
127     final String title = getTitle();
128     addElement(identificationElement, "title", title);
129 
130     if (!identificationElement.getChildren().isEmpty())
131     {
132       rootElement.addContent(identificationElement);
133     }
134   }
135 
136   private void writeFiling()
137   {
138     final DocumentMetaData metadata = getMetaData();
139     final Element filingElement = new Element("filing", ns);
140 
141     final List<String> parents = metadata.getParents();
142     addItems(parents, filingElement, "parents", "parent");
143 
144     final List<String> categories = metadata.getCategories();
145     addItems(categories, filingElement, "categories", "category");
146 
147     final List<String> tags = metadata.getTags();
148     addItems(tags, filingElement, "tags", "tag");
149 
150     final String sortKey = metadata.getSortKey();
151     addElement(filingElement, "sortKey", sortKey);
152 
153     if (!filingElement.getChildren().isEmpty())
154     {
155       rootElement.addContent(filingElement);
156     }
157   }
158 
159   protected final void addItems(final List<String> items,
160       final Element rootElement, final String itemsGi, final String itemGi)
161   {
162     if (!items.isEmpty())
163     {
164       final Element containerElement = new Element(itemsGi, ns);
165       for (final String item : items)
166       {
167         final Element itemElement = new Element(itemGi, ns);
168         itemElement.setText(item);
169         containerElement.addContent(itemElement);
170       }
171       rootElement.addContent(containerElement);
172     }
173   }
174 
175   protected final void writeDescription()
176   {
177     final DocumentMetaData metadata = getMetaData();
178     final Element descriptionElement = new Element("description", ns);
179 
180     final List<String> categories = metadata.getAudience();
181     addItems(categories, descriptionElement, "audience", "member");
182 
183     final String shortDescription = metadata.getShortDescription();
184     addDescElement(descriptionElement, "shortDescription", shortDescription);
185 
186     final String summary = metadata.getSummary();
187     addDescElement(descriptionElement, "summary", summary);
188 
189     final List<String> notes = metadata.getNotes();
190     if (!notes.isEmpty())
191     {
192       final Element notesElement = new Element("notes", ns);
193       final Element ol = new Element("ol", nsXhtml);
194       for (final String note : notes)
195       {
196         final Element li = new Element("li", nsXhtml);
197         li.setText(note);
198         ol.addContent(li);
199       }
200       notesElement.addContent(ol);
201 
202       descriptionElement.addContent(notesElement);
203     }
204 
205     // no authors provided currently
206 
207     if (!descriptionElement.getChildren().isEmpty())
208     {
209       rootElement.addContent(descriptionElement);
210     }
211   }
212 
213   protected final void addElement(final Element rootElement, final String gi,
214       final String content)
215   {
216     if (StringUtils.isNotBlank(content))
217     {
218       final Element element = new Element(gi, ns);
219       element.setText(content);
220       rootElement.addContent(element);
221     }
222   }
223 
224   protected final void addDescElement(final Element rootElement,
225       final String gi, final String content)
226   {
227     if (StringUtils.isNotBlank(content))
228     {
229       final Element element = new Element(gi, ns);
230       writeXmlSubStructure(element, content);
231       rootElement.addContent(element);
232     }
233   }
234 
235   protected final void writeSpecification(final String comment)
236   {
237     final Element specification = new Element("specification", ns);
238     writeXmlSubStructure(specification, comment);
239     rootElement.addContent(specification);
240   }
241 
242   protected final void writeXmlSubStructure(final Element element,
243       final String xmlContent)
244   {
245     // FIXME: Handle XML properly
246     element.setText(xmlContent);
247   }
248 
249   protected final void writeXml(final OutputStream out, final Document document)
250     throws IOException
251   {
252     final XMLOutputter outp = new XMLOutputter();
253     outp.output(document, out);
254   }
255 
256   // --- object basics --------------------------------------------------------
257 
258 }