1 21 24 package org.lobobrowser.html.parser; 25 26 import java.io.*; 27 import javax.xml.parsers.DocumentBuilder ; 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 ; 34 import org.w3c.dom.Document ; 35 import org.xml.sax.EntityResolver ; 36 import org.xml.sax.ErrorHandler ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.SAXException ; 39 40 46 public class DocumentBuilderImpl extends DocumentBuilder { 47 private static final Logger logger = Logger.getLogger(DocumentBuilderImpl.class.getName()); 48 private EntityResolver resolver; 49 private ErrorHandler errorHandler; 50 private final UserAgentContext bcontext; 51 private final HtmlRendererContext rcontext; 52 53 57 public DocumentBuilderImpl(HtmlParserContext context) { 58 this.rcontext = null; 59 this.bcontext = context.getUserAgentContext(); 60 } 61 62 69 public DocumentBuilderImpl(UserAgentContext context) { 70 this.rcontext = null; 71 this.bcontext = context; 72 } 73 74 78 public DocumentBuilderImpl(HtmlParserContext context, HtmlRendererContext rcontext) { 79 this.rcontext = rcontext; 80 this.bcontext = context.getUserAgentContext(); 81 } 82 83 91 public DocumentBuilderImpl(UserAgentContext ucontext, HtmlRendererContext rcontext) { 92 this.rcontext = rcontext; 93 this.bcontext = ucontext; 94 } 95 96 101 public Document parse(InputSource is) throws org.xml.sax.SAXException , IOException { 102 HTMLDocumentImpl document = (HTMLDocumentImpl) this.createDocument(is); 103 document.load(); 104 return document; 105 } 106 107 113 public Document createDocument(InputSource is) throws SAXException , IOException { 114 String charset = is.getEncoding(); 115 if(charset == null) { 116 charset = "US-ASCII"; 117 } 118 String 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 ("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 142 public boolean isNamespaceAware() { 143 return false; 144 } 145 146 public boolean isValidating() { 147 return false; 148 } 149 150 public void setEntityResolver(EntityResolver er) { 151 this.resolver = er; 152 } 153 154 public void setErrorHandler(ErrorHandler eh) { 155 this.errorHandler = eh; 156 } 157 158 public Document newDocument() { 159 return new HTMLDocumentImpl(this.bcontext); 160 } 161 162 private DOMImplementation domImplementation; 163 164 public DOMImplementation 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 getErrorHandler() { 174 return errorHandler; 175 } 176 177 public EntityResolver getResolver() { 178 return resolver; 179 } 180 } 181 | Popular Tags |