Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertyCommentParser |
|
|
3.75;3,75 |
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.spi.core.metadata.comment; |
|
17 | ||
18 | import java.io.BufferedInputStream; |
|
19 | import java.io.IOException; |
|
20 | import java.io.InputStream; |
|
21 | import java.util.List; |
|
22 | import java.util.Locale; |
|
23 | ||
24 | import org.apache.commons.io.IOUtils; |
|
25 | import org.jdom.Document; |
|
26 | import org.jdom.Element; |
|
27 | import org.jdom.JDOMException; |
|
28 | import org.jdom.Namespace; |
|
29 | import org.jdom.input.SAXBuilder; |
|
30 | import org.slf4j.Logger; |
|
31 | import org.slf4j.LoggerFactory; |
|
32 | ||
33 | import de.smartics.properties.api.core.domain.PropertiesContext; |
|
34 | import de.smartics.properties.api.core.domain.PropertyComment; |
|
35 | import de.smartics.properties.api.core.domain.PropertyDescriptor; |
|
36 | import de.smartics.properties.api.core.domain.PropertyValueComment; |
|
37 | import de.smartics.util.lang.Arguments; |
|
38 | import de.smartics.util.lang.NullArgumentException; |
|
39 | ||
40 | /** |
|
41 | * Parses property comments for a property descriptors. |
|
42 | */ |
|
43 | public final class PropertyCommentParser |
|
44 | { |
|
45 | // ********************************* Fields ********************************* |
|
46 | ||
47 | // --- constants ------------------------------------------------------------ |
|
48 | ||
49 | /** |
|
50 | * Reference to the logger for this class. |
|
51 | */ |
|
52 | 0 | private static final Logger LOG = LoggerFactory |
53 | .getLogger(PropertyCommentParser.class); |
|
54 | ||
55 | /** |
|
56 | * The namespace of the XML files this parser can parse. |
|
57 | */ |
|
58 | 0 | private static final Namespace PROPERTY_NS = |
59 | Namespace |
|
60 | .getNamespace("http://www.smartics.de/schema/projectdoc/doctype/property/1"); |
|
61 | ||
62 | // --- members -------------------------------------------------------------- |
|
63 | ||
64 | /** |
|
65 | * The properties context to fetch information from the META-INF folder. |
|
66 | */ |
|
67 | private final PropertiesContext context; |
|
68 | ||
69 | // ****************************** Initializer ******************************* |
|
70 | ||
71 | // ****************************** Constructors ****************************** |
|
72 | ||
73 | /** |
|
74 | * Default constructor. |
|
75 | * |
|
76 | * @param context the properties context to fetch information from the |
|
77 | * META-INF folder. |
|
78 | * @throws NullArgumentException if {@code context} is <code>null</code>. |
|
79 | */ |
|
80 | public PropertyCommentParser(final PropertiesContext context) |
|
81 | throws NullArgumentException |
|
82 | 0 | { |
83 | 0 | Arguments.checkNotNull("context", context); |
84 | ||
85 | 0 | this.context = context; |
86 | 0 | } |
87 | ||
88 | // ****************************** Inner Classes ***************************** |
|
89 | ||
90 | // ********************************* Methods ******************************** |
|
91 | ||
92 | // --- init ----------------------------------------------------------------- |
|
93 | ||
94 | // --- get&set -------------------------------------------------------------- |
|
95 | ||
96 | // --- business ------------------------------------------------------------- |
|
97 | ||
98 | /** |
|
99 | * Parses the comments for the given descriptor. |
|
100 | * |
|
101 | * @param descriptor the descriptor whose comments are requested to be parsed. |
|
102 | * @param locale the locale to select the comments. |
|
103 | * @return the comments for the given locale or the default comments, if the |
|
104 | * locale is not supported. |
|
105 | */ |
|
106 | public PropertyComment parse(final PropertyDescriptor descriptor, |
|
107 | final Locale locale) |
|
108 | { |
|
109 | 0 | final String path = context.createMetaInfPath(descriptor, locale); |
110 | 0 | final ClassLoader loader = descriptor.getClass().getClassLoader(); // NOPMD |
111 | ||
112 | 0 | InputStream input = null; |
113 | try |
|
114 | { |
|
115 | 0 | final InputStream resource = loader.getResourceAsStream(path); |
116 | 0 | if (resource != null) |
117 | { |
|
118 | 0 | input = new BufferedInputStream(resource); |
119 | 0 | final PropertyComment comment = parse(path, input); |
120 | 0 | return comment; |
121 | } |
|
122 | else |
|
123 | { |
|
124 | 0 | LOG.warn("No comments for property descriptor '{}' found: {}", |
125 | descriptor, path); |
|
126 | } |
|
127 | } |
|
128 | 0 | catch (final CommentException e) |
129 | { |
|
130 | 0 | LOG.warn("Cannot parse comments for property descriptor '{}': {}", |
131 | descriptor, e); |
|
132 | } |
|
133 | finally |
|
134 | { |
|
135 | 0 | IOUtils.closeQuietly(input); |
136 | 0 | } |
137 | ||
138 | 0 | return PropertyComment.EMPTY_COMMENT; |
139 | } |
|
140 | ||
141 | private PropertyComment parse(final String systemId, final InputStream input) |
|
142 | throws CommentException |
|
143 | { |
|
144 | try |
|
145 | { |
|
146 | 0 | final SAXBuilder parser = new SAXBuilder(); |
147 | 0 | final Document document = parser.build(input, systemId); |
148 | ||
149 | 0 | final PropertyComment.Builder builder = new PropertyComment.Builder(); |
150 | ||
151 | 0 | final Element rootNode = document.getRootElement(); |
152 | ||
153 | 0 | final String comment = |
154 | rootNode.getChildTextNormalize("specification", PROPERTY_NS); |
|
155 | 0 | final PropertyValueComment valueComment = parseValueComment(rootNode); |
156 | 0 | builder.withText(comment).withValueComment(valueComment); |
157 | ||
158 | 0 | return builder.build(); |
159 | } |
|
160 | 0 | catch (final JDOMException e) |
161 | { |
|
162 | 0 | throw new CommentException(systemId, e); |
163 | } |
|
164 | 0 | catch (final IOException e) |
165 | { |
|
166 | 0 | throw new CommentException(systemId, e); |
167 | } |
|
168 | } |
|
169 | ||
170 | @SuppressWarnings("unchecked") |
|
171 | private PropertyValueComment parseValueComment(final Element rootNode) |
|
172 | { |
|
173 | 0 | final Element valueRange = rootNode.getChild("valueRange", PROPERTY_NS); |
174 | 0 | if (valueRange == null) |
175 | { |
|
176 | 0 | return null; |
177 | } |
|
178 | ||
179 | 0 | final String summary = |
180 | valueRange.getChildTextNormalize("summary", PROPERTY_NS); |
|
181 | 0 | final PropertyValueComment comment = new PropertyValueComment(summary); |
182 | ||
183 | 0 | final List<Element> elements = |
184 | (List<Element>) valueRange.getChildren("element", PROPERTY_NS); |
|
185 | 0 | for (final Element element : elements) |
186 | { |
|
187 | 0 | final String value = element.getChildTextNormalize("value", PROPERTY_NS); |
188 | 0 | final String description = |
189 | element.getChildTextNormalize("description", PROPERTY_NS); |
|
190 | 0 | comment.addValueComment(value, description); |
191 | 0 | } |
192 | ||
193 | 0 | return comment; |
194 | } |
|
195 | ||
196 | // --- object basics -------------------------------------------------------- |
|
197 | ||
198 | } |