1 11 package org.eclipse.update.internal.core; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.eclipse.core.runtime.MultiStatus; 22 import org.eclipse.update.core.model.FeatureModelFactory; 23 import org.xml.sax.Attributes ; 24 import org.xml.sax.InputSource ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.SAXParseException ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 29 public class DigestParser extends DefaultHandler { 30 31 private InternalFeatureParser featureParser; 32 33 private ArrayList featureModels; 34 35 private SAXParser parser; 36 37 private FeatureModelFactory factory; 38 39 private String location; 40 41 private final static SAXParserFactory parserFactory = 42 SAXParserFactory.newInstance(); 43 44 public DigestParser() { 45 super(); 46 featureParser = new InternalFeatureParser(); 47 try { 48 parserFactory.setNamespaceAware(true); 49 this.parser = parserFactory.newSAXParser(); 50 } catch (ParserConfigurationException e) { 51 UpdateCore.log(e); 52 } catch (SAXException e) { 53 UpdateCore.log(e); 54 } 55 } 56 57 public void init(FeatureModelFactory factory) { 58 init(factory, null); 59 } 60 61 66 public void init(FeatureModelFactory factory, String location) { 67 68 this.factory = factory; 69 this.location = location; 70 factory = new LiteFeatureFactory(); 71 featureModels = new ArrayList (); 72 featureParser.internalInit(factory, location); 73 } 74 75 85 public LiteFeature[] parse(InputStream in) throws SAXException , IOException { 86 87 parser.parse(new InputSource (in), this); 88 return (LiteFeature[])featureModels.toArray( new LiteFeature[featureModels.size()]); 89 } 90 91 92 98 public MultiStatus getStatus() { 99 return featureParser.getStatus(); 100 } 101 102 107 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 108 if(localName.equals("digest")) return; 110 if(localName.equals("feature")) featureParser.internalInit(factory, location); 112 113 featureParser.startElement(uri, localName, qName, attributes); 114 } 115 116 121 public void endElement(String uri, String localName, String qName) { 122 if(localName.equals("digest")) return; 124 featureParser.endElement(uri, localName, qName); 125 if(localName.equals("feature")) { try { 127 featureModels.add(featureParser.getFeatureModel()); 128 } catch (SAXException e) { 129 e.printStackTrace(); 130 } 131 } 132 } 133 134 135 140 public void characters(char[] ch, int start, int length) { 141 featureParser.characters(ch, start, length); 142 } 143 144 149 public void error(SAXParseException ex) { 150 featureParser.error(ex); 151 } 152 153 159 public void fatalError(SAXParseException ex) throws SAXException { 160 featureParser.fatalError(ex); 161 } 162 163 166 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException { 167 featureParser.ignorableWhitespace(arg0, arg1, arg2); 168 } 169 } 170 | Popular Tags |