1 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 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 location; 32 33 private class ParseCompleteException extends SAXException { 34 35 private static final long serialVersionUID = 1L; 36 37 public ParseCompleteException(String arg0) { 38 super(arg0); 39 } 40 } 41 42 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 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() + "/"); }finally{ 66 if (in != null){ 67 try{ 68 in.close(); 69 }catch(IOException e){ 70 } 71 } 72 } 73 } 74 77 public synchronized PluginEntry parse(InputStream in, String 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 } 87 return pluginEntry; 88 } 89 90 93 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 94 95 String 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 113 private void processPlugin(Attributes attributes) throws ParseCompleteException { 114 String id = attributes.getValue("id"); String version = attributes.getValue("version"); if (id == null || id.trim().length() == 0) { 117 id = "_no_id_"; Utils.log(NLS.bind(Messages.PluginParser_plugin_no_id, (new String [] { location }))); 119 } 120 if (version == null || version.trim().length() == 0) { 121 version = "0.0.0"; Utils.log(NLS.bind(Messages.PluginParser_plugin_no_version, (new String [] { location }))); 123 } 124 pluginEntry.setVersionedIdentifier(new VersionedIdentifier(id, version)); 125 126 throw new ParseCompleteException(""); } 129 } 130 | Popular Tags |