KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > model > PluginParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.internal.model;
13
14 import java.util.*;
15 import javax.xml.parsers.SAXParserFactory JavaDoc;
16 import org.eclipse.core.internal.runtime.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.core.runtime.model.*;
19 import org.eclipse.osgi.util.NLS;
20 import org.osgi.framework.ServiceReference;
21 import org.xml.sax.*;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23
24 public class PluginParser extends DefaultHandler JavaDoc implements IModel {
25
26     // concrete object factory
27
Factory JavaDoc factory;
28
29     // File name for this plugin or fragment
30
// This to help with error reporting
31
String JavaDoc locationName = null;
32
33     // Current State Information
34
Stack stateStack = new Stack();
35
36     // Current object stack (used to hold the current object we are
37
// populating in this plugin descriptor
38
Stack objectStack = new Stack();
39
40     Locator locator = null;
41
42     // Valid States
43
private static final int IGNORED_ELEMENT_STATE = 0;
44     private static final int INITIAL_STATE = 1;
45     private static final int PLUGIN_STATE = 2;
46     private static final int PLUGIN_RUNTIME_STATE = 3;
47     private static final int PLUGIN_REQUIRES_STATE = 4;
48     private static final int PLUGIN_EXTENSION_POINT_STATE = 5;
49     private static final int PLUGIN_EXTENSION_STATE = 6;
50     private static final int RUNTIME_LIBRARY_STATE = 7;
51     private static final int LIBRARY_EXPORT_STATE = 8;
52 // private static final int LIBRARY_PACKAGES_STATE = 12;
53
private static final int PLUGIN_REQUIRES_IMPORT_STATE = 9;
54     private static final int CONFIGURATION_ELEMENT_STATE = 10;
55     private static final int FRAGMENT_STATE = 11;
56
57     // Keep a group of vectors as a temporary scratch space. These
58
// vectors will be used to populate arrays in the plugin descriptor
59
// once processing of the XML file is complete.
60
private static final int EXTENSION_POINT_INDEX = 0;
61     private static final int EXTENSION_INDEX = 1;
62     private static final int LAST_INDEX = 1;
63     private Vector scratchVectors[] = new Vector[LAST_INDEX + 1];
64
65     private ServiceReference parserReference;
66
67     public PluginParser(Factory JavaDoc factory) {
68         super();
69         this.factory = factory;
70     }
71
72     /**
73      * Receive a Locator object for document events.
74      *
75      * <p>By default, do nothing. Application writers may override this
76      * method in a subclass if they wish to store the locator for use
77      * with other document events.</p>
78      *
79      * @param locator A locator for all SAX document events.
80      * @see org.xml.sax.ContentHandler#setDocumentLocator
81      * @see org.xml.sax.Locator
82      */

83     public void setDocumentLocator(Locator locator) {
84         this.locator = locator;
85     }
86
87     public void characters(char[] ch, int start, int length) {
88         int state = ((Integer JavaDoc) stateStack.peek()).intValue();
89         if (state != CONFIGURATION_ELEMENT_STATE)
90             return;
91         if (state == CONFIGURATION_ELEMENT_STATE) {
92             // Accept character data within an element, is when it is
93
// part of a configuration element (i.e. an element within an EXTENSION element
94
ConfigurationElementModel currentConfigElement = (ConfigurationElementModel) objectStack.peek();
95             String JavaDoc value = new String JavaDoc(ch, start, length);
96             String JavaDoc oldValue = currentConfigElement.getValueAsIs();
97             if (oldValue == null) {
98                 if (value.trim().length() != 0)
99                     currentConfigElement.setValue(value);
100             } else {
101                 currentConfigElement.setValue(oldValue + value);
102             }
103         }
104     }
105
106     public void endDocument() {
107     }
108
109     public void endElement(String JavaDoc uri, String JavaDoc elementName, String JavaDoc qName) {
110         switch (((Integer JavaDoc) stateStack.peek()).intValue()) {
111             case IGNORED_ELEMENT_STATE :
112                 stateStack.pop();
113                 break;
114             case INITIAL_STATE :
115                 // shouldn't get here
116
internalError(NLS.bind(Messages.parse_internalStack, elementName));
117                 break;
118             case PLUGIN_STATE :
119             case FRAGMENT_STATE :
120                 if (elementName.equals(PLUGIN) || elementName.equals(FRAGMENT)) {
121                     stateStack.pop();
122                     PluginModel root = (PluginModel) objectStack.peek();
123
124                     // Put the extension points into this plugin
125
Vector extPointVector = scratchVectors[EXTENSION_POINT_INDEX];
126                     if (extPointVector.size() > 0) {
127                         root.setDeclaredExtensionPoints((ExtensionPointModel[]) extPointVector.toArray(new ExtensionPointModel[extPointVector.size()]));
128                         scratchVectors[EXTENSION_POINT_INDEX].removeAllElements();
129                     }
130
131                     // Put the extensions into this plugin too
132
Vector extVector = scratchVectors[EXTENSION_INDEX];
133                     if (extVector.size() > 0) {
134                         root.setDeclaredExtensions((ExtensionModel[]) extVector.toArray(new ExtensionModel[extVector.size()]));
135                         scratchVectors[EXTENSION_INDEX].removeAllElements();
136                     }
137                 }
138                 break;
139             case PLUGIN_RUNTIME_STATE :
140                 if (elementName.equals(RUNTIME)) {
141                     stateStack.pop();
142                     // take the vector of library entries and put them into the plugin
143
// descriptor
144
Vector libVector = (Vector) objectStack.pop();
145                     if (libVector.size() > 0) {
146                         PluginModel model = (PluginModel) objectStack.peek();
147                         model.setRuntime((LibraryModel[]) libVector.toArray(new LibraryModel[libVector.size()]));
148                     }
149                 }
150                 break;
151             case PLUGIN_REQUIRES_STATE :
152                 if (elementName.equals(PLUGIN_REQUIRES)) {
153                     stateStack.pop();
154                     // take the vector of prerequisites and put them into the plugin
155
// descriptor
156
Vector importVector = (Vector) objectStack.pop();
157                     if (importVector.size() > 0) {
158                         PluginModel parentDescriptor = (PluginModel) objectStack.peek();
159                         parentDescriptor.setRequires((PluginPrerequisiteModel[]) importVector.toArray(new PluginPrerequisiteModel[importVector.size()]));
160                     }
161                 }
162                 break;
163             case PLUGIN_EXTENSION_POINT_STATE :
164                 if (elementName.equals(EXTENSION_POINT)) {
165                     stateStack.pop();
166                 }
167                 break;
168             case PLUGIN_EXTENSION_STATE :
169                 if (elementName.equals(EXTENSION)) {
170                     stateStack.pop();
171                     // Finish up extension object
172
ExtensionModel currentExtension = (ExtensionModel) objectStack.pop();
173                     PluginModel parent = (PluginModel) objectStack.peek();
174                     currentExtension.setParent(parent);
175                     scratchVectors[EXTENSION_INDEX].addElement(currentExtension);
176                 }
177                 break;
178             case RUNTIME_LIBRARY_STATE :
179                 if (elementName.equals(LIBRARY)) {
180                     LibraryModel curLibrary = (LibraryModel) objectStack.pop();
181                     // Clean up the exports for this library entry
182
Vector exportsVector = (Vector) objectStack.pop();
183                     if (exportsVector.size() > 0) {
184                         curLibrary.setExports((String JavaDoc[]) exportsVector.toArray(new String JavaDoc[exportsVector.size()]));
185                     }
186
187                     // Add this library element to the vector "runtime" on the stack
188
Vector libraryVector = (Vector) objectStack.peek();
189                     libraryVector.addElement(curLibrary);
190                     stateStack.pop();
191                 }
192                 break;
193             case LIBRARY_EXPORT_STATE :
194                 if (elementName.equals(LIBRARY_EXPORT)) {
195                     stateStack.pop();
196                 }
197                 break;
198             case PLUGIN_REQUIRES_IMPORT_STATE :
199                 if (elementName.equals(PLUGIN_REQUIRES_IMPORT)) {
200                     stateStack.pop();
201                 }
202                 break;
203             case CONFIGURATION_ELEMENT_STATE :
204                 // We don't care what the element name was
205
stateStack.pop();
206                 // Now finish up the configuration element object
207
ConfigurationElementModel currentConfigElement = (ConfigurationElementModel) objectStack.pop();
208
209                 String JavaDoc value = currentConfigElement.getValueAsIs();
210                 if (value != null) {
211                     currentConfigElement.setValue(value.trim());
212                 }
213
214                 Object JavaDoc parent = objectStack.peek();
215                 currentConfigElement.setParent(parent);
216                 if (((Integer JavaDoc) stateStack.peek()).intValue() == PLUGIN_EXTENSION_STATE) {
217                     // Want to add this configuration element to the subelements of an extension
218
ConfigurationElementModel[] oldValues = (ConfigurationElementModel[]) ((ExtensionModel) parent).getSubElements();
219                     int size = (oldValues == null) ? 0 : oldValues.length;
220                     ConfigurationElementModel[] newValues = new ConfigurationElementModel[size + 1];
221                     for (int i = 0; i < size; i++) {
222                         newValues[i] = oldValues[i];
223                     }
224                     newValues[size] = currentConfigElement;
225                     ((ExtensionModel) parent).setSubElements(newValues);
226                 } else {
227                     ConfigurationElementModel[] oldValues = (ConfigurationElementModel[]) ((ConfigurationElementModel) parent).getSubElements();
228                     int size = (oldValues == null) ? 0 : oldValues.length;
229                     ConfigurationElementModel[] newValues = new ConfigurationElementModel[size + 1];
230                     for (int i = 0; i < size; i++) {
231                         newValues[i] = oldValues[i];
232                     }
233                     newValues[size] = currentConfigElement;
234                     ((ConfigurationElementModel) parent).setSubElements(newValues);
235                 }
236                 break;
237         }
238     }
239
240     public void error(SAXParseException ex) {
241         logStatus(ex);
242     }
243
244     public void fatalError(SAXParseException ex) throws SAXException {
245         logStatus(ex);
246         throw ex;
247     }
248
249     public void handleExtensionPointState(String JavaDoc elementName, Attributes attributes) {
250
251         // We ignore all elements under extension points (if there are any)
252
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
253         internalError(NLS.bind(Messages.parse_unknownElement, EXTENSION_POINT, elementName));
254     }
255
256     public void handleExtensionState(String JavaDoc elementName, Attributes attributes) {
257
258         // You need to change the state here even though we will be executing the same
259
// code for ExtensionState and ConfigurationElementState. We ignore the name
260
// of the element for ConfigurationElements. When we are wrapping up, we will
261
// want to add each configuration element object to the subElements vector of
262
// its parent configuration element object. However, the first configuration
263
// element object we created (the last one we pop off the stack) will need to
264
// be added to a vector in the extension object called _configuration.
265
stateStack.push(new Integer JavaDoc(CONFIGURATION_ELEMENT_STATE));
266
267         // create a new Configuration Element and push it onto the object stack
268
ConfigurationElementModel currentConfigurationElement = factory.createConfigurationElement();
269         objectStack.push(currentConfigurationElement);
270         currentConfigurationElement.setName(elementName);
271
272         // Processing the attributes of a configuration element involves creating
273
// a new configuration property for each attribute and populating the configuration
274
// property with the name/value pair of the attribute. Note there will be one
275
// configuration property for each attribute
276
parseConfigurationElementAttributes(attributes);
277     }
278
279     public void handleInitialState(String JavaDoc elementName, Attributes attributes) {
280         if (elementName.equals(PLUGIN)) {
281             stateStack.push(new Integer JavaDoc(PLUGIN_STATE));
282             parsePluginAttributes(attributes);
283         } else if (elementName.equals(FRAGMENT)) {
284             stateStack.push(new Integer JavaDoc(FRAGMENT_STATE));
285             parseFragmentAttributes(attributes);
286         } else {
287             stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
288             internalError(NLS.bind(Messages.parse_unknownTopElement, elementName));
289         }
290     }
291
292     public void handleLibraryExportState(String JavaDoc elementName, Attributes attributes) {
293
294         // All elements ignored.
295
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
296         internalError(NLS.bind(Messages.parse_unknownElement, LIBRARY_EXPORT, elementName));
297     }
298
299     public void handleLibraryState(String JavaDoc elementName, Attributes attributes) {
300         if (elementName.equals(LIBRARY_EXPORT)) {
301             // Change State
302
stateStack.push(new Integer JavaDoc(LIBRARY_EXPORT_STATE));
303             // The top element on the stack much be a library element
304
LibraryModel currentLib = (LibraryModel) objectStack.peek();
305
306             if (attributes == null)
307                 return;
308
309             String JavaDoc maskValue = null;
310
311             // Process Attributes
312
int len = attributes.getLength();
313             for (int i = 0; i < len; i++) {
314                 String JavaDoc attrName = attributes.getLocalName(i);
315                 String JavaDoc attrValue = attributes.getValue(i).trim();
316
317                 if (attrName.equals(LIBRARY_EXPORT_MASK))
318                     maskValue = attrValue;
319                 else
320                     internalError(NLS.bind(Messages.parse_unknownAttribute, LIBRARY, attrName));
321             }
322
323             // set up mask tables
324
// pop off the library - already in currentLib
325
objectStack.pop();
326             Vector exportMask = (Vector) objectStack.peek();
327             // push library back on
328
objectStack.push(currentLib);
329             if ((maskValue != null) && (!exportMask.contains(maskValue)))
330                 exportMask.addElement(maskValue);
331             return;
332         }
333
334         if (elementName.equals(LIBRARY_PACKAGES)) {
335             LibraryModel currentLib = (LibraryModel) objectStack.peek();
336             if (attributes == null)
337                 return;
338             for (int i = 0; i < attributes.getLength(); i++) {
339                 if (LIBRARY_PACKAGES_PREFIXES.equals(attributes.getLocalName(i))) {
340                     String JavaDoc line = attributes.getValue(i);
341                     String JavaDoc[] prefixes = getArrayFromList(line);
342                     currentLib.setPackagePrefixes(prefixes);
343                 }
344             }
345             return;
346         }
347
348         // Any other element is invalid
349
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
350         internalError(NLS.bind(Messages.parse_unknownElement, LIBRARY, elementName));
351         return;
352     }
353
354     /**
355      * convert a list of comma-separated tokens into an array
356      */

357     protected static String JavaDoc[] getArrayFromList(String JavaDoc line) {
358         if (line == null || line.trim().length() == 0)
359             return null;
360         Vector list = new Vector();
361         StringTokenizer tokens = new StringTokenizer(line, ","); //$NON-NLS-1$
362
while (tokens.hasMoreTokens()) {
363             String JavaDoc token = tokens.nextToken().trim();
364             if (token.length() != 0)
365                 list.addElement(token);
366         }
367         return list.isEmpty() ? null : (String JavaDoc[]) list.toArray(new String JavaDoc[0]);
368     }
369
370     public void handlePluginState(String JavaDoc elementName, Attributes attributes) {
371
372         if (elementName.equals(RUNTIME)) {
373             // We should only have one Runtime element in a plugin or fragment
374
Object JavaDoc whatIsIt = objectStack.peek();
375             if (((whatIsIt instanceof PluginDescriptorModel) && (((PluginDescriptorModel) objectStack.peek()).getRuntime() != null)) || ((whatIsIt instanceof PluginFragmentModel) && (((PluginFragmentModel) objectStack.peek()).getRuntime() != null))) {
376                 // This is at least the 2nd Runtime element we have
377
// hit. Ignore it and give an error.
378
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
379                 return;
380             }
381             stateStack.push(new Integer JavaDoc(PLUGIN_RUNTIME_STATE));
382             // Push a new vector to hold all the library entries
383
objectStack.push(new Vector());
384             return;
385         }
386         if (elementName.equals(PLUGIN_REQUIRES)) {
387             stateStack.push(new Integer JavaDoc(PLUGIN_REQUIRES_STATE));
388             // Push a new vector to hold all the prerequisites
389
objectStack.push(new Vector());
390             parseRequiresAttributes(attributes);
391             return;
392         }
393         if (elementName.equals(EXTENSION_POINT)) {
394             stateStack.push(new Integer JavaDoc(PLUGIN_EXTENSION_POINT_STATE));
395             parseExtensionPointAttributes(attributes);
396             return;
397         }
398         if (elementName.equals(EXTENSION)) {
399             stateStack.push(new Integer JavaDoc(PLUGIN_EXTENSION_STATE));
400             parseExtensionAttributes(attributes);
401             return;
402         }
403
404         // If we get to this point, the element name is one we don't currently accept.
405
// Set the state to indicate that this element will be ignored
406
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
407         internalError(NLS.bind(Messages.parse_unknownElement, PLUGIN + " / " + FRAGMENT, elementName)); //$NON-NLS-1$
408
}
409
410     public void handleRequiresImportState(String JavaDoc elementName, Attributes attributes) {
411
412         // All elements ignored.
413
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
414         internalError(NLS.bind(Messages.parse_unknownElement, PLUGIN_REQUIRES_IMPORT, elementName));
415     }
416
417     public void handleRequiresState(String JavaDoc elementName, Attributes attributes) {
418
419         if (elementName.equals(PLUGIN_REQUIRES_IMPORT)) {
420             parsePluginRequiresImport(attributes);
421             return;
422         }
423         // If we get to this point, the element name is one we don't currently accept.
424
// Set the state to indicate that this element will be ignored
425
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
426         internalError(NLS.bind(Messages.parse_unknownElement, PLUGIN_REQUIRES, elementName));
427     }
428
429     public void handleRuntimeState(String JavaDoc elementName, Attributes attributes) {
430
431         if (elementName.equals(LIBRARY)) {
432             // Change State
433
stateStack.push(new Integer JavaDoc(RUNTIME_LIBRARY_STATE));
434             // Process library attributes
435
parseLibraryAttributes(attributes);
436             return;
437         }
438         // If we get to this point, the element name is one we don't currently accept.
439
// Set the state to indicate that this element will be ignored
440
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
441         internalError(NLS.bind(Messages.parse_unknownElement, RUNTIME, elementName));
442     }
443
444     public void ignoreableWhitespace(char[] ch, int start, int length) {
445     }
446
447     private void logStatus(SAXParseException ex) {
448         String JavaDoc name = ex.getSystemId();
449         if (name == null)
450             name = locationName;
451         if (name == null)
452             name = ""; //$NON-NLS-1$
453
else
454             name = name.substring(1 + name.lastIndexOf("/")); //$NON-NLS-1$
455

456         String JavaDoc msg;
457         if (name.equals("")) //$NON-NLS-1$
458
msg = NLS.bind(Messages.parse_error, ex.getMessage());
459         else
460             msg = NLS.bind(Messages.parse_errorNameLineColumn, (new String JavaDoc[] {name, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber()), ex.getMessage()}));
461         factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, msg, ex));
462     }
463
464     synchronized public PluginModel parsePlugin(InputSource in) throws Exception JavaDoc {
465         SAXParserFactory JavaDoc factory = acquireXMLParsing();
466         if (factory == null)
467             return null; // TODO we log an error
468

469         try {
470             locationName = in.getSystemId();
471             factory.setNamespaceAware(true);
472             factory.setNamespaceAware(true);
473             try {
474                 factory.setFeature("http://xml.org/sax/features/string-interning", true); //$NON-NLS-1$
475
} catch (SAXException se) {
476                 // ignore; we can still operate without string-interning
477
}
478             factory.setValidating(false);
479             factory.newSAXParser().parse(in, this);
480             return (PluginModel) objectStack.pop();
481         } finally {
482             releaseXMLParsing();
483         }
484     }
485
486     private SAXParserFactory JavaDoc acquireXMLParsing() {
487         parserReference = InternalPlatform.getDefault().getBundleContext().getServiceReference("javax.xml.parsers.SAXParserFactory"); //$NON-NLS-1$
488
if (parserReference == null)
489             return null;
490         return (SAXParserFactory JavaDoc) InternalPlatform.getDefault().getBundleContext().getService(parserReference);
491     }
492
493     private void releaseXMLParsing() {
494         if (parserReference != null)
495             InternalPlatform.getDefault().getBundleContext().ungetService(parserReference);
496     }
497
498     public void parseConfigurationElementAttributes(Attributes attributes) {
499
500         ConfigurationElementModel parentConfigurationElement = (ConfigurationElementModel) objectStack.peek();
501         parentConfigurationElement.setStartLine(locator.getLineNumber());
502
503         Vector propVector = null;
504
505         // process attributes
506
int len = (attributes != null) ? attributes.getLength() : 0;
507         if (len == 0)
508             return;
509         propVector = new Vector();
510
511         for (int i = 0; i < len; i++) {
512             String JavaDoc attrName = attributes.getLocalName(i);
513             String JavaDoc attrValue = attributes.getValue(i);
514
515             ConfigurationPropertyModel currentConfigurationProperty = factory.createConfigurationProperty();
516             currentConfigurationProperty.setName(attrName);
517             currentConfigurationProperty.setValue(attrValue);
518             propVector.addElement(currentConfigurationProperty);
519         }
520         parentConfigurationElement.setProperties((ConfigurationPropertyModel[]) propVector.toArray(new ConfigurationPropertyModel[propVector.size()]));
521         propVector = null;
522     }
523
524     public void parseExtensionAttributes(Attributes attributes) {
525
526         PluginModel parent = (PluginModel) objectStack.peek();
527         ExtensionModel currentExtension = factory.createExtension();
528         currentExtension.setStartLine(locator.getLineNumber());
529         objectStack.push(currentExtension);
530
531         // Process Attributes
532
int len = (attributes != null) ? attributes.getLength() : 0;
533         for (int i = 0; i < len; i++) {
534             String JavaDoc attrName = attributes.getLocalName(i);
535             String JavaDoc attrValue = attributes.getValue(i).trim();
536
537             if (attrName.equals(EXTENSION_NAME))
538                 currentExtension.setName(attrValue);
539             else if (attrName.equals(EXTENSION_ID))
540                 currentExtension.setId(attrValue);
541             else if (attrName.equals(EXTENSION_TARGET)) {
542                 // check if point is specified as a simple or qualified name
543
String JavaDoc targetName;
544                 if (attrValue.lastIndexOf('.') == -1) {
545                     String JavaDoc baseId = parent instanceof PluginDescriptorModel ? parent.getId() : ((PluginFragmentModel) parent).getPlugin();
546                     targetName = baseId + "." + attrValue; //$NON-NLS-1$
547
} else
548                     targetName = attrValue;
549                 currentExtension.setExtensionPoint(targetName);
550             } else
551                 internalError(NLS.bind(Messages.parse_unknownAttribute, EXTENSION, attrName));
552         }
553     }
554
555     public void parseExtensionPointAttributes(Attributes attributes) {
556
557         ExtensionPointModel currentExtPoint = factory.createExtensionPoint();
558         currentExtPoint.setStartLine(locator.getLineNumber());
559
560         // Process Attributes
561
int len = (attributes != null) ? attributes.getLength() : 0;
562         for (int i = 0; i < len; i++) {
563             String JavaDoc attrName = attributes.getLocalName(i);
564             String JavaDoc attrValue = attributes.getValue(i).trim();
565
566             if (attrName.equals(EXTENSION_POINT_NAME))
567                 currentExtPoint.setName(attrValue);
568             else if (attrName.equals(EXTENSION_POINT_ID))
569                 currentExtPoint.setId(attrValue);
570             else if (attrName.equals(EXTENSION_POINT_SCHEMA))
571                 currentExtPoint.setSchema(attrValue);
572             else
573                 internalError(NLS.bind(Messages.parse_unknownAttribute, EXTENSION_POINT, attrName));
574         }
575         // currentExtPoint contains a pointer to the parent plugin descriptor.
576
PluginModel root = (PluginModel) objectStack.peek();
577         currentExtPoint.setParent(root);
578
579         // Now populate the vector just below us on the objectStack with this extension point
580
scratchVectors[EXTENSION_POINT_INDEX].addElement(currentExtPoint);
581     }
582
583     public void parseFragmentAttributes(Attributes attributes) {
584         PluginFragmentModel current = factory.createPluginFragment();
585         current.setStartLine(locator.getLineNumber());
586         objectStack.push(current);
587
588         // process attributes
589
int len = attributes.getLength();
590         for (int i = 0; i < len; i++) {
591             String JavaDoc attrName = attributes.getLocalName(i);
592             String JavaDoc attrValue = attributes.getValue(i).trim();
593
594             if (attrName.equals(FRAGMENT_ID))
595                 current.setId(attrValue);
596             else if (attrName.equals(FRAGMENT_NAME))
597                 current.setName(attrValue);
598             else if (attrName.equals(FRAGMENT_VERSION))
599                 current.setVersion(attrValue);
600             else if (attrName.equals(FRAGMENT_PROVIDER))
601                 current.setProviderName(attrValue);
602             else if (attrName.equals(FRAGMENT_PLUGIN_ID))
603                 current.setPlugin(attrValue);
604             else if (attrName.equals(FRAGMENT_PLUGIN_VERSION))
605                 current.setPluginVersion(attrValue);
606             else if (attrName.equals(FRAGMENT_PLUGIN_MATCH)) {
607                 if (FRAGMENT_PLUGIN_MATCH_PERFECT.equals(attrValue))
608                     current.setMatch(PluginFragmentModel.FRAGMENT_MATCH_PERFECT);
609                 else if (FRAGMENT_PLUGIN_MATCH_EQUIVALENT.equals(attrValue))
610                     current.setMatch(PluginFragmentModel.FRAGMENT_MATCH_EQUIVALENT);
611                 else if (FRAGMENT_PLUGIN_MATCH_COMPATIBLE.equals(attrValue))
612                     current.setMatch(PluginFragmentModel.FRAGMENT_MATCH_COMPATIBLE);
613                 else if (FRAGMENT_PLUGIN_MATCH_GREATER_OR_EQUAL.equals(attrValue))
614                     current.setMatch(PluginFragmentModel.FRAGMENT_MATCH_GREATER_OR_EQUAL);
615                 else
616                     internalError(NLS.bind(Messages.parse_validMatch, attrValue));
617             } else
618                 internalError(NLS.bind(Messages.parse_unknownAttribute, FRAGMENT, attrName));
619         }
620     }
621
622     public void parseLibraryAttributes(Attributes attributes) {
623         // Push a vector to hold the export mask
624
objectStack.push(new Vector());
625         LibraryModel current = factory.createLibrary();
626         current.setStartLine(locator.getLineNumber());
627         objectStack.push(current);
628
629         // Now the objectStack should contain the following:
630
// plugin descriptor or fragment (bottom of the stack)
631
// vector to hold all the library entries
632
// vector to hold the export mask for this library entry
633
// this library entry (top of the stack)
634

635         // process attributes
636
int len = (attributes != null) ? attributes.getLength() : 0;
637         for (int i = 0; i < len; i++) {
638             String JavaDoc attrName = attributes.getLocalName(i);
639             String JavaDoc attrValue = attributes.getValue(i).trim();
640
641             if (attrName.equals(LIBRARY_NAME))
642                 current.setName(attrValue);
643             else if (attrName.equals(LIBRARY_TYPE)) {
644                 attrValue = attrValue.toLowerCase();
645                 if (attrValue.equals(LibraryModel.CODE) || attrValue.equals(LibraryModel.RESOURCE))
646                     current.setType(attrValue.toLowerCase());
647                 else
648                     internalError(NLS.bind(Messages.parse_unknownLibraryType, attrValue, current.getName()));
649             } else
650                 internalError(NLS.bind(Messages.parse_unknownAttribute, LIBRARY, attrName));
651         }
652     }
653
654     public void parsePluginAttributes(Attributes attributes) {
655
656         PluginDescriptorModel current = factory.createPluginDescriptor();
657         current.setStartLine(locator.getLineNumber());
658         objectStack.push(current);
659
660         // process attributes
661
int len = attributes.getLength();
662         for (int i = 0; i < len; i++) {
663             String JavaDoc attrName = attributes.getLocalName(i);
664             String JavaDoc attrValue = attributes.getValue(i).trim();
665
666             if (attrName.equals(PLUGIN_ID))
667                 current.setId(attrValue);
668             else if (attrName.equals(PLUGIN_NAME))
669                 current.setName(attrValue);
670             else if (attrName.equals(PLUGIN_VERSION))
671                 current.setVersion(attrValue);
672             else if (attrName.equals(PLUGIN_VENDOR) || (attrName.equals(PLUGIN_PROVIDER)))
673                 current.setProviderName(attrValue);
674             else if (attrName.equals(PLUGIN_CLASS))
675                 current.setPluginClass(attrValue);
676             else
677                 internalError(NLS.bind(Messages.parse_unknownAttribute, PLUGIN, attrName));
678         }
679     }
680
681     public void parsePluginRequiresImport(Attributes attributes) {
682         PluginPrerequisiteModel current = factory.createPluginPrerequisite();
683         current.setStartLine(locator.getLineNumber());
684
685         // process attributes
686
int len = (attributes != null) ? attributes.getLength() : 0;
687         for (int i = 0; i < len; i++) {
688             String JavaDoc attrName = attributes.getLocalName(i);
689             String JavaDoc attrValue = attributes.getValue(i).trim();
690
691             if (attrName.equals(PLUGIN_REQUIRES_PLUGIN))
692                 current.setPlugin(attrValue);
693             else if (attrName.equals(PLUGIN_REQUIRES_PLUGIN_VERSION))
694                 current.setVersion(attrValue);
695             else if (attrName.equals(PLUGIN_REQUIRES_OPTIONAL))
696                 current.setOptional(TRUE.equalsIgnoreCase(attrValue));
697             else if (attrName.equals(PLUGIN_REQUIRES_MATCH)) {
698                 if (PLUGIN_REQUIRES_MATCH_PERFECT.equals(attrValue))
699                     current.setMatchByte(PluginPrerequisiteModel.PREREQ_MATCH_PERFECT);
700                 else if ((PLUGIN_REQUIRES_MATCH_EQUIVALENT.equals(attrValue)) || (PLUGIN_REQUIRES_MATCH_EXACT.equals(attrValue)))
701                     current.setMatchByte(PluginPrerequisiteModel.PREREQ_MATCH_EQUIVALENT);
702                 else if (PLUGIN_REQUIRES_MATCH_COMPATIBLE.equals(attrValue))
703                     current.setMatchByte(PluginPrerequisiteModel.PREREQ_MATCH_COMPATIBLE);
704                 else if (PLUGIN_REQUIRES_MATCH_GREATER_OR_EQUAL.equals(attrValue))
705                     current.setMatchByte(PluginPrerequisiteModel.PREREQ_MATCH_GREATER_OR_EQUAL);
706                 else
707                     internalError(NLS.bind(Messages.parse_validMatch, attrValue));
708             } else if (attrName.equals(PLUGIN_REQUIRES_EXPORT)) {
709                 if (TRUE.equals(attrValue))
710                     current.setExport(true);
711                 else if (FALSE.equals(attrValue))
712                     current.setExport(false);
713                 else
714                     internalError(NLS.bind(Messages.parse_validExport, attrValue));
715             } else
716                 internalError(NLS.bind(Messages.parse_unknownAttribute, PLUGIN_REQUIRES_IMPORT, attrName));
717
718         }
719         // Populate the vector of prerequisites with this new element
720
((Vector) objectStack.peek()).addElement(current);
721     }
722
723     public void parseRequiresAttributes(Attributes attributes) {
724     }
725
726     static String JavaDoc replace(String JavaDoc s, String JavaDoc from, String JavaDoc to) {
727         String JavaDoc str = s;
728         int fromLen = from.length();
729         int toLen = to.length();
730         int ix = str.indexOf(from);
731         while (ix != -1) {
732             str = str.substring(0, ix) + to + str.substring(ix + fromLen);
733             ix = str.indexOf(from, ix + toLen);
734         }
735         return str;
736     }
737
738     public void startDocument() {
739         stateStack.push(new Integer JavaDoc(INITIAL_STATE));
740         for (int i = 0; i <= LAST_INDEX; i++) {
741             scratchVectors[i] = new Vector();
742         }
743     }
744
745     public void startElement(String JavaDoc uri, String JavaDoc elementName, String JavaDoc qName, Attributes attributes) {
746         switch (((Integer JavaDoc) stateStack.peek()).intValue()) {
747             case INITIAL_STATE :
748                 handleInitialState(elementName, attributes);
749                 break;
750             case FRAGMENT_STATE :
751                 handlePluginState(elementName, attributes);
752                 break;
753             case PLUGIN_STATE :
754                 handlePluginState(elementName, attributes);
755                 break;
756             case PLUGIN_RUNTIME_STATE :
757                 handleRuntimeState(elementName, attributes);
758                 break;
759             case PLUGIN_REQUIRES_STATE :
760                 handleRequiresState(elementName, attributes);
761                 break;
762             case PLUGIN_EXTENSION_POINT_STATE :
763                 handleExtensionPointState(elementName, attributes);
764                 break;
765             case PLUGIN_EXTENSION_STATE :
766             case CONFIGURATION_ELEMENT_STATE :
767                 handleExtensionState(elementName, attributes);
768                 break;
769             case RUNTIME_LIBRARY_STATE :
770                 handleLibraryState(elementName, attributes);
771                 break;
772             case LIBRARY_EXPORT_STATE :
773                 handleLibraryExportState(elementName, attributes);
774                 break;
775             case PLUGIN_REQUIRES_IMPORT_STATE :
776                 handleRequiresImportState(elementName, attributes);
777                 break;
778             default :
779                 stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
780                 internalError(NLS.bind(Messages.parse_unknownTopElement, elementName));
781         }
782     }
783
784     public void warning(SAXParseException ex) {
785         logStatus(ex);
786     }
787
788     private void internalError(String JavaDoc message) {
789         if (locationName != null)
790             factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, locationName + ": " + message, null)); //$NON-NLS-1$
791
else
792             factory.error(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.PARSE_PROBLEM, message, null));
793     }
794
795 }
796
Popular Tags