KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > ant > modulesbuilder > ModuleXmlParser


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.ant.modulesbuilder;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9
10 import javax.xml.parsers.ParserConfigurationException JavaDoc;
11 import javax.xml.parsers.SAXParser JavaDoc;
12 import javax.xml.parsers.SAXParserFactory JavaDoc;
13
14 import org.xml.sax.Attributes JavaDoc;
15 import org.xml.sax.EntityResolver JavaDoc;
16 import org.xml.sax.SAXException JavaDoc;
17 import org.xml.sax.XMLReader JavaDoc;
18 import org.xml.sax.helpers.DefaultHandler JavaDoc;
19
20 /**
21  * Parses module.xml module descriptor file.
22  *
23  * @author Ara Abrahamian (ara_e_w@yahoo.com)
24  * @created Jun 10, 2002
25  * @version $Revision: 1.2 $
26  */

27 class ModuleXmlParser extends DefaultHandler JavaDoc
28 {
29     private final SAXParserFactory JavaDoc _factory;
30     private Module module;
31     private EntityResolver JavaDoc entityResolver = null;
32
33     public ModuleXmlParser()
34     {
35         _factory = SAXParserFactory.newInstance();
36         _factory.setValidating(false);
37     }
38
39     public void setEntityResolver(EntityResolver JavaDoc entityResolver)
40     {
41         this.entityResolver = entityResolver;
42     }
43
44     public Module parse(InputStream JavaDoc in)
45     {
46         try {
47             SAXParser JavaDoc parser = _factory.newSAXParser();
48
49             // NB this has to use the reader directly; when I tried to use
50
// parser.getXMLReader().setEntityResolver(entityResolver);
51
// it seemed to ignore the resolver in the subsequent
52
// parser.parse(in, this);
53
XMLReader JavaDoc reader = parser.getXMLReader();
54
55             if (entityResolver != null)
56                 reader.setEntityResolver(entityResolver);
57             reader.setContentHandler(this);
58             reader.parse(new org.xml.sax.InputSource JavaDoc(in));
59         }
60         catch (IOException JavaDoc e) {
61             module = null;
62             e.printStackTrace();
63         }
64         catch (IllegalArgumentException JavaDoc e) {
65             module = null;
66             e.printStackTrace();
67         }
68         catch (ParserConfigurationException JavaDoc e) {
69             module = null;
70             e.printStackTrace();
71         }
72         catch (SAXException JavaDoc 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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
85     {
86         if (qName.equals("module-dependency")) {
87             module.addDependency(attributes.getValue("module-name"));
88         }
89     }
90 }
91
Popular Tags