KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > parser > DocumentBuilderImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 15, 2005
23  */

24 package org.lobobrowser.html.parser;
25
26 import java.io.*;
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import java.util.logging.*;
29
30 import org.lobobrowser.html.*;
31 import org.lobobrowser.html.domimpl.*;
32 import org.lobobrowser.html.io.*;
33 import org.w3c.dom.DOMImplementation JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.xml.sax.EntityResolver JavaDoc;
36 import org.xml.sax.ErrorHandler JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40 /**
41  * The <code>DocumentBuilderImpl</code> class is an HTML
42  * DOM parser that implements the
43  * standard W3C <code>DocumentBuilder</code> interface.
44  * @author J. H. S.
45  */

46 public class DocumentBuilderImpl extends DocumentBuilder JavaDoc {
47     private static final Logger logger = Logger.getLogger(DocumentBuilderImpl.class.getName());
48     private EntityResolver JavaDoc resolver;
49     private ErrorHandler JavaDoc errorHandler;
50     private final UserAgentContext bcontext;
51     private final HtmlRendererContext rcontext;
52
53     /**
54      * @deprecated HtmlParserContext is no longer used.
55      * @see #DocumentBuilderImpl(UserAgentContext)
56      */

57     public DocumentBuilderImpl(HtmlParserContext context) {
58         this.rcontext = null;
59         this.bcontext = context.getUserAgentContext();
60     }
61
62     /**
63      * Constructs a <code>DocumentBuilderImpl</code>. This constructor
64      * should be used when only the parsing functionality (without rendering)
65      * is required.
66      * @param context An instance of {@link org.lobobrowser.html.UserAgentContext},
67      * which may be an instance of {@link org.lobobrowser.html.test.SimpleUserAgentContext}.
68      */

69     public DocumentBuilderImpl(UserAgentContext context) {
70         this.rcontext = null;
71         this.bcontext = context;
72     }
73
74     /**
75      * @deprecated HtmlParserContext is no longer used.
76      * @see #DocumentBuilderImpl(UserAgentContext,HtmlRendererContext)
77      */

78     public DocumentBuilderImpl(HtmlParserContext context, HtmlRendererContext rcontext) {
79         this.rcontext = rcontext;
80         this.bcontext = context.getUserAgentContext();
81     }
82     
83     /**
84      * Constructs a <code>DocumentBuilderImpl</code>. This constructor
85      * should be used when rendering is expected.
86      * @param ucontext An instance of {@link org.lobobrowser.html.UserAgentContext},
87      * which may be an instance of {@link org.lobobrowser.html.test.SimpleUserAgentContext}.
88      * @param rcontext An instance of {@link org.lobobrowser.html.HtmlRendererContext},
89      * which may be an instance of {@link org.lobobrowser.html.test.SimpleHtmlRendererContext}.
90      */

91     public DocumentBuilderImpl(UserAgentContext ucontext, HtmlRendererContext rcontext) {
92         this.rcontext = rcontext;
93         this.bcontext = ucontext;
94     }
95
96     /**
97      * Parser an HTML document given as an <code>InputSource</code>.
98      * @param is The input source, which may be an instance of
99      * {@link org.lobobrowser.html.parser.InputSourceImpl}.
100      */

101     public Document JavaDoc parse(InputSource JavaDoc is) throws org.xml.sax.SAXException JavaDoc, IOException {
102         HTMLDocumentImpl document = (HTMLDocumentImpl) this.createDocument(is);
103         document.load();
104         return document;
105     }
106
107     /**
108      * Creates a document without parsing it so
109      * it can be used for incremental rendering.
110      * @param is The input source, which may be an instance of
111      * {@link org.lobobrowser.html.parser.InputSourceImpl}.
112      */

113     public Document JavaDoc createDocument(InputSource JavaDoc is) throws SAXException JavaDoc, IOException {
114         String JavaDoc charset = is.getEncoding();
115         if(charset == null) {
116             charset = "US-ASCII";
117         }
118         String JavaDoc uri = is.getSystemId();
119         if(uri == null) {
120             logger.warning("parse(): InputSource has no SystemId (URI); document item URLs will not be resolvable.");
121         }
122         InputStream in = is.getByteStream();
123         WritableLineReader wis;
124         if(in != null) {
125             wis = new WritableLineReader(new InputStreamReader(in, charset));
126         }
127         else {
128             Reader reader = is.getCharacterStream();
129             if(reader != null) {
130                 wis = new WritableLineReader(reader);
131             }
132             else {
133                 throw new IllegalArgumentException JavaDoc("InputSource has neither a byte stream nor a character stream");
134             }
135         }
136         HTMLDocumentImpl document = new HTMLDocumentImpl(this.bcontext, this.rcontext, wis, uri);
137         return document;
138     }
139     
140     //TODO: parseAsync
141

142     public boolean isNamespaceAware() {
143         return false;
144     }
145
146     public boolean isValidating() {
147         return false;
148     }
149
150     public void setEntityResolver(EntityResolver JavaDoc er) {
151         this.resolver = er;
152     }
153
154     public void setErrorHandler(ErrorHandler JavaDoc eh) {
155         this.errorHandler = eh;
156     }
157
158     public Document JavaDoc newDocument() {
159         return new HTMLDocumentImpl(this.bcontext);
160     }
161
162     private DOMImplementation JavaDoc domImplementation;
163     
164     public DOMImplementation JavaDoc getDOMImplementation() {
165         synchronized(this) {
166             if(this.domImplementation == null) {
167                 this.domImplementation = new DOMImplementationImpl(this.bcontext);
168             }
169             return this.domImplementation;
170         }
171     }
172
173     public ErrorHandler JavaDoc getErrorHandler() {
174         return errorHandler;
175     }
176
177     public EntityResolver JavaDoc getResolver() {
178         return resolver;
179     }
180 }
181
Popular Tags