1 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 ; 16 import java.util.Date ; 17 import java.util.StringTokenizer ; 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 28 29 public class ConfigurationParser extends DefaultHandler implements IConfigurationConstants { 30 31 private static final String URL_PROPERTY = "org.eclipse.update.resolution_url"; private static final String EMPTY_STRING = ""; 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 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)); throw new InvocationTargetException(e); 53 } catch (SAXException e) { 54 Utils.log(Utils.newStatus("ConfigurationParser", e)); throw new InvocationTargetException(e); 56 } 57 } 58 59 public Configuration parse(URL url) throws Exception { 60 61 Utils.debug("Start parsing Configuration:" + url); long lastModified = 0; 64 try { 65 configURL = url; 66 if ("file".equals(url.getProtocol())) { 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 e) { 77 Utils.log(Utils.newStatus("ConfigurationParser.parse() error:", e)); 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 96 public void startElement( 97 String uri, 98 String localName, 99 String qName, 100 Attributes attributes) 101 throws SAXException { 102 103 Utils.debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); try { 106 107 String 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 [] { e.getMessage() })), e); 126 } catch (CoreException e) { 127 throw new SAXException(NLS.bind(Messages.InstalledSiteParser_ErrorParsingFile, (new String [] { e.toString() })), e); 128 } 129 } 130 131 134 private void processSite(Attributes attributes) 135 throws MalformedURLException, CoreException { 136 137 if (config == null) 138 return; 139 140 currentSiteURL = null; 142 143 String 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 url = new URL(PlatformConfiguration.getInstallURL(), urlString); 153 return; 154 } 155 156 String 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 currentSiteURL = url; 166 167 int policyType; 168 String [] policyList = null; 169 String 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 pluginList = attributes.getValue(CFG_LIST); 186 if (pluginList != null) { 187 StringTokenizer st = new StringTokenizer (pluginList,","); policyList = new String [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 flag = attributes.getValue(CFG_UPDATEABLE); 199 if (flag != null) { 200 if (flag.equals("true")) site.setUpdateable(true); 202 else 203 site.setUpdateable(false); 204 } 205 206 flag = attributes.getValue(CFG_ENABLED); 207 if (flag != null && flag.equals("false")) site.setEnabled(false); 209 else 210 site.setEnabled(true); 211 212 String linkname = attributes.getValue(CFG_LINK_FILE); 213 if (linkname != null && !linkname.equals("")) { site.setLinkFileName(linkname.replace('/', File.separatorChar)); 215 } 216 217 Utils.debug("End process config site url:" + urlString + " policy:" + typeString + " updatable:"+flag ); 220 currentSiteURL = site.getURL(); 221 config.addSiteEntry(currentSiteURL.toExternalForm(), site); 222 } 223 224 227 private void processFeature(Attributes attributes) 228 throws MalformedURLException, CoreException { 229 230 if (currentSiteURL == null) 231 return; 233 String id = attributes.getValue(CFG_FEATURE_ENTRY_ID); 234 if (id == null) 235 return; 236 String version = attributes.getValue(CFG_FEATURE_ENTRY_VERSION); 237 String pluginVersion = attributes.getValue(CFG_FEATURE_ENTRY_PLUGIN_VERSION); 238 if (pluginVersion == null || pluginVersion.trim().length() == 0) 239 pluginVersion = version; 240 String pluginIdentifier = attributes.getValue(CFG_FEATURE_ENTRY_PLUGIN_IDENTIFIER); 241 if (pluginIdentifier != null && pluginIdentifier.trim().length() == 0) 242 pluginIdentifier = null; 243 String application = attributes.getValue(CFG_FEATURE_ENTRY_APPLICATION); 244 245 String locations = attributes.getValue(CFG_FEATURE_ENTRY_ROOT); 247 StringTokenizer st = locations != null ? new StringTokenizer (locations,",") : new StringTokenizer (""); ArrayList rootList = new ArrayList (st.countTokens()); 249 while (st.hasMoreTokens()){ 250 try{ 251 URL rootEntry = new URL(st.nextToken()); 252 rootList.add(rootEntry); 253 } catch (MalformedURLException e) { 254 } 256 } 257 URL[] roots = (URL[]) rootList.toArray(new URL[rootList.size()]); 258 259 boolean primary = false; 261 String flag = attributes.getValue(CFG_FEATURE_ENTRY_PRIMARY); 262 if (flag != null) { 263 if (flag.equals("true")) primary = true; 265 } 266 267 FeatureEntry featureEntry = new FeatureEntry(id, version, pluginIdentifier, pluginVersion, primary, application, roots); 268 269 String 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 } 281 282 283 286 private void processConfig(Attributes attributes) { 287 String date = attributes.getValue(CFG_DATE); 288 if (date == null || date.trim().length() == 0) 289 config = new Configuration(); else { 291 long time = 0; 292 try { 293 time = Long.parseLong(date); 294 config = new Configuration(new Date (time)); 295 } catch (NumberFormatException e1) { 296 time = new Date ().getTime(); 297 Utils.log(NLS.bind(Messages.InstalledSiteParser_date, (new String [] { date }))); 298 config = new Configuration(); } 300 } 301 302 config.setURL(configURL); 303 304 try { 305 String 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 (); 312 config.setLinkedConfig(sharedConfig); 313 } 314 } catch (Exception e) { 315 Utils.log(Utils.newStatus(Messages.ConfigurationParser_cannotLoadSharedInstall, e)); 317 } 318 319 String flag = attributes.getValue(CFG_TRANSIENT); 320 if (flag != null) { 321 config.setTransient(flag.equals("true")); } 323 324 Utils.debug("End Processing Config Tag: date:" + attributes.getValue(CFG_DATE)); } 327 328 private boolean isValidSite(URL url) { 329 URL resolvedURL= url; 330 if (url.getProtocol().equals("platform")) { try { 332 resolvedURL = PlatformConfiguration.resolvePlatformURL(url); } catch (IOException e) { 334 } 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 "); return false; 345 } 346 return true; 347 } 348 351 public void endElement(String uri, String localName, String qName) 352 throws SAXException { 353 super.endElement(uri, localName, qName); 354 355 Utils.debug("End Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); try { 358 359 String tag = localName.trim(); 360 361 if (tag.equalsIgnoreCase(CFG)) { 362 SiteEntry[] sites = config.getSites(); 366 for (int i=0; i<sites.length; i++) 367 sites[i].initialized(); 368 return; 369 } 370 } catch (Exception e) { 371 } 373 } 374 } 375 | Popular Tags |