View Javadoc

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;
11  import java.net.MalformedURLException;
12  import java.util.*;
13  import org.xml.sax.*;
14  import org.xml.sax.helpers.*;
15  import hotsax.html.sax.helpers.EntityResolverImpl;
16  
17  /***
18   * SaxParser - lite SAX parser. Based only on 
19   * @author  edh
20   * @version 
21   */
22  public class SaxParser  implements org.xml.sax.XMLReader {
23  
24      private HtmlParser yyparser;
25      private EntityResolver entityResolver;
26      private DTDHandler dtdHandler;
27      private ContentHandler contentHandler;
28      private ErrorHandler errorHandler;
29  
30      private org.xml.sax.helpers.AttributesImpl 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  	/*** IO reader to use */
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 EntityResolverImpl();
54          dtdHandler = new DefaultHandler();
55          contentHandler = new DefaultHandler();
56          errorHandler = new DefaultHandler();
57          
58          attrList = new org.xml.sax.helpers.AttributesImpl(); 
59  
60  		yyparser = new HtmlParser();  // must set the reader before using
61      }
62  
63      
64      /***
65       * create a Reader to be used by the lexer based on the InputSource
66       * Such as 
67       */
68      protected Reader createReader(InputSource source)
69          throws IOException, MalformedURLException
70      {
71          // create reader from source's character stream
72          if (source.getCharacterStream() != null) {
73              return source.getCharacterStream();
74          }
75  
76          // create reader from source's byte stream
77          if (source.getEncoding() != null && source.getByteStream() != null) {
78              java.io.Reader reader = new InputStreamReader(source.getByteStream(), source.getEncoding());
79              return reader;
80          }
81  
82          // create new input stream
83          InputStream is = source.getByteStream();
84          if (is == null) {
85  
86              // create url and open the stream
87              URL url = new URL(source.getSystemId());
88              is = url.openStream();
89          }
90  
91          return new InputStreamReader(is);
92          
93      }
94          
95      
96      public org.xml.sax.ContentHandler getContentHandler() {
97          return contentHandler;
98      }
99      
100     public Object getProperty(String p1) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException {
101         return properties.get(p1);
102     }
103     
104     public void setFeature(String p1,boolean p2) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException {
105         Boolean bool = new Boolean(p2);
106         features.put(p1, bool);
107     }
108     
109     public void setEntityResolver(org.xml.sax.EntityResolver p1) {
110         entityResolver = p1;
111     }
112 
113     
114     public void setContentHandler(org.xml.sax.ContentHandler p1) {
115         contentHandler = p1;
116     }
117     
118     public void setDTDHandler(org.xml.sax.DTDHandler p1) {
119         dtdHandler = p1;
120     }
121     
122     public org.xml.sax.ErrorHandler getErrorHandler() {
123         return errorHandler;
124     }
125     
126     public org.xml.sax.EntityResolver getEntityResolver() {
127         return entityResolver;
128     }
129     
130     public void setErrorHandler(org.xml.sax.ErrorHandler p1) {
131         errorHandler = p1;
132     }
133     
134     public org.xml.sax.DTDHandler getDTDHandler() {
135         return dtdHandler;
136     }
137     
138     public void setProperty(String p1, Object p2) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException {
139         properties.put(p1, p2);
140     }
141     
142     public boolean getFeature(String p1) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException {
143         Boolean bool = (Boolean)features.get(p1);
144         
145         return bool.booleanValue();
146     }
147 
148 
149     /***
150      * Parser setup code. Initialize a new reader based on type of URI
151      * Call into actual parser below with newly created InputSource.
152      *   handles URIs of type "http://" and file://
153      *
154      * @param p1 URI to open
155      */
156     
157     public void parse(String p1) 
158         throws IOException, org.xml.sax.SAXException 
159     {
160         InputSource source = entityResolver.resolveEntity(p1,p1);
161         try {
162            if (source == null) { // entity resolver didn't do its job, try normal URL opening
163               source = new InputSource(p1);
164             
165               source.setCharacterStream(createReader(source)); 
166            }
167            parse(source);
168         }
169         catch (Exception ex)
170         {
171             throw new SAXException(ex);
172         }
173         finally {
174             // NOTE: Changed code to attempt to close the stream
175             //       even after parsing failure. -Ac
176             try {
177                 Reader reader = source.getCharacterStream();
178                 if (reader != null) { 
179                     reader.close();
180                 }
181                 else {
182                     InputStream is = source.getByteStream();
183                     if (is != null) {
184                         is.close();
185                     }
186                 }
187             }
188             catch (IOException e) {
189                 // ignore
190             }
191         }
192            
193     }
194     
195     
196     /***
197      * Parse the input document using the current InputSource's reader
198      */
199     
200     public void parse(org.xml.sax.InputSource p1) 
201         throws java.io.IOException, org.xml.sax.SAXException 
202     {
203         Boolean debugWrapper = (Boolean)properties.get("debug");
204         boolean debug = (debugWrapper == null ? false : debugWrapper.booleanValue());
205         
206         Reader reader = p1.getCharacterStream();
207         
208 		yyparser.setReader(reader);
209 		//yyparser = new HtmlParser(reader);
210 		ParserDelegate delegate = yyparser.getParserDelegate();
211         delegate.setXMLReader(this);
212 
213         if (debug) {
214             yyparser.yydebug = true;
215         }
216         yyparser.yyparse();    // fires off Content/Lexical handler events vis ParserDelegate
217         
218     }
219 
220     public void startDocument()
221         throws SAXException
222     {
223         if (contentHandler != null)
224             contentHandler.startDocument();
225     }        
226 
227     public void endDocument()
228         throws SAXException
229     {
230         if (contentHandler != null)
231             contentHandler.endDocument();
232     }        
233     
234     
235     
236     /***
237      * collect attributes into a list and call ContentHandler.startElement
238      **/
239     public void startElement(String name)
240         throws SAXException
241     {
242         if (contentHandler != null)
243             contentHandler.startElement("", name, "", (Attributes)attrList);
244     }
245     
246     public void endElement(String name)
247         throws SAXException
248     {
249         if (contentHandler != null)
250             contentHandler.endElement("", name, "");
251     }
252     
253 	// accessor methods
254 	
255 	/*** 
256 	 * Return the HtmlParser handle.
257 	 * @return the handle to yyparser.
258      */
259     public HtmlParser getyyParser() { return yyparser; }
260 }