KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hotsax > html > sax > SaxParser


1 /*
2  * SaxParser.java
3  *
4  * Created on May 20, 2001, 12:26 AM
5  */

6
7 package hotsax.html.sax;
8
9 import java.io.*;
10 import java.net.URL JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.util.*;
13 import org.xml.sax.*;
14 import org.xml.sax.helpers.*;
15 import org.apache.xerces.utils.*;
16 import org.apache.xerces.readers.*;
17
18 /**
19  * SaxParser - lite SAX parser. Based only on
20  * @author edh
21  * @version
22  */

23 public class SaxParser implements org.xml.sax.XMLReader JavaDoc {
24
25     private EntityResolver entityResolver;
26     private DTDHandler dtdHandler;
27     private ContentHandler contentHandler;
28     private ErrorHandler errorHandler;
29
30     private org.xml.sax.helpers.AttributesImpl JavaDoc attrList; // collect attributes in a list
31

32     //private EntityHandler entityHandler;
33

34     /** properties are set but ignored */
35     private HashMap properties;
36     
37     /** features are set but ignored */
38     private HashMap features;
39     
40     /** lexer interface */
41     private HtmlLexer lexer;
42     
43     
44     private Reader reader;
45     
46     /** Creates new SaxParser */
47     public SaxParser() {
48         properties = new HashMap();
49         features = new HashMap();
50         //entityHandler = new DefaultEntityHandler(new StringPool(), null);
51
//entityHandler.setReaderFactory(new DefaultReaderFactory());
52

53         entityResolver = new DefaultHandler();
54         dtdHandler = new DefaultHandler();
55         contentHandler = new DefaultHandler();
56         errorHandler = new DefaultHandler();
57         
58         attrList = new org.xml.sax.helpers.AttributesImpl JavaDoc();
59     }
60
61     
62     /**
63      * create a Reader to be used by the lexer based on the InputSource
64      */

65     protected Reader createReader(InputSource source)
66         throws IOException, MalformedURLException JavaDoc
67     {
68         // create reader from source's character stream
69
if (source.getCharacterStream() != null) {
70             return source.getCharacterStream();
71         }
72
73         // create reader from source's byte stream
74
if (source.getEncoding() != null && source.getByteStream() != null) {
75             java.io.Reader JavaDoc reader = new InputStreamReader(source.getByteStream(), source.getEncoding());
76             return reader;
77         }
78
79         // create new input stream
80
InputStream is = source.getByteStream();
81         if (is == null) {
82
83             // create url and open the stream
84
URL JavaDoc url = new URL JavaDoc(source.getSystemId());
85             is = url.openStream();
86         }
87
88         return new InputStreamReader(is);
89         
90     }
91         
92     
93     public org.xml.sax.ContentHandler JavaDoc getContentHandler() {
94         return contentHandler;
95     }
96     
97     public Object JavaDoc getProperty(String JavaDoc p1) throws org.xml.sax.SAXNotRecognizedException JavaDoc, org.xml.sax.SAXNotSupportedException JavaDoc {
98         return properties.get(p1);
99     }
100     
101     public void setFeature(String JavaDoc p1,boolean p2) throws org.xml.sax.SAXNotRecognizedException JavaDoc, org.xml.sax.SAXNotSupportedException JavaDoc {
102         Boolean JavaDoc bool = new Boolean JavaDoc(p2);
103         features.put(p1, bool);
104     }
105     
106     public void setEntityResolver(org.xml.sax.EntityResolver JavaDoc p1) {
107         entityResolver = p1;
108     }
109
110     
111     public void setContentHandler(org.xml.sax.ContentHandler JavaDoc p1) {
112         contentHandler = p1;
113     }
114     
115     public void setDTDHandler(org.xml.sax.DTDHandler JavaDoc p1) {
116         dtdHandler = p1;
117     }
118     
119     public org.xml.sax.ErrorHandler JavaDoc getErrorHandler() {
120         return errorHandler;
121     }
122     
123     public org.xml.sax.EntityResolver JavaDoc getEntityResolver() {
124         return entityResolver;
125     }
126     
127     public void setErrorHandler(org.xml.sax.ErrorHandler JavaDoc p1) {
128         errorHandler = p1;
129     }
130     
131     public org.xml.sax.DTDHandler JavaDoc getDTDHandler() {
132         return dtdHandler;
133     }
134     
135     public void setProperty(String JavaDoc p1, Object JavaDoc p2) throws org.xml.sax.SAXNotRecognizedException JavaDoc, org.xml.sax.SAXNotSupportedException JavaDoc {
136         properties.put(p1, p2);
137     }
138     
139     public boolean getFeature(String JavaDoc p1) throws org.xml.sax.SAXNotRecognizedException JavaDoc, org.xml.sax.SAXNotSupportedException JavaDoc {
140         Boolean JavaDoc bool = (Boolean JavaDoc)features.get(p1);
141         
142         return bool.booleanValue();
143     }
144
145
146     /**
147      * Parser setup code. Initialize a new reader based on type of URI
148      * Call into actual parser below with newly created InputSource
149      */

150     
151     public void parse(String JavaDoc p1)
152         throws IOException, org.xml.sax.SAXException JavaDoc
153     {
154         InputSource source = entityResolver.resolveEntity(p1,p1);
155         try {
156            source = new InputSource(p1);
157             
158            source.setCharacterStream(createReader(source));
159
160            parse(source);
161         }
162         catch (Exception JavaDoc ex)
163         {
164             System.err.println("caught exception parsing " + ex.getClass().getName() + " " + ex.getMessage());
165             ex.printStackTrace();
166         }
167         finally {
168             // NOTE: Changed code to attempt to close the stream
169
// even after parsing failure. -Ac
170
try {
171                 Reader reader = source.getCharacterStream();
172                 if (reader != null) {
173                     reader.close();
174                 }
175                 else {
176                     InputStream is = source.getByteStream();
177                     if (is != null) {
178                         is.close();
179                     }
180                 }
181             }
182             catch (IOException e) {
183                 // ignore
184
}
185         }
186            
187     }
188     
189     
190     /**
191      * Parse the input document using the current InputSource's reader
192      */

193     
194     public void parse(org.xml.sax.InputSource JavaDoc p1)
195         throws java.io.IOException JavaDoc, org.xml.sax.SAXException JavaDoc
196     {
197         Boolean JavaDoc debugWrapper = (Boolean JavaDoc)properties.get("debug");
198         boolean debug = (debugWrapper == null ? false : debugWrapper.booleanValue());
199         HtmlParser yyparser;
200         
201         Reader reader = p1.getCharacterStream();
202         
203         yyparser = new HtmlParser(reader);
204
205         ParserDelegate delegate = yyparser.getDelegate();
206         delegate.setSaxParser(this);
207         
208         yyparser.yyparse(); // fires off Content/Lexical handler events vis ParserDelegate
209

210     }
211
212     public void startDocument()
213         throws SAXException
214     {
215         if (contentHandler != null)
216             contentHandler.startDocument();
217     }
218
219     public void endDocument()
220         throws SAXException
221     {
222         if (contentHandler != null)
223             contentHandler.endDocument();
224     }
225     
226     
227     
228     /**
229      * collect attributes into a list and call ContentHandler.startElement
230      **/

231     public void startElement(String JavaDoc name)
232         throws SAXException
233     {
234         if (contentHandler != null)
235             contentHandler.startElement("", name, "", (Attributes)attrList);
236     }
237     
238     public void endElement(String JavaDoc name)
239         throws SAXException
240     {
241         if (contentHandler != null)
242             contentHandler.endElement("", name, "");
243     }
244     
245     
246 }
247
Popular Tags