1 38 package com.gargoylesoftware.htmlunit.xml; 39 40 import java.io.IOException ; 41 import java.io.StringReader ; 42 43 import javax.xml.parsers.DocumentBuilder ; 44 import javax.xml.parsers.DocumentBuilderFactory ; 45 import javax.xml.parsers.ParserConfigurationException ; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.w3c.dom.Document ; 50 import org.xml.sax.ErrorHandler ; 51 import org.xml.sax.InputSource ; 52 import org.xml.sax.SAXException ; 53 import org.xml.sax.SAXParseException ; 54 55 import com.gargoylesoftware.htmlunit.Page; 56 import com.gargoylesoftware.htmlunit.WebResponse; 57 import com.gargoylesoftware.htmlunit.WebWindow; 58 59 66 public class XmlPage implements Page { 67 private final String content_; 68 private Document document_; 69 70 private WebWindow enclosingWindow_; 71 private final WebResponse webResponse_; 72 private static final ErrorHandler DISCARD_MESSAGES_HANDLER = new ErrorHandler () { 73 77 public void error(final SAXParseException exception) throws SAXException { 78 } 80 84 public void fatalError(final SAXParseException exception) 85 throws SAXException { 86 } 88 92 public void warning(final SAXParseException exception) 93 throws SAXException { 94 } 96 }; 97 98 99 108 public XmlPage( final WebResponse webResponse, final WebWindow enclosingWindow ) throws IOException { 109 webResponse_ = webResponse; 110 content_ = webResponse.getContentAsString(); 111 enclosingWindow_ = enclosingWindow; 112 113 final DocumentBuilderFactory factory = 114 DocumentBuilderFactory.newInstance(); 115 final InputSource source = new InputSource (new StringReader (content_)); 116 try { 117 final DocumentBuilder builder = factory.newDocumentBuilder(); 118 builder.setErrorHandler(DISCARD_MESSAGES_HANDLER); 119 document_ = builder.parse(source); 120 } 121 catch (final SAXException e) { 122 getLog().warn("Failed parsing xml document " + webResponse.getUrl() + ": " + e.getMessage()); 123 } 124 catch (final ParserConfigurationException e) { 125 getLog().warn("Failed parsing xml document " + webResponse.getUrl() + ": " + e.getMessage()); 126 } 127 128 } 129 130 131 134 public void cleanUp() { 135 } 136 137 138 143 public String getContent() { 144 return content_; 145 } 146 147 148 153 public WebWindow getEnclosingWindow() { 154 return enclosingWindow_; 155 } 156 157 161 protected final Log getLog() { 162 return LogFactory.getLog(getClass()); 163 } 164 165 166 171 public WebResponse getWebResponse() { 172 return webResponse_; 173 } 174 175 179 public Document getXmlDocument() { 180 return document_; 181 } 182 183 186 public void initialize() { 187 } 188 } 189 190 | Popular Tags |