KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ResourceBundle JavaDoc;
17
18 import javax.xml.parsers.*;
19
20 import org.eclipse.osgi.util.NLS;
21 import org.xml.sax.*;
22 import org.xml.sax.helpers.*;
23
24 /**
25  * A more complete feature parser. It adds the plugins listed to the feature.
26  */

27 public class FullFeatureParser extends DefaultHandler implements IConfigurationConstants{
28
29     private SAXParser parser;
30     private FeatureEntry feature;
31     private URL url;
32     private boolean isDescription;
33     private StringBuffer JavaDoc description = new StringBuffer JavaDoc();
34
35     private final static SAXParserFactory parserFactory =
36         SAXParserFactory.newInstance();
37
38     /**
39      * Constructs a feature parser.
40      */

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

55     public void parse(){
56         InputStream in = null;
57         try {
58             if (feature.getSite() == null)
59                 return;
60             this.url = new URL(feature.getSite().getResolvedURL(), feature.getURL() + FEATURE_XML);
61             in = url.openStream();
62             parser.parse(new InputSource(in), this);
63         } catch (SAXException e) {
64         } catch (IOException e) {
65         } finally {
66             if (in != null)
67                 try {
68                     in.close();
69                 } catch (IOException e1) {
70                     Utils.log(e1.getLocalizedMessage());
71                 }
72         }
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 ("plugin".equals(localName)) { //$NON-NLS-1$
85
processPlugin(attributes);
86         } else if ("description".equals(localName)){ //$NON-NLS-1$
87
isDescription = true;
88         } else if ("license".equals(localName)) { //$NON-NLS-1$
89
processLicense(attributes);
90         }
91     }
92
93     /*
94      * Process feature information
95      */

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

102         if (id == null || id.trim().equals("") //$NON-NLS-1$
103
|| ver == null || ver.trim().equals("")) { //$NON-NLS-1$
104
System.out.println(NLS.bind(Messages.FeatureParser_IdOrVersionInvalid, (new String JavaDoc[] { id, ver})));
105         } else {
106 // String label = attributes.getValue("label"); //$NON-NLS-1$
107
// String provider = attributes.getValue("provider-name"); //$NON-NLS-1$
108
String JavaDoc nl = attributes.getValue("nl"); //$NON-NLS-1$
109
String JavaDoc os = attributes.getValue("os"); //$NON-NLS-1$
110
String JavaDoc ws = attributes.getValue("ws"); //$NON-NLS-1$
111
String JavaDoc arch = attributes.getValue("arch"); //$NON-NLS-1$
112
if (!Utils.isValidEnvironment(os, ws, arch,nl))
113                 return;
114
115             PluginEntry plugin = new PluginEntry();
116             plugin.setPluginIdentifier(id);
117             plugin.setPluginVersion(ver);
118             feature.addPlugin(plugin);
119             
120             Utils.
121                 debug("End process DefaultFeature tag: id:" +id + " ver:" +ver + " url:" + feature.getURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
122
}
123     }
124     
125     private void processLicense(Attributes attributes ){
126         feature.setLicenseURL(attributes.getValue("url")); //$NON-NLS-1$
127
}
128     
129     /* (non-Javadoc)
130      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
131      */

132     public void characters(char[] ch, int start, int length)
133             throws SAXException {
134         if (!isDescription)
135             return;
136         description.append(ch, start, length);
137     }
138     /* (non-Javadoc)
139      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
140      */

141     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
142             throws SAXException {
143         if ("description".equals(localName)) { //$NON-NLS-1$
144
isDescription = false;
145             String JavaDoc d = description.toString().trim();
146             ResourceBundle JavaDoc bundle = feature.getResourceBundle();
147             feature.setDescription(Utils.getResourceString(bundle, d));
148         }
149     }
150 }
151
Popular Tags