KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.update.internal.configurator;
12
13
14 import java.io.*;
15 import java.net.*;
16
17 import javax.xml.parsers.*;
18
19 import org.eclipse.osgi.util.NLS;
20 import org.xml.sax.*;
21 import org.xml.sax.helpers.*;
22
23 /**
24  * Default feature parser.
25  * Parses the feature manifest file as defined by the platform.
26  *
27  * @since 3.0
28  */

29 public class FeatureParser extends DefaultHandler {
30
31     private SAXParser parser;
32     private FeatureEntry feature;
33     private URL url;
34
35     private final static SAXParserFactory parserFactory =
36         SAXParserFactory.newInstance();
37
38     /**
39      * Constructs a feature parser.
40      */

41     public FeatureParser() {
42         super();
43         try {
44             parserFactory.setNamespaceAware(true);
45             this.parser = parserFactory.newSAXParser();
46         } catch (ParserConfigurationException e) {
47             System.out.println(e);
48         } catch (SAXException e) {
49             System.out.println(e);
50         }
51     }
52     /**
53      * Parses the specified url and constructs a feature
54      */

55     public FeatureEntry parse(URL featureURL){
56         feature=null;
57         InputStream in = null;
58         try {
59             this.url = featureURL;
60             in = featureURL.openStream();
61             parser.parse(new InputSource(in), this);
62         } catch (SAXException e) {
63         } catch (IOException e) {
64         } finally {
65             if (in != null)
66                 try {
67                     in.close();
68                 } catch (IOException e1) {
69                     Utils.log(e1.getLocalizedMessage());
70                 }
71         }
72         return feature;
73     }
74
75     /**
76      * Handle start of element tags
77      * @see DefaultHandler#startElement(String, String, String, Attributes)
78      * @since 2.0
79      */

80     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes attributes) throws SAXException {
81
82         Utils.debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
83

84         if ("feature".equals(localName)) { //$NON-NLS-1$
85
processFeature(attributes);
86             // stop parsing now
87
throw new SAXException(""); //$NON-NLS-1$
88
}
89     }
90
91     /*
92      * Process feature information
93      */

94     private void processFeature(Attributes attributes) {
95
96         // identifier and version
97
String JavaDoc id = attributes.getValue("id"); //$NON-NLS-1$
98
String JavaDoc ver = attributes.getValue("version"); //$NON-NLS-1$
99

100         if (id == null || id.trim().equals("") //$NON-NLS-1$
101
|| ver == null || ver.trim().equals("")) { //$NON-NLS-1$
102
System.out.println(NLS.bind(Messages.FeatureParser_IdOrVersionInvalid, (new String JavaDoc[] { id, ver})));
103         } else {
104 // String label = attributes.getValue("label"); //$NON-NLS-1$
105
// String provider = attributes.getValue("provider-name"); //$NON-NLS-1$
106
// String imageURL = attributes.getValue("image"); //$NON-NLS-1$
107
String JavaDoc os = attributes.getValue("os"); //$NON-NLS-1$
108
String JavaDoc ws = attributes.getValue("ws"); //$NON-NLS-1$
109
String JavaDoc nl = attributes.getValue("nl"); //$NON-NLS-1$
110
String JavaDoc arch = attributes.getValue("arch"); //$NON-NLS-1$
111
if (!Utils.isValidEnvironment(os, ws, arch, nl))
112                 return;
113 // String exclusive = attributes.getValue("exclusive"); //$NON-NLS-1$
114
// String affinity = attributes.getValue("colocation-affinity"); //$NON-NLS-1$
115

116             String JavaDoc primary = attributes.getValue("primary"); //$NON-NLS-1$
117
boolean isPrimary = "true".equals(primary); //$NON-NLS-1$
118
String JavaDoc application = attributes.getValue("application"); //$NON-NLS-1$
119
String JavaDoc plugin = attributes.getValue("plugin"); //$NON-NLS-1$
120

121             //TODO rootURLs
122
feature = new FeatureEntry(id, ver, plugin, "", isPrimary, application, null ); //$NON-NLS-1$
123
if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
124
File f = new File(url.getFile().replace('/', File.separatorChar));
125                 feature.setURL("features" + "/" + f.getParentFile().getName() + "/");// + f.getName()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
126
} else {
127                 // externalized URLs might be in relative form, ensure they are absolute
128
feature.setURL(Utils.makeAbsolute(Utils.getInstallURL(), url).toExternalForm());
129             }
130
131             Utils.
132                 debug("End process DefaultFeature tag: id:" +id + " ver:" +ver + " url:" + feature.getURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
133
}
134     }
135 }
136
Popular Tags