1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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.Arg; |
38 |
|
import de.smartics.util.lang.NullArgumentException; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
public final class PropertyCommentParser |
44 |
|
{ |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
1 |
private static final Logger LOG = LoggerFactory |
53 |
|
.getLogger(PropertyCommentParser.class); |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
1 |
private static final Namespace PROPERTY_NS = |
59 |
|
Namespace |
60 |
|
.getNamespace("http://www.smartics.de/schema/projectdoc/doctype/property/1"); |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private final PropertiesContext context; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
public PropertyCommentParser(final PropertiesContext context) |
81 |
|
throws NullArgumentException |
82 |
1 |
{ |
83 |
1 |
this.context = Arg.checkNotNull("context", context); |
84 |
1 |
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
public PropertyComment parse(final PropertyDescriptor descriptor, |
105 |
|
final Locale locale) |
106 |
|
{ |
107 |
1 |
final String path = context.createMetaInfPath(descriptor, locale); |
108 |
1 |
final ClassLoader loader = descriptor.getDeclaringType().getClassLoader(); |
109 |
|
|
110 |
1 |
InputStream input = null; |
111 |
|
try |
112 |
|
{ |
113 |
1 |
final InputStream resource = loader.getResourceAsStream(path); |
114 |
1 |
if (resource != null) |
115 |
|
{ |
116 |
1 |
input = new BufferedInputStream(resource); |
117 |
1 |
final PropertyComment comment = parse(path, input); |
118 |
1 |
return comment; |
119 |
|
} |
120 |
|
else |
121 |
|
{ |
122 |
0 |
LOG.warn("No comments for property descriptor '{}' found: {}", |
123 |
|
descriptor, path); |
124 |
|
} |
125 |
|
} |
126 |
0 |
catch (final CommentException e) |
127 |
|
{ |
128 |
0 |
LOG.warn("Cannot parse comments for property descriptor '{}': {}", |
129 |
|
descriptor, e); |
130 |
|
} |
131 |
|
finally |
132 |
|
{ |
133 |
1 |
IOUtils.closeQuietly(input); |
134 |
0 |
} |
135 |
|
|
136 |
0 |
return PropertyComment.EMPTY_COMMENT; |
137 |
|
} |
138 |
|
|
139 |
|
private PropertyComment parse(final String systemId, final InputStream input) |
140 |
|
throws CommentException |
141 |
|
{ |
142 |
|
try |
143 |
|
{ |
144 |
1 |
final SAXBuilder parser = new SAXBuilder(); |
145 |
1 |
final Document document = parser.build(input, systemId); |
146 |
|
|
147 |
1 |
final PropertyComment.Builder builder = new PropertyComment.Builder(); |
148 |
|
|
149 |
1 |
final Element rootNode = document.getRootElement(); |
150 |
|
|
151 |
1 |
final String comment = |
152 |
|
rootNode.getChildTextNormalize("specification", PROPERTY_NS); |
153 |
1 |
final PropertyValueComment valueComment = parseValueComment(rootNode); |
154 |
1 |
builder.withText(comment).with(valueComment); |
155 |
|
|
156 |
1 |
return builder.build(); |
157 |
|
} |
158 |
0 |
catch (final JDOMException e) |
159 |
|
{ |
160 |
0 |
throw new CommentException(systemId, e); |
161 |
|
} |
162 |
0 |
catch (final IOException e) |
163 |
|
{ |
164 |
0 |
throw new CommentException(systemId, e); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
@SuppressWarnings("unchecked") |
169 |
|
private PropertyValueComment parseValueComment(final Element rootNode) |
170 |
|
{ |
171 |
1 |
final Element valueRange = rootNode.getChild("valueRange", PROPERTY_NS); |
172 |
1 |
if (valueRange == null) |
173 |
|
{ |
174 |
0 |
return null; |
175 |
|
} |
176 |
|
|
177 |
1 |
final String summary = |
178 |
|
valueRange.getChildTextNormalize("summary", PROPERTY_NS); |
179 |
1 |
final PropertyValueComment comment = new PropertyValueComment(summary); |
180 |
|
|
181 |
1 |
final List<Element> elements = |
182 |
|
(List<Element>) valueRange.getChildren("element", PROPERTY_NS); |
183 |
1 |
for (final Element element : elements) |
184 |
|
{ |
185 |
5 |
final String value = element.getChildTextNormalize("value", PROPERTY_NS); |
186 |
5 |
final String description = |
187 |
|
element.getChildTextNormalize("description", PROPERTY_NS); |
188 |
5 |
comment.addValueComment(value, description); |
189 |
5 |
} |
190 |
|
|
191 |
1 |
return comment; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
} |