/
MetatypesPlugin.java
321 lines (265 loc) · 10.5 KB
/
MetatypesPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package biz.aQute.bnd.reporter.plugins.entries.bundle;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import aQute.bnd.annotation.plugin.BndPlugin;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
import aQute.bnd.service.Plugin;
import aQute.bnd.service.reporter.ReportEntryPlugin;
import aQute.lib.xml.XML;
import aQute.service.reporter.Reporter;
import biz.aQute.bnd.reporter.component.dto.AttributeDefinitionDTO;
import biz.aQute.bnd.reporter.component.dto.IconDTO;
import biz.aQute.bnd.reporter.component.dto.ObjectClassDefinitionDTO;
import biz.aQute.bnd.reporter.component.dto.OptionDTO;
import biz.aQute.bnd.reporter.generator.EntryNamesReference;
import biz.aQute.bnd.reporter.helpers.LocaleHelper;
/**
* This plugin allows to add all the metatype resources defined in a bundle to
* the report.
*/
@BndPlugin(name = "entry." + EntryNamesReference.METATYPES)
public class MetatypesPlugin implements ReportEntryPlugin<Jar>, Plugin {
final private static String METATYPE_DIR = "OSGI-INF/metatype";
final private static String DEFAULT_LOCALIZATION_BASE = "OSGI-INF/l10n/";
final private static String DEFAULT_BUNDLE_LOCALIZATION_BASE = "OSGI-INF/l10n/bundle";
final private static String DESIGNATE_TAG = "Designate";
final private static String OCD_TAG = "OCD";
final private static String AD_TAG = "AD";
final private static String ICON_TAG = "Icon";
final private static String OPTION_TAG = "Option";
final private static String LOCALIZATION_ATTR = "localization";
final private static String ID_ATTR = "id";
final private static String NAME_ATTR = "name";
final private static String DESCRIPTION_ATTR = "description";
final private static String RESOURCE_ATTR = "resource";
final private static String SIZE_ATTR = "size";
final private static String PID_ATTR = "pid";
final private static String FACTORY_PID_ATTR = "factoryPid";
final private static String CARDINALITY_ATTR = "cardinality";
final private static String TYPE_ATTR = "type";
final private static String DEFAULT_ATTR = "default";
final private static String REQUIRED_ATTR = "required";
final private static String MIN_ATTR = "min";
final private static String MAX_ATTR = "max";
final private static String LABEL_ATTR = "label";
final private static String VALUE_ATTR = "value";
private Reporter _reporter;
private final Map<String, String> _properties = new HashMap<>();
public MetatypesPlugin() {
_properties.put(ReportEntryPlugin.ENTRY_NAME_PROPERTY, EntryNamesReference.METATYPES);
_properties.put(ReportEntryPlugin.SOURCE_CLASS_PROPERTY, Jar.class.getCanonicalName());
}
@Override
public List<ObjectClassDefinitionDTO> extract(final Jar jar, final Locale locale) throws Exception {
Objects.requireNonNull(jar, "jar");
Objects.requireNonNull(locale, "locale");
final DocumentBuilder db = XML.newDocumentBuilderFactory()
.newDocumentBuilder();
final List<ObjectClassDefinitionDTO> metas = new LinkedList<>();
if (jar.getDirectories()
.get(METATYPE_DIR) != null) {
for (final Resource metatype : jar.getDirectories()
.get(METATYPE_DIR)
.values()) {
try (InputStream in = metatype.openInputStream()) {
Document doc = null;
try {
doc = db.parse(in);
} catch (final Exception e) {
_reporter.warning("unable to parse metatype xml file", e);
doc = null;
}
if (doc != null) {
final Element root = doc.getDocumentElement();
final ObjectClassDefinitionDTO meta = new ObjectClassDefinitionDTO();
final NodeList ocds = root.getElementsByTagName(OCD_TAG);
if (ocds.getLength() > 0) {
final Element ocd = ((Element) ocds.item(0));
if (ocd.hasAttribute(ID_ATTR)) {
meta.id = ocd.getAttribute(ID_ATTR);
} else {
meta.id = "!! MISSING !!";
_reporter.warning("missing ocd id attribute in metatype");
}
final LocaleHelper loHelper;
if (root.hasAttribute(LOCALIZATION_ATTR)) {
loHelper = getLocalizationHelper(jar, locale, root.getAttribute(LOCALIZATION_ATTR));
} else {
loHelper = getLocalizationHelper(jar, locale, DEFAULT_LOCALIZATION_BASE + meta.id);
}
if (ocd.hasAttribute(NAME_ATTR)) {
final String name = loHelper.get(ocd.getAttribute(NAME_ATTR));
if (name == null) {
meta.name = "!! MISSING !!";
_reporter.warning("missing ocd name attribute for metatype: %s", meta.id);
} else {
meta.name = name;
}
} else {
meta.name = "!! MISSING !!";
_reporter.warning("missing ocd name attribute for metatype: %s", meta.id);
}
if (ocd.hasAttribute(DESCRIPTION_ATTR)) {
final String description = loHelper.get(ocd.getAttribute(DESCRIPTION_ATTR));
if (description != null) {
meta.description = description;
}
}
final NodeList designates = root.getElementsByTagName(DESIGNATE_TAG);
for (int i = 0; i < designates.getLength(); i++) {
final Element designate = (Element) designates.item(i);
if (designate.hasAttribute(PID_ATTR)) {
meta.pids.add(designate.getAttribute(PID_ATTR));
}
if (designate.hasAttribute(FACTORY_PID_ATTR)) {
meta.factoryPids.add(designate.getAttribute(FACTORY_PID_ATTR));
}
}
final NodeList icons = ocd.getElementsByTagName(ICON_TAG);
for (int i = 0; i < icons.getLength(); i++) {
final Element icon = (Element) icons.item(i);
final IconDTO iconDto = new IconDTO();
if (icon.hasAttribute(RESOURCE_ATTR)) {
iconDto.resource = icon.getAttribute(RESOURCE_ATTR);
} else {
iconDto.resource = "!! MISSING !!";
_reporter.warning("missing ocd resource icon attribute for metatype: %s", meta.id);
}
if (icon.hasAttribute(SIZE_ATTR)) {
iconDto.size = Integer.valueOf(icon.getAttribute(SIZE_ATTR));
}
meta.icons.add(iconDto);
}
final NodeList ads = ocd.getElementsByTagName(AD_TAG);
for (int i = 0; i < ads.getLength(); i++) {
final Element ad = (Element) ads.item(i);
final AttributeDefinitionDTO attrDto = new AttributeDefinitionDTO();
if (ad.hasAttribute(NAME_ATTR)) {
final String name = loHelper.get(ad.getAttribute(NAME_ATTR));
if (name == null) {
attrDto.name = "!! MISSING !!";
_reporter.warning("missing AD name attribute for metatype: %s", meta.id);
} else {
attrDto.name = name;
}
} else {
attrDto.name = "!! MISSING !!";
_reporter.warning("missing AD name attribute for metatype: %s", meta.id);
}
if (ad.hasAttribute(DESCRIPTION_ATTR)) {
final String description = loHelper.get(ad.getAttribute(DESCRIPTION_ATTR));
if (description != null) {
attrDto.description = description;
}
}
if (ad.hasAttribute(ID_ATTR)) {
attrDto.id = ad.getAttribute(ID_ATTR);
} else {
attrDto.id = "!! MISSING !!";
_reporter.warning("missing AD id attribute for metatype: %s", meta.id);
}
if (ad.hasAttribute(CARDINALITY_ATTR)) {
attrDto.cardinality = Integer.parseInt(ad.getAttribute(CARDINALITY_ATTR));
}
if (ad.hasAttribute(TYPE_ATTR)) {
attrDto.type = ad.getAttribute(TYPE_ATTR);
}
if (ad.hasAttribute(DEFAULT_ATTR)) {
final List<String> values = new LinkedList<>(
Arrays.asList(ad.getAttribute(DEFAULT_ATTR)
.split("(?<!\\\\),")));
final List<String> valuesEsc = new LinkedList<>();
for (final String v : values) {
valuesEsc.add(v.replaceAll("\\\\(?<!\\\\\\\\)", ""));
}
for (final String v : valuesEsc) {
attrDto.values.add(v);
}
}
if (ad.hasAttribute(REQUIRED_ATTR)) {
attrDto.required = Boolean.parseBoolean(ad.getAttribute(REQUIRED_ATTR));
}
if (ad.hasAttribute(MAX_ATTR)) {
attrDto.max = ad.getAttribute(MAX_ATTR);
}
if (ad.hasAttribute(MIN_ATTR)) {
attrDto.min = ad.getAttribute(MIN_ATTR);
}
final NodeList options = ad.getElementsByTagName(OPTION_TAG);
for (int j = 0; j < options.getLength(); j++) {
final Element option = (Element) options.item(j);
final OptionDTO oDto = new OptionDTO();
String value = null;
if (option.hasAttribute(VALUE_ATTR)) {
value = option.getAttribute(VALUE_ATTR);
oDto.value = value;
} else {
oDto.value = "!! MISSING !!";
_reporter.warning("missing AD option value attribute for metatype %s", meta.id);
}
if (option.hasAttribute(LABEL_ATTR)) {
final String label = loHelper.get(option.getAttribute(LABEL_ATTR));
if (label != null) {
oDto.label = label;
} else {
oDto.label = oDto.value;
}
} else {
oDto.label = oDto.value;
}
attrDto.options.add(oDto);
}
meta.attributes.add(attrDto);
}
metas.add(meta);
}
}
}
}
}
return !metas.isEmpty() ? metas : null;
}
private LocaleHelper getLocalizationHelper(final Jar jar, final Locale locale, final String basePath)
throws Exception {
LocaleHelper result;
result = LocaleHelper.createIfPresent(jar, locale, basePath);
if (result == null) {
String path = jar.getManifest()
.getMainAttributes()
.getValue(Constants.BUNDLE_LOCALIZATION);
if (path == null || path.isEmpty()) {
path = DEFAULT_BUNDLE_LOCALIZATION_BASE;
}
result = LocaleHelper.createIfPresent(jar, locale, path);
}
if (result == null) {
return LocaleHelper.empty();
}
return result;
}
@Override
public Map<String, String> getProperties() {
return Collections.unmodifiableMap(_properties);
}
@Override
public void setProperties(final Map<String, String> map) throws Exception {
_properties.putAll(map);
}
@Override
public void setReporter(final Reporter processor) {
_reporter = processor;
}
}