KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > PluginParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.update.internal.configurator;
12
13
14 import java.io.*;
15
16 import javax.xml.parsers.*;
17
18 import org.eclipse.osgi.util.NLS;
19 import org.xml.sax.*;
20 import org.xml.sax.helpers.*;
21
22 /**
23  * Parse default feature.xml
24  */

25
26 public class PluginParser extends DefaultHandler implements IConfigurationConstants {
27     private final static SAXParserFactory parserFactory =
28         SAXParserFactory.newInstance();
29     private SAXParser parser;
30     private PluginEntry pluginEntry;
31     private String JavaDoc location;
32
33     private class ParseCompleteException extends SAXException {
34         
35         private static final long serialVersionUID = 1L;
36
37         public ParseCompleteException(String JavaDoc arg0) {
38             super(arg0);
39         }
40     }
41
42     /**
43      * Constructor for DefaultFeatureParser
44      */

45     public PluginParser() {
46         super();
47         try {
48             parserFactory.setNamespaceAware(true);
49             this.parser = parserFactory.newSAXParser();
50         } catch (ParserConfigurationException e) {
51             System.out.println(e);
52         } catch (SAXException e) {
53             System.out.println(e);
54         }
55     }
56
57     /**
58      * @since 2.0
59      */

60     public synchronized PluginEntry parse(File pluginFile) throws SAXException, IOException {
61         FileInputStream in = null;
62         try{
63             in = new FileInputStream(pluginFile);
64             return parse(in, PLUGINS + "/" + pluginFile.getParentFile().getName() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
65
}finally{
66             if (in != null){
67                 try{
68                     in.close();
69                 }catch(IOException e){
70                 }
71             }
72         }
73     }
74     /**
75      * @since 3.0
76      */

77     public synchronized PluginEntry parse(InputStream in, String JavaDoc bundleUrl) throws SAXException, IOException {
78         try {
79             location = bundleUrl;
80             pluginEntry = new PluginEntry();
81             pluginEntry.setURL(bundleUrl);
82             parser.parse(new InputSource(in), this);
83         } catch (ParseCompleteException e) {
84             // expected, we stopped the parsing when we have the information we need
85
/// no need to pursue the parsing
86
}
87         return pluginEntry;
88     }
89
90     /**
91      * @see DefaultHandler#startElement(String, String, String, Attributes)
92      */

93     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws SAXException {
94
95         String JavaDoc tag = localName.trim();
96
97         if (tag.equalsIgnoreCase(CFG_PLUGIN)) {
98             pluginEntry.isFragment(false);
99             processPlugin(attributes);
100             return;
101         }
102
103         if (tag.equalsIgnoreCase(CFG_FRAGMENT)) {
104             pluginEntry.isFragment(true);
105             processPlugin(attributes);
106             return;
107         }
108     }
109
110     /**
111      * process plugin entry info
112      */

113     private void processPlugin(Attributes attributes) throws ParseCompleteException {
114         String JavaDoc id = attributes.getValue("id"); //$NON-NLS-1$
115
String JavaDoc version = attributes.getValue("version"); //$NON-NLS-1$
116
if (id == null || id.trim().length() == 0) {
117             id = "_no_id_"; //$NON-NLS-1$
118
Utils.log(NLS.bind(Messages.PluginParser_plugin_no_id, (new String JavaDoc[] { location })));
119         }
120         if (version == null || version.trim().length() == 0) {
121             version = "0.0.0"; //$NON-NLS-1$
122
Utils.log(NLS.bind(Messages.PluginParser_plugin_no_version, (new String JavaDoc[] { location })));
123         }
124         pluginEntry.setVersionedIdentifier(new VersionedIdentifier(id, version));
125         
126         // stop parsing now
127
throw new ParseCompleteException(""); //$NON-NLS-1$
128
}
129 }
130
Popular Tags