1 5 package xdoclet.ant.modulesbuilder; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 10 import javax.xml.parsers.ParserConfigurationException ; 11 import javax.xml.parsers.SAXParser ; 12 import javax.xml.parsers.SAXParserFactory ; 13 14 import org.xml.sax.Attributes ; 15 import org.xml.sax.EntityResolver ; 16 import org.xml.sax.SAXException ; 17 import org.xml.sax.XMLReader ; 18 import org.xml.sax.helpers.DefaultHandler ; 19 20 27 class ModuleXmlParser extends DefaultHandler 28 { 29 private final SAXParserFactory _factory; 30 private Module module; 31 private EntityResolver entityResolver = null; 32 33 public ModuleXmlParser() 34 { 35 _factory = SAXParserFactory.newInstance(); 36 _factory.setValidating(false); 37 } 38 39 public void setEntityResolver(EntityResolver entityResolver) 40 { 41 this.entityResolver = entityResolver; 42 } 43 44 public Module parse(InputStream in) 45 { 46 try { 47 SAXParser parser = _factory.newSAXParser(); 48 49 XMLReader reader = parser.getXMLReader(); 54 55 if (entityResolver != null) 56 reader.setEntityResolver(entityResolver); 57 reader.setContentHandler(this); 58 reader.parse(new org.xml.sax.InputSource (in)); 59 } 60 catch (IOException e) { 61 module = null; 62 e.printStackTrace(); 63 } 64 catch (IllegalArgumentException e) { 65 module = null; 66 e.printStackTrace(); 67 } 68 catch (ParserConfigurationException e) { 69 module = null; 70 e.printStackTrace(); 71 } 72 catch (SAXException e) { 73 module = null; 74 e.printStackTrace(); 75 } 76 return module; 77 } 78 79 public void startDocument() 80 { 81 module = new Module(); 82 } 83 84 public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) 85 { 86 if (qName.equals("module-dependency")) { 87 module.addDependency(attributes.getValue("module-name")); 88 } 89 } 90 } 91 | Popular Tags |