View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.saxbenchmark.htmlparser;
17  
18  import java.io.IOException;
19  
20  import org.htmlparser.Parser;
21  import org.htmlparser.lexer.Lexer;
22  import org.htmlparser.lexer.Page;
23  import org.htmlparser.sax.Feedback;
24  import org.htmlparser.sax.Locator;
25  import org.htmlparser.sax.XMLReader;
26  import org.htmlparser.util.DefaultParserFeedback;
27  import org.htmlparser.util.NodeIterator;
28  import org.htmlparser.util.ParserException;
29  import org.xml.sax.InputSource;
30  import org.xml.sax.SAXException;
31  import org.xml.sax.SAXParseException;
32  
33  /***
34   * @author JMcCrindle
35   */
36  public class HTMLParserXMLReader extends XMLReader {
37      /*
38       * (non-Javadoc)
39       * 
40       * @see org.htmlparser.sax.XMLReader#parse(org.xml.sax.InputSource)
41       */
42      public void parse(InputSource inputSource) throws IOException, SAXException {
43          if (null != mContentHandler)
44              try {
45                  mParser = new Parser(new Lexer(new Page(inputSource
46                          .getByteStream(), inputSource.getEncoding())));
47                  Locator locator = new Locator(mParser);
48                  org.htmlparser.util.ParserFeedback feedback;
49                  if (null != mErrorHandler)
50                      feedback = new Feedback(mErrorHandler, locator);
51                  else
52                      feedback = new DefaultParserFeedback(0);
53                  mParser.setFeedback(feedback);
54                  mContentHandler.setDocumentLocator(locator);
55                  try {
56                      mContentHandler.startDocument();
57                      for (NodeIterator iterator = mParser.elements(); iterator
58                              .hasMoreNodes(); doSAX(iterator.nextNode()))
59                          ;
60                      mContentHandler.endDocument();
61                  } catch (SAXException se) {
62                      if (null != mErrorHandler)
63                          mErrorHandler.fatalError(new SAXParseException(
64                                  "contentHandler threw me", locator, se));
65                  }
66              } catch (ParserException pe) {
67                  if (null != mErrorHandler)
68                      mErrorHandler.fatalError(new SAXParseException(pe
69                              .getMessage(), "", "", 0, 0));
70              }
71      }
72  }