File: ManifestHandler.java

package info (click to toggle)
libjpf-java 1.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,280 kB
  • sloc: java: 13,449; xml: 337; makefile: 17
file content (408 lines) | stat: -rw-r--r-- 20,153 bytes parent folder | download | duplicates (4)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.registry.xml;

import org.java.plugin.registry.ExtensionMultiplicity;
import org.java.plugin.registry.MatchingRule;
import org.java.plugin.registry.ParameterMultiplicity;
import org.java.plugin.registry.ParameterType;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;

/**
 * @version $Id$
 */
final class ManifestHandler extends BaseHandler {
    private ModelPluginManifest manifest = null;
    private ModelDocumentation documentation = null;
    private ModelPrerequisite prerequisite;
    private ModelLibrary library;
    private ModelExtensionPoint extensionPoint;
    private ModelExtension extension;
    private StringBuilder docText = null;
    private SimpleStack<ModelAttribute> attributeStack = null;
    private ModelAttribute attribute;
    private SimpleStack<ModelParameterDef> paramDefStack = null;
    private ModelParameterDef paramDef;
    private SimpleStack<ModelParameter> paramStack = null;
    private ModelParameter param;
    private StringBuilder paramValue = null;

    ManifestHandler(final EntityResolver anEntityResolver) {
        super(anEntityResolver);
    }
    
    /**
     * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
     *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(final String uri, final String localName,
            final String qName, final Attributes attributes)
            throws SAXException {
        if (log.isDebugEnabled()) {
            log.debug("startElement - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
                    + localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        String name = qName;
        if ("plugin".equals(name)) { //$NON-NLS-1$
            if (manifest != null) {
                throw new SAXException("unexpected [" + name //$NON-NLS-1$
                        + "] element (manifest already defined)"); //$NON-NLS-1$
            }
            manifest = new ModelPluginDescriptor();
            manifest.setId(attributes.getValue("id")); //$NON-NLS-1$
            manifest.setVersion(attributes.getValue("version")); //$NON-NLS-1$
            manifest.setVendor(attributes.getValue("vendor")); //$NON-NLS-1$
            manifest.setDocsPath(attributes.getValue("docs-path")); //$NON-NLS-1$
            ((ModelPluginDescriptor) manifest).setClassName(
                    attributes.getValue("class")); //$NON-NLS-1$
        } else if ("plugin-fragment".equals(name)) { //$NON-NLS-1$
            if (manifest != null) {
                throw new SAXException("unexpected [" + name //$NON-NLS-1$
                        + "] element (manifest already defined)"); //$NON-NLS-1$
            }
            manifest = new ModelPluginFragment();
            manifest.setId(attributes.getValue("id")); //$NON-NLS-1$
            manifest.setVersion(attributes.getValue("version")); //$NON-NLS-1$
            manifest.setVendor(attributes.getValue("vendor")); //$NON-NLS-1$
            manifest.setDocsPath(attributes.getValue("docs-path")); //$NON-NLS-1$
            ((ModelPluginFragment) manifest).setPluginId(
                    attributes.getValue("plugin-id")); //$NON-NLS-1$
            if (attributes.getValue("plugin-version") != null) { //$NON-NLS-1$
                ((ModelPluginFragment) manifest).setPluginVersion(
                        attributes.getValue("plugin-version")); //$NON-NLS-1$
            }
            if (attributes.getValue("match") != null) { //$NON-NLS-1$
                ((ModelPluginFragment) manifest).setMatchingRule(
                        MatchingRule.fromCode(attributes.getValue("match"))); //$NON-NLS-1$
            } else {
                ((ModelPluginFragment) manifest).setMatchingRule(MatchingRule.COMPATIBLE);
            }
        } else if ("doc".equals(name)) { //$NON-NLS-1$
            documentation = new ModelDocumentation();
            documentation.setCaption(attributes.getValue("caption")); //$NON-NLS-1$
        } else if ("doc-ref".equals(name)) { //$NON-NLS-1$
            if (documentation == null) {
                if (entityResolver != null) {
                    throw new SAXException("[doc-ref] element found " //$NON-NLS-1$
                            + "outside of [doc] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[doc-ref] element found outside of [doc] element"); //$NON-NLS-1$
                return;
            }
            ModelDocumentationReference docRef =
                new ModelDocumentationReference();
            docRef.setPath(attributes.getValue("path")); //$NON-NLS-1$
            docRef.setCaption(attributes.getValue("caption")); //$NON-NLS-1$
            documentation.getReferences().add(docRef);
        } else if ("doc-text".equals(name)) { //$NON-NLS-1$
            if (documentation == null) {
                if (entityResolver != null) {
                    throw new SAXException("[doc-text] element found " //$NON-NLS-1$
                            + "outside of [doc] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[doc-text] element found outside of [doc] element"); //$NON-NLS-1$
                return;
            }
            docText = new StringBuilder();
        } else if ("attributes".equals(name)) { //$NON-NLS-1$
            attributeStack = new SimpleStack<ModelAttribute>();
        } else if ("attribute".equals(name)) { //$NON-NLS-1$
            if (attributeStack == null) {
                if (entityResolver != null) {
                    throw new SAXException("[attribute] element found " //$NON-NLS-1$
                            + "outside of [attributes] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[attribute] element found " //$NON-NLS-1$
                        + "outside of [attributes] element"); //$NON-NLS-1$
                return;
            }
            if (attribute != null) {
                attributeStack.push(attribute);
            }
            attribute = new ModelAttribute();
            attribute.setId(attributes.getValue("id")); //$NON-NLS-1$
            attribute.setValue(attributes.getValue("value")); //$NON-NLS-1$
        } else if ("requires".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("import".equals(name)) { //$NON-NLS-1$
            prerequisite = new ModelPrerequisite();
            if (attributes.getValue("id") != null) { //$NON-NLS-1$
                prerequisite.setId(attributes.getValue("id")); //$NON-NLS-1$
            }
            prerequisite.setPluginId(attributes.getValue("plugin-id")); //$NON-NLS-1$
            if (attributes.getValue("plugin-version") != null) { //$NON-NLS-1$
                prerequisite.setPluginVersion(
                        attributes.getValue("plugin-version")); //$NON-NLS-1$
            }
            if (attributes.getValue("match") != null) { //$NON-NLS-1$
                prerequisite.setMatchingRule(
                        MatchingRule.fromCode(attributes.getValue("match"))); //$NON-NLS-1$
            } else {
                prerequisite.setMatchingRule(MatchingRule.COMPATIBLE);
            }
            prerequisite.setExported(attributes.getValue("exported")); //$NON-NLS-1$
            prerequisite.setOptional(attributes.getValue("optional")); //$NON-NLS-1$
            prerequisite.setReverseLookup(
                    attributes.getValue("reverse-lookup")); //$NON-NLS-1$
        } else if ("runtime".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("library".equals(name)) { //$NON-NLS-1$
            library = new ModelLibrary();
            library.setId(attributes.getValue("id")); //$NON-NLS-1$
            library.setPath(attributes.getValue("path")); //$NON-NLS-1$
            library.setCodeLibrary(attributes.getValue("type")); //$NON-NLS-1$
            if (attributes.getValue("version") != null) { //$NON-NLS-1$
                library.setVersion(attributes.getValue("version")); //$NON-NLS-1$
            }
        } else if ("export".equals(name)) { //$NON-NLS-1$
            if (library == null) {
                if (entityResolver != null) {
                    throw new SAXException("[export] element found " //$NON-NLS-1$
                            + "outside of [library] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[export] element found outside of [library] element"); //$NON-NLS-1$
                return;
            }
            library.getExports().add(attributes.getValue("prefix")); //$NON-NLS-1$
        } else if ("extension-point".equals(name)) { //$NON-NLS-1$
            extensionPoint = new ModelExtensionPoint();
            extensionPoint.setId(attributes.getValue("id")); //$NON-NLS-1$
            extensionPoint.setParentPluginId(
                    attributes.getValue("parent-plugin-id")); //$NON-NLS-1$
            extensionPoint.setParentPointId(
                    attributes.getValue("parent-point-id")); //$NON-NLS-1$
            if (attributes.getValue("extension-multiplicity") != null) { //$NON-NLS-1$
                extensionPoint.setExtensionMultiplicity(
                        ExtensionMultiplicity.fromCode(
                                attributes.getValue("extension-multiplicity"))); //$NON-NLS-1$
            } else {
                extensionPoint.setExtensionMultiplicity(
                        ExtensionMultiplicity.ANY);
            }
            paramDefStack = new SimpleStack<ModelParameterDef>();
        } else if ("parameter-def".equals(name)) { //$NON-NLS-1$
            if (extensionPoint == null) {
                if (entityResolver != null) {
                    throw new SAXException("[parameter-def] element found " //$NON-NLS-1$
                            + "outside of [extension-point] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[parameter-def] element found " //$NON-NLS-1$
                        + "outside of [extension-point] element"); //$NON-NLS-1$
                return;
            }
            if (paramDef != null) {
                paramDefStack.push(paramDef);
            }
            paramDef = new ModelParameterDef();
            paramDef.setId(attributes.getValue("id")); //$NON-NLS-1$
            if (attributes.getValue("multiplicity") != null) { //$NON-NLS-1$
                paramDef.setMultiplicity(
                        ParameterMultiplicity.fromCode(
                                attributes.getValue("multiplicity"))); //$NON-NLS-1$
            } else {
                paramDef.setMultiplicity(ParameterMultiplicity.ONE);
            }
            if (attributes.getValue("type") != null) { //$NON-NLS-1$
                paramDef.setType(
                        ParameterType.fromCode(attributes.getValue("type"))); //$NON-NLS-1$
            } else {
                paramDef.setType(ParameterType.STRING);
            }
            paramDef.setCustomData(attributes.getValue("custom-data")); //$NON-NLS-1$
            paramDef.setDefaultValue(attributes.getValue("default-value")); //$NON-NLS-1$
        } else if ("extension".equals(name)) { //$NON-NLS-1$
            extension = new ModelExtension();
            extension.setId(attributes.getValue("id")); //$NON-NLS-1$
            extension.setPluginId(attributes.getValue("plugin-id")); //$NON-NLS-1$
            extension.setPointId(attributes.getValue("point-id")); //$NON-NLS-1$
            paramStack = new SimpleStack<ModelParameter>();
        } else if ("parameter".equals(name)) { //$NON-NLS-1$
            if (extension == null) {
                if (entityResolver != null) {
                    throw new SAXException("[parameter] element found " //$NON-NLS-1$
                            + "outside of [extension] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[parameter] element found " //$NON-NLS-1$
                        + "outside of [extension] element"); //$NON-NLS-1$
                return;
            }
            if (param != null) {
                paramStack.push(param);
            }
            param = new ModelParameter();
            param.setId(attributes.getValue("id")); //$NON-NLS-1$
            param.setValue(attributes.getValue("value")); //$NON-NLS-1$
        } else if ("value".equals(name)) { //$NON-NLS-1$
            if (param == null) {
                if (entityResolver != null) {
                    throw new SAXException("[value] element found " //$NON-NLS-1$
                            + "outside of [parameter] element"); //$NON-NLS-1$
                }
                // ignore this element
                log.warn("[value] element found " //$NON-NLS-1$
                        + "outside of [parameter] element"); //$NON-NLS-1$
                return;
            }
            paramValue = new StringBuilder();
        } else {
            if (entityResolver != null) {
                throw new SAXException("unexpected manifest element - [" //$NON-NLS-1$
                        + uri + "]/[" + localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            }
            // ignore this element
            log.warn("unexpected manifest element - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
                    + localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }

    /**
     * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(final String uri, final String localName,
            final String qName) {
        if (log.isDebugEnabled()) {
            log.debug("endElement - [" + uri + "]/[" + localName //$NON-NLS-1$ //$NON-NLS-2$
                    + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        String name = qName;
        if ("plugin".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("plugin-fragment".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("doc".equals(name)) { //$NON-NLS-1$
            if (param != null) {
                param.setDocumentation(documentation);
            } else if (extension != null) {
                extension.setDocumentation(documentation);
            } else if (paramDef != null) {
                paramDef.setDocumentation(documentation);
            } else if (extensionPoint != null) {
                extensionPoint.setDocumentation(documentation);
            } else if (library != null) {
                library.setDocumentation(documentation);
            } else if (prerequisite != null) {
                prerequisite.setDocumentation(documentation);
            } else if (attribute != null) {
                attribute.setDocumentation(documentation);
            } else {
                manifest.setDocumentation(documentation);
            }
            documentation = null;
        } else if ("doc-ref".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("doc-text".equals(name)) { //$NON-NLS-1$
            documentation.setText(docText.toString());
            docText = null;
        } else if ("attributes".equals(name)) { //$NON-NLS-1$
            attributeStack = null;
        } else if ("attribute".equals(name)) { //$NON-NLS-1$
            if (attributeStack.size() == 0) {
                manifest.getAttributes().add(attribute);
                attribute = null;
            } else {
                ModelAttribute temp = attribute;
                attribute = attributeStack.pop();
                attribute.getAttributes().add(temp);
                temp = null;
            }
        } else if ("requires".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("import".equals(name)) { //$NON-NLS-1$
            manifest.getPrerequisites().add(prerequisite);
            prerequisite = null;
        } else if ("runtime".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("library".equals(name)) { //$NON-NLS-1$
            manifest.getLibraries().add(library);
            library = null;
        } else if ("export".equals(name)) { //$NON-NLS-1$
            // no-op
        } else if ("extension-point".equals(name)) { //$NON-NLS-1$
            manifest.getExtensionPoints().add(extensionPoint);
            extensionPoint = null;
            paramDefStack = null;
        } else if ("parameter-def".equals(name)) { //$NON-NLS-1$
            if (paramDefStack.size() == 0) {
                extensionPoint.getParamDefs().add(paramDef);
                paramDef = null;
            } else {
                ModelParameterDef temp = paramDef;
                paramDef = paramDefStack.pop();
                paramDef.getParamDefs().add(temp);
                temp = null;
            }
        } else if ("extension".equals(name)) { //$NON-NLS-1$
            manifest.getExtensions().add(extension);
            extension = null;
            paramStack = null;
        } else if ("parameter".equals(name)) { //$NON-NLS-1$
            if (paramStack.size() == 0) {
                extension.getParams().add(param);
                param = null;
            } else {
                ModelParameter temp = param;
                param = paramStack.pop();
                param.getParams().add(temp);
                temp = null;
            }
        } else if ("value".equals(name)) { //$NON-NLS-1$
            param.setValue(paramValue.toString());
            paramValue = null;
        } else {
            // ignore any other element
            log.warn("ignoring manifest element - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
                    + localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }

    /**
     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
     */
    @Override
    public void characters(final char[] ch, final int start, final int length)
            throws SAXException {
        if (docText != null) {
            docText.append(ch, start, length);
        } else if (paramValue != null) {
            paramValue.append(ch, start, length);
        } else {
            if (entityResolver != null) {
                throw new SAXException("unexpected character data"); //$NON-NLS-1$
            }
            // ignore these characters
            log.warn("ignoring character data - [" //$NON-NLS-1$
                    + new String(ch, start, length) + "]"); //$NON-NLS-1$
        }
    }
    
    ModelPluginManifest getResult() {
        return manifest;
    }
}