KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.*;
13 import java.lang.reflect.*;
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import javax.xml.parsers.*;
19
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.osgi.util.NLS;
22 import org.xml.sax.*;
23 import org.xml.sax.helpers.*;
24
25 /**
26  * parse the default site.xml
27  */

28
29 public class ConfigurationParser extends DefaultHandler implements IConfigurationConstants {
30     
31     private static final String JavaDoc URL_PROPERTY = "org.eclipse.update.resolution_url"; //$NON-NLS-1$
32
private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
33
private final static SAXParserFactory parserFactory =
34         SAXParserFactory.newInstance();
35     private SAXParser parser;
36     
37     private URL currentSiteURL;
38     private Configuration config;
39     private URL configURL;
40     private InputStream input;
41     
42     /**
43      * Constructor for ConfigurationParser
44      */

45     public ConfigurationParser() throws InvocationTargetException {
46
47         try {
48             parserFactory.setNamespaceAware(true);
49             this.parser = parserFactory.newSAXParser();
50         } catch (ParserConfigurationException e) {
51             Utils.log(Utils.newStatus("ConfigurationParser", e)); //$NON-NLS-1$
52
throw new InvocationTargetException(e);
53         } catch (SAXException e) {
54             Utils.log(Utils.newStatus("ConfigurationParser", e)); //$NON-NLS-1$
55
throw new InvocationTargetException(e);
56         }
57     }
58     
59     public Configuration parse(URL url) throws Exception JavaDoc {
60
61         // DEBUG:
62
Utils.debug("Start parsing Configuration:" + url); //$NON-NLS-1$
63
long lastModified = 0;
64         try {
65             configURL = url;
66             if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
67
File inputFile = new File(url.getFile());
68                 if (!inputFile.exists() || !inputFile.canRead())
69                     return null;
70                 lastModified = inputFile.lastModified();
71                 input = new FileInputStream(inputFile);
72             } else
73                 input = url.openStream();
74             parser.parse(new InputSource(input), this);
75             return config;
76         } catch (Exception JavaDoc e) {
77             Utils.log(Utils.newStatus("ConfigurationParser.parse() error:", e)); //$NON-NLS-1$
78
throw e;
79         } finally {
80             if (config != null)
81                 config.setLastModified(lastModified);
82             try {
83                 if (input != null) {
84                     input.close();
85                     input = null;
86                 }
87             } catch (IOException e1) {
88                 Utils.log(e1.getLocalizedMessage());
89             }
90         }
91     }
92
93     /**
94      * @see DefaultHandler#startElement(String, String, String, Attributes)
95      */

96     public void startElement(
97         String JavaDoc uri,
98         String JavaDoc localName,
99         String JavaDoc qName,
100         Attributes attributes)
101         throws SAXException {
102
103         // DEBUG:
104
Utils.debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
105
try {
106
107             String JavaDoc tag = localName.trim();
108
109             if (tag.equalsIgnoreCase(CFG)) {
110                 processConfig(attributes);
111                 return;
112             }
113
114             if (tag.equalsIgnoreCase(CFG_SITE)) {
115                 processSite(attributes);
116                 return;
117             }
118
119             if (tag.equalsIgnoreCase(CFG_FEATURE_ENTRY)) {
120                 processFeature(attributes);
121                 return;
122             }
123
124         } catch (MalformedURLException e) {
125             throw new SAXException(NLS.bind(Messages.InstalledSiteParser_UnableToCreateURL, (new String JavaDoc[] { e.getMessage() })), e);
126         } catch (CoreException e) {
127             throw new SAXException(NLS.bind(Messages.InstalledSiteParser_ErrorParsingFile, (new String JavaDoc[] { e.toString() })), e);
128         }
129     }
130
131     /**
132      * process the Site info
133      */

134     private void processSite(Attributes attributes)
135         throws MalformedURLException, CoreException {
136
137         if (config == null)
138             return;
139         
140         // reset current site
141
currentSiteURL = null;
142         
143         String JavaDoc urlString = attributes.getValue(CFG_URL);
144         if (urlString == null)
145             return;
146
147         URL url = null;
148         try {
149             url = new URL(urlString);
150         } catch (MalformedURLException e) {
151             // try relative to install url
152
url = new URL(PlatformConfiguration.getInstallURL(), urlString);
153             return;
154         }
155         
156         // when reading externalized URLs, need to convert them to absolute form
157
String JavaDoc property = System.getProperty(URL_PROPERTY, EMPTY_STRING);
158         URL root = property == null || property.length() == 0 ? Utils.getInstallURL() : new URL(property);
159         url = Utils.makeAbsolute(root, url);
160         
161         if (!isValidSite(url))
162             return;
163         
164         // use this new site
165
currentSiteURL = url;
166
167         int policyType;
168         String JavaDoc[] policyList = null;
169         String JavaDoc typeString = attributes.getValue(CFG_POLICY);
170         if (typeString == null) {
171             policyType = PlatformConfiguration.getDefaultPolicy();
172             policyList = DEFAULT_POLICY_LIST;
173         } else {
174             int i;
175             for (i = 0; i < CFG_POLICY_TYPE.length; i++) {
176                 if (typeString.equals(CFG_POLICY_TYPE[i])) {
177                     break;
178                 }
179             }
180             if (i >= CFG_POLICY_TYPE.length) {
181                 policyType = PlatformConfiguration.getDefaultPolicy();
182                 policyList = DEFAULT_POLICY_LIST;
183             } else {
184                 policyType = i;
185                 String JavaDoc pluginList = attributes.getValue(CFG_LIST);
186                 if (pluginList != null) {
187                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pluginList,","); //$NON-NLS-1$
188
policyList = new String JavaDoc[st.countTokens()];
189                     for (i=0; i<policyList.length; i++)
190                         policyList[i] = st.nextToken();
191                 }
192             }
193         }
194
195         SitePolicy sp = new SitePolicy(policyType, policyList);
196         SiteEntry site = new SiteEntry(url, sp);
197
198         String JavaDoc flag = attributes.getValue(CFG_UPDATEABLE);
199         if (flag != null) {
200             if (flag.equals("true")) //$NON-NLS-1$
201
site.setUpdateable(true);
202             else
203                 site.setUpdateable(false);
204         }
205         
206         flag = attributes.getValue(CFG_ENABLED);
207         if (flag != null && flag.equals("false")) //$NON-NLS-1$
208
site.setEnabled(false);
209         else
210             site.setEnabled(true);
211
212         String JavaDoc linkname = attributes.getValue(CFG_LINK_FILE);
213         if (linkname != null && !linkname.equals("")) { //$NON-NLS-1$
214
site.setLinkFileName(linkname.replace('/', File.separatorChar));
215         }
216
217         // DEBUG:
218
Utils.debug("End process config site url:" + urlString + " policy:" + typeString + " updatable:"+flag ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
219

220         currentSiteURL = site.getURL();
221         config.addSiteEntry(currentSiteURL.toExternalForm(), site);
222     }
223     
224     /**
225      * process the DefaultFeature info
226      */

227     private void processFeature(Attributes attributes)
228         throws MalformedURLException, CoreException {
229
230         if (currentSiteURL == null)
231             return; // the site was not correct
232

233         String JavaDoc id = attributes.getValue(CFG_FEATURE_ENTRY_ID);
234         if (id == null)
235             return;
236         String JavaDoc version = attributes.getValue(CFG_FEATURE_ENTRY_VERSION);
237         String JavaDoc pluginVersion = attributes.getValue(CFG_FEATURE_ENTRY_PLUGIN_VERSION);
238         if (pluginVersion == null || pluginVersion.trim().length() == 0)
239             pluginVersion = version;
240         String JavaDoc pluginIdentifier = attributes.getValue(CFG_FEATURE_ENTRY_PLUGIN_IDENTIFIER);
241         if (pluginIdentifier != null && pluginIdentifier.trim().length() == 0)
242             pluginIdentifier = null;
243         String JavaDoc application = attributes.getValue(CFG_FEATURE_ENTRY_APPLICATION);
244         
245         // get install locations
246
String JavaDoc locations = attributes.getValue(CFG_FEATURE_ENTRY_ROOT);
247         StringTokenizer JavaDoc st = locations != null ? new StringTokenizer JavaDoc(locations,",") : new StringTokenizer JavaDoc(""); //$NON-NLS-1$ //$NON-NLS-2$
248
ArrayList JavaDoc rootList = new ArrayList JavaDoc(st.countTokens());
249         while (st.hasMoreTokens()){
250             try{
251                 URL rootEntry = new URL(st.nextToken());
252                 rootList.add(rootEntry);
253             } catch (MalformedURLException e) {
254                 // skip bad entries ...
255
}
256         }
257         URL[] roots = (URL[]) rootList.toArray(new URL[rootList.size()]);
258
259         // get primary flag
260
boolean primary = false;
261         String JavaDoc flag = attributes.getValue(CFG_FEATURE_ENTRY_PRIMARY);
262         if (flag != null) {
263             if (flag.equals("true")) //$NON-NLS-1$
264
primary = true;
265         }
266         
267         FeatureEntry featureEntry = new FeatureEntry(id, version, pluginIdentifier, pluginVersion, primary, application, roots);
268
269         // set the url
270
String JavaDoc url = attributes.getValue(CFG_URL);
271         if (url != null && url.trim().length() > 0)
272             featureEntry.setURL(url);
273         
274         SiteEntry site = config.getSiteEntry(currentSiteURL.toExternalForm());
275         site.addFeatureEntry(featureEntry);
276         
277         // configured ?
278
// String configuredString = attributes.getValue("configured"); //$NON-NLS-1$
279
// boolean configured = configuredString.trim().equalsIgnoreCase("true") ? true : false; //$NON-NLS-1$
280
}
281
282
283     /**
284      * process the Config info
285      */

286     private void processConfig(Attributes attributes) {
287         String JavaDoc date = attributes.getValue(CFG_DATE);
288         if (date == null || date.trim().length() == 0)
289             config = new Configuration(); // constructed with current date
290
else {
291             long time = 0;
292             try {
293                 time = Long.parseLong(date);
294                 config = new Configuration(new Date JavaDoc(time));
295             } catch (NumberFormatException JavaDoc e1) {
296                 time = new Date JavaDoc().getTime();
297                 Utils.log(NLS.bind(Messages.InstalledSiteParser_date, (new String JavaDoc[] { date })));
298                 config = new Configuration(); // constructed with current date
299
}
300         }
301         
302         config.setURL(configURL);
303         
304         try {
305             String JavaDoc sharedURLString = attributes.getValue(CFG_SHARED_URL);
306             if (sharedURLString != null) {
307                 URL sharedURL = Utils.makeAbsolute(Utils.getInstallURL(), new URL(sharedURLString));
308                 ConfigurationParser parser = new ConfigurationParser();
309                 Configuration sharedConfig = parser.parse(sharedURL);
310                 if (sharedConfig == null)
311                     throw new Exception JavaDoc();
312                 config.setLinkedConfig(sharedConfig);
313             }
314         } catch (Exception JavaDoc e) {
315             // could not load from shared install
316
Utils.log(Utils.newStatus(Messages.ConfigurationParser_cannotLoadSharedInstall, e));
317         }
318
319         String JavaDoc flag = attributes.getValue(CFG_TRANSIENT);
320         if (flag != null) {
321             config.setTransient(flag.equals("true")); //$NON-NLS-1$
322
}
323         
324         // DEBUG:
325
Utils.debug("End Processing Config Tag: date:" + attributes.getValue(CFG_DATE)); //$NON-NLS-1$
326
}
327     
328     private boolean isValidSite(URL url) {
329         URL resolvedURL= url;
330         if (url.getProtocol().equals("platform")) { //$NON-NLS-1$
331
try {
332                 resolvedURL = PlatformConfiguration.resolvePlatformURL(url); // 19536
333
} catch (IOException e) {
334                 // will use the baseline URL ...
335
}
336         }
337         
338         if (!PlatformConfiguration.supportsDetection(resolvedURL))
339             return false;
340
341         File siteRoot = new File(resolvedURL.getFile().replace('/', File.separatorChar));
342         if (!siteRoot.exists()) {
343             Utils.debug("Site " + resolvedURL + " does not exist "); //$NON-NLS-1$ //$NON-NLS-2$
344
return false;
345         }
346         return true;
347     }
348     /* (non-Javadoc)
349      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
350      */

351     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
352             throws SAXException {
353         super.endElement(uri, localName, qName);
354         
355         // DEBUG:
356
Utils.debug("End Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
357
try {
358
359             String JavaDoc tag = localName.trim();
360
361             if (tag.equalsIgnoreCase(CFG)) {
362                  // This is a bit of a hack.
363
// When no features were added to the site, but the site is initialized from platform.xml
364
// we need to set the feature set to empty, so we don't try to detect them.
365
SiteEntry[] sites = config.getSites();
366                 for (int i=0; i<sites.length; i++)
367                     sites[i].initialized();
368                 return;
369             }
370         } catch (Exception JavaDoc e) {
371             // silent ignore
372
}
373     }
374 }
375
Popular Tags