1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37 @NotThreadSafe
38 abstract class AbstractReporter
39 {
40
41
42
43
44
45
46
47 protected static final String XHTML = "http://www.w3.org/1999/xhtml";
48
49
50
51
52
53
54 protected final Namespace ns;
55
56
57
58
59 protected final Namespace nsXhtml;
60
61
62
63
64 protected Element rootElement;
65
66
67
68
69
70 protected AbstractReporter(final String namespace)
71 {
72 ns = Namespace.getNamespace(namespace);
73 nsXhtml = Namespace.getNamespace(XHTML);
74 }
75
76
77
78
79
80
81
82
83
84
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
100
101
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
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
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
257
258 }