KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > adaptor > PluginParser


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

11 package org.eclipse.core.runtime.adaptor;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.*;
15 import javax.xml.parsers.SAXParserFactory JavaDoc;
16 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
17 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.xml.sax.*;
21 import org.xml.sax.helpers.DefaultHandler JavaDoc;
22
23 /**
24  * Internal class.
25  */

26 public class PluginParser extends DefaultHandler JavaDoc implements IModel {
27     private static ServiceTracker xmlTracker = null;
28
29     private PluginInfo manifestInfo = new PluginInfo();
30     private BundleContext context;
31     private String JavaDoc target; // The targeted platform for the given manifest
32

33     public class PluginInfo implements IPluginInfo {
34         private String JavaDoc schemaVersion;
35         private String JavaDoc pluginId;
36         private String JavaDoc version;
37         private String JavaDoc vendor;
38
39         // an ordered list of library path names.
40
private ArrayList libraryPaths;
41         // TODO Should get rid of the libraries map and just have a
42
// list of library export statements instead. Library paths must
43
// preserve order.
44
private Map libraries; //represent the libraries and their export statement
45
private ArrayList requires;
46         private boolean requiresExpanded = false; //indicates if the requires have been processed.
47
private boolean compatibilityFound = false; //set to true is the requirement list contain compatilibity
48
private String JavaDoc pluginClass;
49         private String JavaDoc masterPluginId;
50         private String JavaDoc masterVersion;
51         private String JavaDoc masterMatch;
52         private Set filters;
53         private String JavaDoc pluginName;
54         private boolean singleton;
55         private boolean fragment;
56         private static final String JavaDoc TARGET21 = "2.1"; //$NON-NLS-1$
57

58         public boolean isFragment() {
59             return fragment;
60         }
61
62         public String JavaDoc toString() {
63             return "plugin-id: " + pluginId + " version: " + version + " libraries: " + libraries + " class:" + pluginClass + " master: " + masterPluginId + " master-version: " + masterVersion + " requires: " + requires + " singleton: " + singleton; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
64
}
65
66         public Map getLibraries() {
67             if (libraries == null)
68                 return new HashMap(0);
69             return libraries;
70         }
71
72         public ArrayList getRequires() {
73             if (!TARGET21.equals(target) && schemaVersion == null && !requiresExpanded) {
74                 requiresExpanded = true;
75                 if (requires == null) {
76                     requires = new ArrayList(1);
77                     requires.add(new Prerequisite(PluginConverterImpl.PI_RUNTIME, TARGET21, false, false, IModel.PLUGIN_REQUIRES_MATCH_GREATER_OR_EQUAL));
78                     requires.add(new Prerequisite(PluginConverterImpl.PI_RUNTIME_COMPATIBILITY, null, false, false, null));
79                 } else {
80                     //Add elements on the requirement list of ui and help.
81
for (int i = 0; i < requires.size(); i++) {
82                         Prerequisite analyzed = (Prerequisite) requires.get(i);
83                         if ("org.eclipse.ui".equals(analyzed.getName())) { //$NON-NLS-1$
84
requires.add(i + 1, new Prerequisite("org.eclipse.ui.workbench.texteditor", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
85
requires.add(i + 1, new Prerequisite("org.eclipse.jface.text", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
86
requires.add(i + 1, new Prerequisite("org.eclipse.ui.editors", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
87
requires.add(i + 1, new Prerequisite("org.eclipse.ui.views", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
88
requires.add(i + 1, new Prerequisite("org.eclipse.ui.ide", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
89
} else if ("org.eclipse.help".equals(analyzed.getName())) { //$NON-NLS-1$
90
requires.add(i + 1, new Prerequisite("org.eclipse.help.base", null, true, analyzed.isExported(), null)); //$NON-NLS-1$
91
} else if (PluginConverterImpl.PI_RUNTIME.equals(analyzed.getName()) && !compatibilityFound) {
92                             requires.add(i + 1, new Prerequisite(PluginConverterImpl.PI_RUNTIME_COMPATIBILITY, null, false, analyzed.isExported(), null));
93                         }
94                     }
95                     if (!requires.contains(new Prerequisite(PluginConverterImpl.PI_RUNTIME_COMPATIBILITY, null, false, false, null))) {
96                         requires.add(new Prerequisite(PluginConverterImpl.PI_RUNTIME_COMPATIBILITY, null, false, false, null));
97                     }
98                     //Remove any prereq on runtime and add a prereq on runtime 2.1
99
//This is used to recognize the version for which the given plugin was initially targeted.
100
Prerequisite runtimePrereq = new Prerequisite(PluginConverterImpl.PI_RUNTIME, null, false, false, null);
101                     requires.remove(runtimePrereq);
102                     requires.add(new Prerequisite(PluginConverterImpl.PI_RUNTIME, TARGET21, false, false, IModel.PLUGIN_REQUIRES_MATCH_GREATER_OR_EQUAL));
103                 }
104             }
105             if (requires == null)
106                 return requires = new ArrayList(0);
107
108             return requires;
109         }
110
111         public String JavaDoc getMasterId() {
112             return masterPluginId;
113         }
114
115         public String JavaDoc getMasterVersion() {
116             return masterVersion;
117         }
118
119         public String JavaDoc getMasterMatch() {
120             return masterMatch;
121         }
122
123         public String JavaDoc getPluginClass() {
124             return pluginClass;
125         }
126
127         public String JavaDoc getUniqueId() {
128             return pluginId;
129         }
130
131         public String JavaDoc getVersion() {
132             return version;
133         }
134
135         public Set getPackageFilters() {
136             return filters;
137         }
138
139         public String JavaDoc[] getLibrariesName() {
140             if (libraryPaths == null)
141                 return new String JavaDoc[0];
142             return (String JavaDoc[]) libraryPaths.toArray(new String JavaDoc[libraryPaths.size()]);
143         }
144
145         public String JavaDoc getPluginName() {
146             return pluginName;
147         }
148
149         public String JavaDoc getProviderName() {
150             return vendor;
151         }
152
153         public boolean isSingleton() {
154             return singleton;
155         }
156
157         public String JavaDoc getRoot() {
158             return isFragment() ? FRAGMENT : PLUGIN;
159         }
160
161         /*
162          * Provides some basic form of validation. Since plugin/fragment is the only mandatory
163          * attribute, it is the only one we cara about here.
164          */

165         public String JavaDoc validateForm() {
166             if (this.pluginId == null)
167                 return EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_MISSING_ATTRIBUTE", new String JavaDoc[] {getRoot(), PLUGIN_ID, getRoot()}); //$NON-NLS-1$
168
if (this.pluginName == null)
169                 return EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_MISSING_ATTRIBUTE", new String JavaDoc[] {getRoot(), PLUGIN_NAME, getRoot()}); //$NON-NLS-1$
170
if (this.version == null)
171                 return EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_MISSING_ATTRIBUTE", new String JavaDoc[] {getRoot(), PLUGIN_VERSION, getRoot()}); //$NON-NLS-1$
172
if (isFragment() && this.masterPluginId == null)
173                 return EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_MISSING_ATTRIBUTE", new String JavaDoc[] {getRoot(), FRAGMENT_PLUGIN_ID, getRoot()}); //$NON-NLS-1$
174
if (isFragment() && this.masterVersion == null)
175                 return EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_MISSING_ATTRIBUTE", new String JavaDoc[] {getRoot(), FRAGMENT_PLUGIN_VERSION, getRoot()}); //$NON-NLS-1$
176
return null;
177         }
178     }
179
180     // Current State Information
181
Stack stateStack = new Stack();
182
183     // Current object stack (used to hold the current object we are populating in this plugin info
184
Stack objectStack = new Stack();
185     Locator locator = null;
186
187     // Valid States
188
private static final int IGNORED_ELEMENT_STATE = 0;
189     private static final int INITIAL_STATE = 1;
190     private static final int PLUGIN_STATE = 2;
191     private static final int PLUGIN_RUNTIME_STATE = 3;
192     private static final int PLUGIN_REQUIRES_STATE = 4;
193     private static final int PLUGIN_EXTENSION_POINT_STATE = 5;
194     private static final int PLUGIN_EXTENSION_STATE = 6;
195     private static final int RUNTIME_LIBRARY_STATE = 7;
196     private static final int LIBRARY_EXPORT_STATE = 8;
197     private static final int PLUGIN_REQUIRES_IMPORT_STATE = 9;
198     private static final int FRAGMENT_STATE = 11;
199
200     public PluginParser(BundleContext context, String JavaDoc target) {
201         super();
202         this.context = context;
203         this.target = target;
204     }
205
206     /**
207      * Receive a Locator object for document events.
208      *
209      * <p>
210      * By default, do nothing. Application writers may override this method in
211      * a subclass if they wish to store the locator for use with other document
212      * events.
213      * </p>
214      *
215      * @param locator A locator for all SAX document events.
216      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
217      * @see org.xml.sax.Locator
218      */

219     public void setDocumentLocator(Locator locator) {
220         this.locator = locator;
221     }
222
223     public void endDocument() {
224     }
225
226     public void endElement(String JavaDoc uri, String JavaDoc elementName, String JavaDoc qName) {
227         switch (((Integer JavaDoc) stateStack.peek()).intValue()) {
228             case IGNORED_ELEMENT_STATE :
229                 stateStack.pop();
230                 break;
231             case INITIAL_STATE :
232                 // shouldn't get here
233
// internalError(Policy.bind("parse.internalStack", elementName)); //$NON-NLS-1$
234
break;
235             case PLUGIN_STATE :
236             case FRAGMENT_STATE :
237                 break;
238             case PLUGIN_RUNTIME_STATE :
239                 if (elementName.equals(RUNTIME)) {
240                     stateStack.pop();
241                 }
242                 break;
243             case PLUGIN_REQUIRES_STATE :
244                 if (elementName.equals(PLUGIN_REQUIRES)) {
245                     stateStack.pop();
246                     objectStack.pop();
247                 }
248                 break;
249             case PLUGIN_EXTENSION_POINT_STATE :
250                 if (elementName.equals(EXTENSION_POINT)) {
251                     stateStack.pop();
252                 }
253                 break;
254             case PLUGIN_EXTENSION_STATE :
255                 if (elementName.equals(EXTENSION)) {
256                     stateStack.pop();
257                 }
258                 break;
259             case RUNTIME_LIBRARY_STATE :
260                 if (elementName.equals(LIBRARY)) {
261                     String JavaDoc curLibrary = (String JavaDoc) objectStack.pop();
262                     if (!curLibrary.trim().equals("")) { //$NON-NLS-1$
263
Vector exportsVector = (Vector) objectStack.pop();
264                         if (manifestInfo.libraries == null) {
265                             manifestInfo.libraries = new HashMap(3);
266                             manifestInfo.libraryPaths = new ArrayList(3);
267                         }
268                         manifestInfo.libraries.put(curLibrary, exportsVector);
269                         manifestInfo.libraryPaths.add(curLibrary.replace('\\', '/'));
270                     }
271                     stateStack.pop();
272                 }
273                 break;
274             case LIBRARY_EXPORT_STATE :
275                 if (elementName.equals(LIBRARY_EXPORT)) {
276                     stateStack.pop();
277                 }
278                 break;
279             case PLUGIN_REQUIRES_IMPORT_STATE :
280                 if (elementName.equals(PLUGIN_REQUIRES_IMPORT)) {
281                     stateStack.pop();
282                 }
283                 break;
284         }
285     }
286
287     public void error(SAXParseException ex) {
288         logStatus(ex);
289     }
290
291     public void fatalError(SAXParseException ex) throws SAXException {
292         logStatus(ex);
293         throw ex;
294     }
295
296     public void handleExtensionPointState(String JavaDoc elementName, Attributes attributes) {
297         // nothing to do for extension-points' children
298
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
299     }
300
301     public void handleExtensionState(String JavaDoc elementName, Attributes attributes) {
302         // nothing to do for extensions' children
303
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
304     }
305
306     public void handleInitialState(String JavaDoc elementName, Attributes attributes) {
307         if (elementName.equals(PLUGIN)) {
308             stateStack.push(new Integer JavaDoc(PLUGIN_STATE));
309             parsePluginAttributes(attributes);
310         } else if (elementName.equals(FRAGMENT)) {
311             manifestInfo.fragment = true;
312             stateStack.push(new Integer JavaDoc(FRAGMENT_STATE));
313             parseFragmentAttributes(attributes);
314         } else {
315             stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
316             internalError(elementName);
317         }
318     }
319
320     public void handleLibraryExportState(String JavaDoc elementName, Attributes attributes) {
321         // All elements ignored.
322
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
323     }
324
325     public void handleLibraryState(String JavaDoc elementName, Attributes attributes) {
326         if (elementName.equals(LIBRARY_EXPORT)) {
327             // Change State
328
stateStack.push(new Integer JavaDoc(LIBRARY_EXPORT_STATE));
329             // The top element on the stack much be a library element
330
String JavaDoc currentLib = (String JavaDoc) objectStack.peek();
331             if (attributes == null)
332                 return;
333             String JavaDoc maskValue = attributes.getValue("", LIBRARY_EXPORT_MASK); //$NON-NLS-1$
334
// pop off the library - already in currentLib
335
objectStack.pop();
336             Vector exportMask = (Vector) objectStack.peek();
337             // push library back on
338
objectStack.push(currentLib);
339             //Split the export upfront
340
if (maskValue != null) {
341                 StringTokenizer tok = new StringTokenizer(maskValue, ","); //$NON-NLS-1$
342
while (tok.hasMoreTokens()) {
343                     String JavaDoc value = tok.nextToken();
344                     if (!exportMask.contains(maskValue))
345                         exportMask.addElement(value.trim());
346                 }
347             }
348             return;
349         }
350         if (elementName.equals(LIBRARY_PACKAGES)) {
351             stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
352             return;
353         }
354         stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
355         internalError(elementName);
356         return;
357     }
358
359     public void handlePluginState(String JavaDoc elementName, Attributes attributes) {
360         if (elementName.equals(RUNTIME)) {
361             // We should only have one Runtime element in a plugin or fragment
362
Object JavaDoc whatIsIt = objectStack.peek();
363             if ((whatIsIt instanceof PluginInfo) && ((PluginInfo) objectStack.peek()).libraries != null) {
364                 // This is at least the 2nd Runtime element we have hit. Ignore it.
365
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
366                 return;
367             }
368             stateStack.push(new Integer JavaDoc(PLUGIN_RUNTIME_STATE));
369             // Push a new vector to hold all the library entries objectStack.push(new Vector());
370
return;
371         }
372         if (elementName.equals(PLUGIN_REQUIRES)) {
373             stateStack.push(new Integer JavaDoc(PLUGIN_REQUIRES_STATE));
374             // Push a new vector to hold all the prerequisites
375
objectStack.push(new Vector());
376             parseRequiresAttributes(attributes);
377             return;
378         }
379         if (elementName.equals(EXTENSION_POINT)) {
380             // mark the plugin as singleton and ignore all elements under extension (if there are any)
381
manifestInfo.singleton = true;
382             stateStack.push(new Integer JavaDoc(PLUGIN_EXTENSION_POINT_STATE));
383             return;
384         }
385         if (elementName.equals(EXTENSION)) {
386             // mark the plugin as singleton and ignore all elements under extension (if there are any)
387
manifestInfo.singleton = true;
388             stateStack.push(new Integer JavaDoc(PLUGIN_EXTENSION_STATE));
389             return;
390         }
391         // If we get to this point, the element name is one we don't currently accept.
392
// Set the state to indicate that this element will be ignored
393
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
394         internalError(elementName);
395     }
396
397     public void handleRequiresImportState(String JavaDoc elementName, Attributes attributes) {
398         // All elements ignored.
399
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
400     }
401
402     public void handleRequiresState(String JavaDoc elementName, Attributes attributes) {
403         if (elementName.equals(PLUGIN_REQUIRES_IMPORT)) {
404             parsePluginRequiresImport(attributes);
405             return;
406         }
407         // If we get to this point, the element name is one we don't currently accept.
408
// Set the state to indicate that this element will be ignored
409
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
410         internalError(elementName);
411     }
412
413     public void handleRuntimeState(String JavaDoc elementName, Attributes attributes) {
414         if (elementName.equals(LIBRARY)) {
415             // Change State
416
stateStack.push(new Integer JavaDoc(RUNTIME_LIBRARY_STATE));
417             // Process library attributes
418
parseLibraryAttributes(attributes);
419             return;
420         }
421         // If we get to this point, the element name is one we don't currently accept.
422
// Set the state to indicate that this element will be ignored
423
stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
424         internalError(elementName);
425     }
426
427     private void logStatus(SAXParseException ex) {
428         String JavaDoc name = ex.getSystemId();
429         if (name == null)
430             name = ""; //$NON-NLS-1$
431
else
432             name = name.substring(1 + name.lastIndexOf("/")); //$NON-NLS-1$
433
String JavaDoc msg;
434         if (name.equals("")) //$NON-NLS-1$
435
msg = EclipseAdaptorMsg.formatter.getString("parse.error", ex.getMessage()); //$NON-NLS-1$
436
else
437             msg = EclipseAdaptorMsg.formatter.getString("parse.errorNameLineColumn", new String JavaDoc[] {name, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber()), ex.getMessage()}); //$NON-NLS-1$
438

439         FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, msg, 0, ex, null);
440         EclipseAdaptor.getDefault().getFrameworkLog().log(entry);
441     }
442
443     synchronized public PluginInfo parsePlugin(InputStream JavaDoc in) throws Exception JavaDoc {
444         SAXParserFactory JavaDoc factory = acquireXMLParsing(context);
445         if (factory == null) {
446             FrameworkLogEntry entry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_NO_SAX_FACTORY"), 0, null, null); //$NON-NLS-1$
447
EclipseAdaptor.getDefault().getFrameworkLog().log(entry);
448             return null;
449         }
450
451         factory.setNamespaceAware(true);
452         factory.setNamespaceAware(true);
453         try {
454             factory.setFeature("http://xml.org/sax/features/string-interning", true); //$NON-NLS-1$
455
} catch (SAXException se) {
456             // ignore; we can still operate without string-interning
457
}
458         factory.setValidating(false);
459         factory.newSAXParser().parse(in, this);
460         return manifestInfo;
461     }
462
463     public static SAXParserFactory JavaDoc acquireXMLParsing(BundleContext context) {
464         if (xmlTracker == null) {
465             xmlTracker = new ServiceTracker(context, "javax.xml.parsers.SAXParserFactory", null);
466             xmlTracker.open();
467         }
468         return (SAXParserFactory JavaDoc) xmlTracker.getService();
469     }
470
471     public static void releaseXMLParsing() {
472         if (xmlTracker != null)
473             xmlTracker.close();
474     }
475
476     public void parseFragmentAttributes(Attributes attributes) {
477         // process attributes
478
objectStack.push(manifestInfo);
479         int len = attributes.getLength();
480         for (int i = 0; i < len; i++) {
481             String JavaDoc attrName = attributes.getLocalName(i);
482             String JavaDoc attrValue = attributes.getValue(i).trim();
483             if (attrName.equals(FRAGMENT_ID))
484                 manifestInfo.pluginId = attrValue;
485             else if (attrName.equals(FRAGMENT_NAME))
486                 manifestInfo.pluginName = attrValue;
487             else if (attrName.equals(FRAGMENT_VERSION))
488                 manifestInfo.version = attrValue;
489             else if (attrName.equals(FRAGMENT_PROVIDER))
490                 manifestInfo.vendor = attrValue;
491             else if (attrName.equals(FRAGMENT_PLUGIN_ID))
492                 manifestInfo.masterPluginId = attrValue;
493             else if (attrName.equals(FRAGMENT_PLUGIN_VERSION))
494                 manifestInfo.masterVersion = attrValue;
495             else if (attrName.equals(FRAGMENT_PLUGIN_MATCH))
496                 manifestInfo.masterMatch = attrValue;
497         }
498     }
499
500     public void parseLibraryAttributes(Attributes attributes) {
501         // Push a vector to hold the export mask
502
objectStack.push(new Vector());
503         String JavaDoc current = attributes.getValue("", LIBRARY_NAME); //$NON-NLS-1$
504
objectStack.push(current);
505     }
506
507     public void parsePluginAttributes(Attributes attributes) {
508         // process attributes
509
objectStack.push(manifestInfo);
510         int len = attributes.getLength();
511         for (int i = 0; i < len; i++) {
512             String JavaDoc attrName = attributes.getLocalName(i);
513             String JavaDoc attrValue = attributes.getValue(i).trim();
514             if (attrName.equals(PLUGIN_ID))
515                 manifestInfo.pluginId = attrValue;
516             else if (attrName.equals(PLUGIN_NAME))
517                 manifestInfo.pluginName = attrValue;
518             else if (attrName.equals(PLUGIN_VERSION))
519                 manifestInfo.version = attrValue;
520             else if (attrName.equals(PLUGIN_VENDOR) || (attrName.equals(PLUGIN_PROVIDER)))
521                 manifestInfo.vendor = attrValue;
522             else if (attrName.equals(PLUGIN_CLASS))
523                 manifestInfo.pluginClass = attrValue;
524         }
525     }
526
527     public class Prerequisite {
528         String JavaDoc name;
529         String JavaDoc version;
530         boolean optional;
531         boolean export;
532         String JavaDoc match;
533
534         public boolean isExported() {
535             return export;
536         }
537
538         public String JavaDoc getMatch() {
539             return match;
540         }
541
542         public String JavaDoc getName() {
543             return name;
544         }
545
546         public boolean isOptional() {
547             return optional;
548         }
549
550         public String JavaDoc getVersion() {
551             return version;
552         }
553
554         public Prerequisite(String JavaDoc preqName, String JavaDoc prereqVersion, boolean isOtional, boolean isExported, String JavaDoc prereqMatch) {
555             name = preqName;
556             version = prereqVersion;
557             optional = isOtional;
558             export = isExported;
559             match = prereqMatch;
560         }
561
562         public String JavaDoc toString() {
563             return name;
564         }
565
566         public boolean equals(Object JavaDoc prereq) {
567             if (!(prereq instanceof Prerequisite))
568                 return false;
569             return name.equals(((Prerequisite) prereq).name);
570         }
571     }
572
573     public void parsePluginRequiresImport(Attributes attributes) {
574         if (manifestInfo.requires == null) {
575             manifestInfo.requires = new ArrayList();
576             // to avoid cycles
577
// if (!manifestInfo.pluginId.equals(PluginConverterImpl.PI_RUNTIME)) //$NON-NLS-1$
578
// manifestInfo.requires.add(new Prerequisite(PluginConverterImpl.PI_RUNTIME, null, false, false, null)); //$NON-NLS-1$
579
}
580         // process attributes
581
String JavaDoc plugin = attributes.getValue("", PLUGIN_REQUIRES_PLUGIN); //$NON-NLS-1$
582
if (plugin == null)
583             return;
584         if (plugin.equals(PluginConverterImpl.PI_BOOT)) //$NON-NLS-1$//$NON-NLS-2$
585
return;
586         if (plugin.equals(PluginConverterImpl.PI_RUNTIME_COMPATIBILITY))
587             manifestInfo.compatibilityFound = true;
588         String JavaDoc version = attributes.getValue("", PLUGIN_REQUIRES_PLUGIN_VERSION); //$NON-NLS-1$
589
String JavaDoc optional = attributes.getValue("", PLUGIN_REQUIRES_OPTIONAL); //$NON-NLS-1$
590
String JavaDoc export = attributes.getValue("", PLUGIN_REQUIRES_EXPORT); //$NON-NLS-1$
591
String JavaDoc match = attributes.getValue("", PLUGIN_REQUIRES_MATCH); //$NON-NLS-1$
592
manifestInfo.requires.add(new Prerequisite(plugin, version, "true".equalsIgnoreCase(optional) ? true : false, "true".equalsIgnoreCase(export) ? true : false, match)); //$NON-NLS-1$ //$NON-NLS-2$
593
}
594
595     public void parseRequiresAttributes(Attributes attributes) {
596         //Nothing to do.
597
}
598
599     static String JavaDoc replace(String JavaDoc s, String JavaDoc from, String JavaDoc to) {
600         String JavaDoc str = s;
601         int fromLen = from.length();
602         int toLen = to.length();
603         int ix = str.indexOf(from);
604         while (ix != -1) {
605             str = str.substring(0, ix) + to + str.substring(ix + fromLen);
606             ix = str.indexOf(from, ix + toLen);
607         }
608         return str;
609     }
610
611     public void startDocument() {
612         stateStack.push(new Integer JavaDoc(INITIAL_STATE));
613     }
614
615     public void startElement(String JavaDoc uri, String JavaDoc elementName, String JavaDoc qName, Attributes attributes) {
616         switch (((Integer JavaDoc) stateStack.peek()).intValue()) {
617             case INITIAL_STATE :
618                 handleInitialState(elementName, attributes);
619                 break;
620             case FRAGMENT_STATE :
621             case PLUGIN_STATE :
622                 handlePluginState(elementName, attributes);
623                 break;
624             case PLUGIN_RUNTIME_STATE :
625                 handleRuntimeState(elementName, attributes);
626                 break;
627             case PLUGIN_REQUIRES_STATE :
628                 handleRequiresState(elementName, attributes);
629                 break;
630             case PLUGIN_EXTENSION_POINT_STATE :
631                 handleExtensionPointState(elementName, attributes);
632                 break;
633             case PLUGIN_EXTENSION_STATE :
634                 handleExtensionState(elementName, attributes);
635                 break;
636             case RUNTIME_LIBRARY_STATE :
637                 handleLibraryState(elementName, attributes);
638                 break;
639             case LIBRARY_EXPORT_STATE :
640                 handleLibraryExportState(elementName, attributes);
641                 break;
642             case PLUGIN_REQUIRES_IMPORT_STATE :
643                 handleRequiresImportState(elementName, attributes);
644                 break;
645             default :
646                 stateStack.push(new Integer JavaDoc(IGNORED_ELEMENT_STATE));
647         }
648     }
649
650     public void warning(SAXParseException ex) {
651         logStatus(ex);
652     }
653
654     private void internalError(String JavaDoc elementName) {
655         FrameworkLogEntry error;
656         String JavaDoc message = EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONVERTER_PARSE_UNKNOWNTOP_ELEMENT", elementName); //$NON-NLS-1$
657
error = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, (manifestInfo.pluginId == null ? message : "Plug-in : " + manifestInfo.pluginId + ", " + message), 0, null, null); //$NON-NLS-1$ //$NON-NLS-2$
658
EclipseAdaptor.getDefault().getFrameworkLog().log(error);
659     }
660
661     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
662         // Since 3.0, a processing instruction of the form <?eclipse version="3.0"?> at
663
// the start of the manifest file is used to indicate the plug-in manifest
664
// schema version in effect. Pre-3.0 (i.e., 2.1) plug-in manifest files do not
665
// have one of these, and this is how we can distinguish the manifest of a
666
// pre-3.0 plug-in from a post-3.0 one (for compatibility tranformations).
667
if (target.equalsIgnoreCase("eclipse")) { //$NON-NLS-1$
668
// just the presence of this processing instruction indicates that this
669
// plug-in is at least 3.0
670
manifestInfo.schemaVersion = "3.0"; //$NON-NLS-1$
671
StringTokenizer tokenizer = new StringTokenizer(data, "=\""); //$NON-NLS-1$
672
while (tokenizer.hasMoreTokens()) {
673                 String JavaDoc token = tokenizer.nextToken();
674                 if (token.equalsIgnoreCase("version")) { //$NON-NLS-1$
675
if (!tokenizer.hasMoreTokens()) {
676                         break;
677                     }
678                     manifestInfo.schemaVersion = tokenizer.nextToken();
679                     break;
680                 }
681             }
682         }
683     }
684 }
Popular Tags