KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > xml > ManifestHandler


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.registry.xml;
20
21 import org.java.plugin.registry.ExtensionPoint;
22 import org.java.plugin.registry.PluginPrerequisite;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.EntityResolver JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * @version $Id: ManifestHandler.java,v 1.6 2006/08/19 17:37:21 ddimon Exp $
29  */

30 final class ManifestHandler extends BaseHandler {
31     private ModelPluginManifest manifest = null;
32     private ModelDocumentation documentation = null;
33     private ModelPrerequisite prerequisite;
34     private ModelLibrary library;
35     private ModelExtensionPoint extensionPoint;
36     private ModelExtension extension;
37     private StringBuffer JavaDoc docText = null;
38     private SimpleStack attributeStack = null;
39     private ModelAttribute attribute;
40     private SimpleStack paramDefStack = null;
41     private ModelParameterDef paramDef;
42     private SimpleStack paramStack = null;
43     private ModelParameter param;
44     private StringBuffer JavaDoc paramValue = null;
45
46     ManifestHandler(final EntityResolver JavaDoc anEntityResolver) {
47         super(anEntityResolver);
48     }
49     
50     /**
51      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
52      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
53      */

54     public void startElement(final String JavaDoc uri, final String JavaDoc localName,
55             final String JavaDoc qName, final Attributes JavaDoc attributes)
56             throws SAXException JavaDoc {
57         if (log.isDebugEnabled()) {
58             log.debug("startElement - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
59
+ localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
60
}
61         String JavaDoc name = qName;
62         if ("plugin".equals(name)) { //$NON-NLS-1$
63
if (manifest != null) {
64                 throw new SAXException JavaDoc("unexpected [" + name //$NON-NLS-1$
65
+ "] element (manifest already defined)"); //$NON-NLS-1$
66
}
67             manifest = new ModelPluginDescriptor();
68             manifest.setId(attributes.getValue("id")); //$NON-NLS-1$
69
manifest.setVersion(attributes.getValue("version")); //$NON-NLS-1$
70
manifest.setVendor(attributes.getValue("vendor")); //$NON-NLS-1$
71
manifest.setDocsPath(attributes.getValue("docs-path")); //$NON-NLS-1$
72
((ModelPluginDescriptor) manifest).setClassName(
73                     attributes.getValue("class")); //$NON-NLS-1$
74
} else if ("plugin-fragment".equals(name)) { //$NON-NLS-1$
75
if (manifest != null) {
76                 throw new SAXException JavaDoc("unexpected [" + name //$NON-NLS-1$
77
+ "] element (manifest already defined)"); //$NON-NLS-1$
78
}
79             manifest = new ModelPluginFragment();
80             manifest.setId(attributes.getValue("id")); //$NON-NLS-1$
81
manifest.setVersion(attributes.getValue("version")); //$NON-NLS-1$
82
manifest.setVendor(attributes.getValue("vendor")); //$NON-NLS-1$
83
manifest.setDocsPath(attributes.getValue("docs-path")); //$NON-NLS-1$
84
((ModelPluginFragment) manifest).setPluginId(
85                     attributes.getValue("plugin-id")); //$NON-NLS-1$
86
if (attributes.getValue("plugin-version") != null) { //$NON-NLS-1$
87
((ModelPluginFragment) manifest).setPluginVersion(
88                         attributes.getValue("plugin-version")); //$NON-NLS-1$
89
}
90             if (attributes.getValue("match") != null) { //$NON-NLS-1$
91
((ModelPluginFragment) manifest).setMatch(
92                         attributes.getValue("match")); //$NON-NLS-1$
93
} else {
94                 ((ModelPluginFragment) manifest).setMatch(PluginPrerequisite.MATCH_COMPATIBLE);
95             }
96         } else if ("doc".equals(name)) { //$NON-NLS-1$
97
documentation = new ModelDocumentation();
98             documentation.setCaption(attributes.getValue("caption")); //$NON-NLS-1$
99
} else if ("doc-ref".equals(name)) { //$NON-NLS-1$
100
if (documentation == null) {
101                 if (entityResolver != null) {
102                     throw new SAXException JavaDoc("[doc-ref] element found " //$NON-NLS-1$
103
+ "outside of [doc] element"); //$NON-NLS-1$
104
}
105                 // ignore this element
106
log.warn("[doc-ref] element found outside of [doc] element"); //$NON-NLS-1$
107
return;
108             }
109             ModelDocumentationReference docRef =
110                 new ModelDocumentationReference();
111             docRef.setPath(attributes.getValue("path")); //$NON-NLS-1$
112
docRef.setCaption(attributes.getValue("caption")); //$NON-NLS-1$
113
documentation.getReferences().add(docRef);
114         } else if ("doc-text".equals(name)) { //$NON-NLS-1$
115
if (documentation == null) {
116                 if (entityResolver != null) {
117                     throw new SAXException JavaDoc("[doc-text] element found " //$NON-NLS-1$
118
+ "outside of [doc] element"); //$NON-NLS-1$
119
}
120                 // ignore this element
121
log.warn("[doc-text] element found outside of [doc] element"); //$NON-NLS-1$
122
return;
123             }
124             docText = new StringBuffer JavaDoc();
125         } else if ("attributes".equals(name)) { //$NON-NLS-1$
126
attributeStack = new SimpleStack();
127         } else if ("attribute".equals(name)) { //$NON-NLS-1$
128
if (attributeStack == null) {
129                 if (entityResolver != null) {
130                     throw new SAXException JavaDoc("[attribute] element found " //$NON-NLS-1$
131
+ "outside of [attributes] element"); //$NON-NLS-1$
132
}
133                 // ignore this element
134
log.warn("[attribute] element found " //$NON-NLS-1$
135
+ "outside of [attributes] element"); //$NON-NLS-1$
136
return;
137             }
138             if (attribute != null) {
139                 attributeStack.push(attribute);
140             }
141             attribute = new ModelAttribute();
142             attribute.setId(attributes.getValue("id")); //$NON-NLS-1$
143
attribute.setValue(attributes.getValue("value")); //$NON-NLS-1$
144
} else if ("requires".equals(name)) { //$NON-NLS-1$
145
// no-op
146
} else if ("import".equals(name)) { //$NON-NLS-1$
147
prerequisite = new ModelPrerequisite();
148             if (attributes.getValue("id") != null) { //$NON-NLS-1$
149
prerequisite.setId(attributes.getValue("id")); //$NON-NLS-1$
150
}
151             prerequisite.setPluginId(attributes.getValue("plugin-id")); //$NON-NLS-1$
152
if (attributes.getValue("plugin-version") != null) { //$NON-NLS-1$
153
prerequisite.setPluginVersion(
154                         attributes.getValue("plugin-version")); //$NON-NLS-1$
155
}
156             if (attributes.getValue("match") != null) { //$NON-NLS-1$
157
prerequisite.setMatch(attributes.getValue("match")); //$NON-NLS-1$
158
} else {
159                 prerequisite.setMatch(PluginPrerequisite.MATCH_COMPATIBLE);
160             }
161             prerequisite.setExported(attributes.getValue("exported")); //$NON-NLS-1$
162
prerequisite.setOptional(attributes.getValue("optional")); //$NON-NLS-1$
163
prerequisite.setReverseLookup(
164                     attributes.getValue("reverse-lookup")); //$NON-NLS-1$
165
} else if ("runtime".equals(name)) { //$NON-NLS-1$
166
// no-op
167
} else if ("library".equals(name)) { //$NON-NLS-1$
168
library = new ModelLibrary();
169             library.setId(attributes.getValue("id")); //$NON-NLS-1$
170
library.setPath(attributes.getValue("path")); //$NON-NLS-1$
171
library.setCodeLibrary(attributes.getValue("type")); //$NON-NLS-1$
172
if (attributes.getValue("version") != null) { //$NON-NLS-1$
173
library.setVersion(attributes.getValue("version")); //$NON-NLS-1$
174
}
175         } else if ("export".equals(name)) { //$NON-NLS-1$
176
if (library == null) {
177                 if (entityResolver != null) {
178                     throw new SAXException JavaDoc("[export] element found " //$NON-NLS-1$
179
+ "outside of [library] element"); //$NON-NLS-1$
180
}
181                 // ignore this element
182
log.warn("[export] element found outside of [library] element"); //$NON-NLS-1$
183
return;
184             }
185             library.getExports().add(attributes.getValue("prefix")); //$NON-NLS-1$
186
} else if ("extension-point".equals(name)) { //$NON-NLS-1$
187
extensionPoint = new ModelExtensionPoint();
188             extensionPoint.setId(attributes.getValue("id")); //$NON-NLS-1$
189
extensionPoint.setParentPluginId(
190                     attributes.getValue("parent-plugin-id")); //$NON-NLS-1$
191
extensionPoint.setParentPointId(
192                     attributes.getValue("parent-point-id")); //$NON-NLS-1$
193
if (attributes.getValue("extension-multiplicity") != null) { //$NON-NLS-1$
194
extensionPoint.setExtensionMultiplicity(
195                         attributes.getValue("extension-multiplicity")); //$NON-NLS-1$
196
} else {
197                 extensionPoint.setExtensionMultiplicity(ExtensionPoint.EXT_MULT_ANY);
198             }
199             paramDefStack = new SimpleStack();
200         } else if ("parameter-def".equals(name)) { //$NON-NLS-1$
201
if (extensionPoint == null) {
202                 if (entityResolver != null) {
203                     throw new SAXException JavaDoc("[parameter-def] element found " //$NON-NLS-1$
204
+ "outside of [extension-point] element"); //$NON-NLS-1$
205
}
206                 // ignore this element
207
log.warn("[parameter-def] element found " //$NON-NLS-1$
208
+ "outside of [extension-point] element"); //$NON-NLS-1$
209
return;
210             }
211             if (paramDef != null) {
212                 paramDefStack.push(paramDef);
213             }
214             paramDef = new ModelParameterDef();
215             paramDef.setId(attributes.getValue("id")); //$NON-NLS-1$
216
if (attributes.getValue("multiplicity") != null) { //$NON-NLS-1$
217
paramDef.setMultiplicity(attributes.getValue("multiplicity")); //$NON-NLS-1$
218
} else {
219                 paramDef.setMultiplicity(ExtensionPoint.ParameterDefinition.MULT_ONE);
220             }
221             if (attributes.getValue("type") != null) { //$NON-NLS-1$
222
paramDef.setType(attributes.getValue("type")); //$NON-NLS-1$
223
} else {
224                 paramDef.setType(ExtensionPoint.ParameterDefinition.TYPE_STRING);
225             }
226             paramDef.setCustomData(attributes.getValue("custom-data")); //$NON-NLS-1$
227
paramDef.setDefaultValue(attributes.getValue("default-value")); //$NON-NLS-1$
228
} else if ("extension".equals(name)) { //$NON-NLS-1$
229
extension = new ModelExtension();
230             extension.setId(attributes.getValue("id")); //$NON-NLS-1$
231
extension.setPluginId(attributes.getValue("plugin-id")); //$NON-NLS-1$
232
extension.setPointId(attributes.getValue("point-id")); //$NON-NLS-1$
233
paramStack = new SimpleStack();
234         } else if ("parameter".equals(name)) { //$NON-NLS-1$
235
if (extension == null) {
236                 if (entityResolver != null) {
237                     throw new SAXException JavaDoc("[parameter] element found " //$NON-NLS-1$
238
+ "outside of [extension] element"); //$NON-NLS-1$
239
}
240                 // ignore this element
241
log.warn("[parameter] element found " //$NON-NLS-1$
242
+ "outside of [extension] element"); //$NON-NLS-1$
243
return;
244             }
245             if (param != null) {
246                 paramStack.push(param);
247             }
248             param = new ModelParameter();
249             param.setId(attributes.getValue("id")); //$NON-NLS-1$
250
param.setValue(attributes.getValue("value")); //$NON-NLS-1$
251
} else if ("value".equals(name)) { //$NON-NLS-1$
252
if (param == null) {
253                 if (entityResolver != null) {
254                     throw new SAXException JavaDoc("[value] element found " //$NON-NLS-1$
255
+ "outside of [parameter] element"); //$NON-NLS-1$
256
}
257                 // ignore this element
258
log.warn("[value] element found " //$NON-NLS-1$
259
+ "outside of [parameter] element"); //$NON-NLS-1$
260
return;
261             }
262             paramValue = new StringBuffer JavaDoc();
263         } else {
264             if (entityResolver != null) {
265                 throw new SAXException JavaDoc("unexpected manifest element - [" //$NON-NLS-1$
266
+ uri + "]/[" + localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
267
}
268             // ignore this element
269
log.warn("unexpected manifest element - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
270
+ localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
271
}
272     }
273
274     /**
275      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
276      * java.lang.String, java.lang.String)
277      */

278     public void endElement(final String JavaDoc uri, final String JavaDoc localName,
279             final String JavaDoc qName) {
280         if (log.isDebugEnabled()) {
281             log.debug("endElement - [" + uri + "]/[" + localName //$NON-NLS-1$ //$NON-NLS-2$
282
+ "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
283
}
284         String JavaDoc name = qName;
285         if ("plugin".equals(name)) { //$NON-NLS-1$
286
// no-op
287
} else if ("plugin-fragment".equals(name)) { //$NON-NLS-1$
288
// no-op
289
} else if ("doc".equals(name)) { //$NON-NLS-1$
290
if (param != null) {
291                 param.setDocumentation(documentation);
292             } else if (extension != null) {
293                 extension.setDocumentation(documentation);
294             } else if (paramDef != null) {
295                 paramDef.setDocumentation(documentation);
296             } else if (extensionPoint != null) {
297                 extensionPoint.setDocumentation(documentation);
298             } else if (library != null) {
299                 library.setDocumentation(documentation);
300             } else if (prerequisite != null) {
301                 prerequisite.setDocumentation(documentation);
302             } else if (attribute != null) {
303                 attribute.setDocumentation(documentation);
304             } else {
305                 manifest.setDocumentation(documentation);
306             }
307             documentation = null;
308         } else if ("doc-ref".equals(name)) { //$NON-NLS-1$
309
// no-op
310
} else if ("doc-text".equals(name)) { //$NON-NLS-1$
311
documentation.setText(docText.toString());
312             docText = null;
313         } else if ("attributes".equals(name)) { //$NON-NLS-1$
314
attributeStack = null;
315         } else if ("attribute".equals(name)) { //$NON-NLS-1$
316
if (attributeStack.size() == 0) {
317                 manifest.getAttributes().add(attribute);
318                 attribute = null;
319             } else {
320                 ModelAttribute temp = attribute;
321                 attribute = (ModelAttribute) attributeStack.pop();
322                 attribute.getAttributes().add(temp);
323                 temp = null;
324             }
325         } else if ("requires".equals(name)) { //$NON-NLS-1$
326
// no-op
327
} else if ("import".equals(name)) { //$NON-NLS-1$
328
manifest.getPrerequisites().add(prerequisite);
329             prerequisite = null;
330         } else if ("runtime".equals(name)) { //$NON-NLS-1$
331
// no-op
332
} else if ("library".equals(name)) { //$NON-NLS-1$
333
manifest.getLibraries().add(library);
334             library = null;
335         } else if ("export".equals(name)) { //$NON-NLS-1$
336
// no-op
337
} else if ("extension-point".equals(name)) { //$NON-NLS-1$
338
manifest.getExtensionPoints().add(extensionPoint);
339             extensionPoint = null;
340             paramDefStack = null;
341         } else if ("parameter-def".equals(name)) { //$NON-NLS-1$
342
if (paramDefStack.size() == 0) {
343                 extensionPoint.getParamDefs().add(paramDef);
344                 paramDef = null;
345             } else {
346                 ModelParameterDef temp = paramDef;
347                 paramDef = (ModelParameterDef) paramDefStack.pop();
348                 paramDef.getParamDefs().add(temp);
349                 temp = null;
350             }
351         } else if ("extension".equals(name)) { //$NON-NLS-1$
352
manifest.getExtensions().add(extension);
353             extension = null;
354             paramStack = null;
355         } else if ("parameter".equals(name)) { //$NON-NLS-1$
356
if (paramStack.size() == 0) {
357                 extension.getParams().add(param);
358                 param = null;
359             } else {
360                 ModelParameter temp = param;
361                 param = (ModelParameter) paramStack.pop();
362                 param.getParams().add(temp);
363                 temp = null;
364             }
365         } else if ("value".equals(name)) { //$NON-NLS-1$
366
param.setValue(paramValue.toString());
367             paramValue = null;
368         } else {
369             // ignore any other element
370
log.warn("ignoring manifest element - [" + uri + "]/[" //$NON-NLS-1$ //$NON-NLS-2$
371
+ localName + "]/[" + qName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
372
}
373     }
374
375     /**
376      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
377      */

378     public void characters(final char[] ch, final int start, final int length)
379             throws SAXException JavaDoc {
380         if (docText != null) {
381             docText.append(ch, start, length);
382         } else if (paramValue != null) {
383             paramValue.append(ch, start, length);
384         } else {
385             if (entityResolver != null) {
386                 throw new SAXException JavaDoc("unexpected character data"); //$NON-NLS-1$
387
}
388             // ignore these characters
389
log.warn("ignoring character data - [" //$NON-NLS-1$
390
+ new String JavaDoc(ch, start, length) + "]"); //$NON-NLS-1$
391
}
392     }
393     
394     ModelPluginManifest getResult() {
395         return manifest;
396     }
397 }
398
Popular Tags