KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > xml > XmlPage


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.xml;
39
40 import java.io.IOException JavaDoc;
41 import java.io.StringReader JavaDoc;
42
43 import javax.xml.parsers.DocumentBuilder JavaDoc;
44 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
45 import javax.xml.parsers.ParserConfigurationException JavaDoc;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import org.w3c.dom.Document JavaDoc;
50 import org.xml.sax.ErrorHandler JavaDoc;
51 import org.xml.sax.InputSource JavaDoc;
52 import org.xml.sax.SAXException JavaDoc;
53 import org.xml.sax.SAXParseException JavaDoc;
54
55 import com.gargoylesoftware.htmlunit.Page;
56 import com.gargoylesoftware.htmlunit.WebResponse;
57 import com.gargoylesoftware.htmlunit.WebWindow;
58
59 /**
60  * A page that will be returned for response with content type "text/xml".
61  * It doesn't implement itself {@link org.w3c.dom.Document} to allow to see the source of badly formed
62  * xml responses.
63  * @version $Revision: 100 $
64  * @author Marc Guillemot
65  */

66 public class XmlPage implements Page {
67     private final String JavaDoc content_;
68     private Document JavaDoc document_;
69
70     private WebWindow enclosingWindow_;
71     private final WebResponse webResponse_;
72     private static final ErrorHandler JavaDoc DISCARD_MESSAGES_HANDLER = new ErrorHandler JavaDoc() {
73         /**
74          * Does nothing as we're not interested in.
75          * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
76          */

77         public void error(final SAXParseException JavaDoc exception) throws SAXException JavaDoc {
78             // Does nothing as we're not interested in.
79
}
80         /**
81          * Does nothing as we're not interested in.
82          * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
83          */

84         public void fatalError(final SAXParseException JavaDoc exception)
85                 throws SAXException JavaDoc {
86             // Does nothing as we're not interested in.
87
}
88         /**
89          * Does nothing as we're not interested in.
90          * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
91          */

92         public void warning(final SAXParseException JavaDoc exception)
93                 throws SAXException JavaDoc {
94             // Does nothing as we're not interested in.
95
}
96     };
97
98
99     /**
100      * Create an instance.
101      * A warning is logged if an exception is thrown while parsing the xml content
102      * (for instance when the content is not a valid xml and can't be parsed).
103      *
104      * @param webResponse The response from the server
105      * @param enclosingWindow The window that holds the page.
106      * @throws IOException If the page could not be created
107      */

108     public XmlPage( final WebResponse webResponse, final WebWindow enclosingWindow ) throws IOException JavaDoc {
109         webResponse_ = webResponse;
110         content_ = webResponse.getContentAsString();
111         enclosingWindow_ = enclosingWindow;
112
113         final DocumentBuilderFactory JavaDoc factory =
114             DocumentBuilderFactory.newInstance();
115         final InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content_));
116         try {
117             final DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
118             builder.setErrorHandler(DISCARD_MESSAGES_HANDLER);
119             document_ = builder.parse(source);
120         }
121         catch (final SAXException JavaDoc e) {
122             getLog().warn("Failed parsing xml document " + webResponse.getUrl() + ": " + e.getMessage());
123         }
124         catch (final ParserConfigurationException JavaDoc e) {
125             getLog().warn("Failed parsing xml document " + webResponse.getUrl() + ": " + e.getMessage());
126         }
127
128     }
129
130
131     /**
132      * Clean up this page.
133      */

134     public void cleanUp() {
135     }
136
137
138     /**
139      * Return the content of the page
140      *
141      * @return See above
142      */

143     public String JavaDoc getContent() {
144         return content_;
145     }
146
147
148     /**
149      * Return the window that this page is sitting inside.
150      *
151      * @return The enclosing frame or null if this page isn't inside a frame.
152      */

153     public WebWindow getEnclosingWindow() {
154         return enclosingWindow_;
155     }
156
157     /**
158      * Return the log object for this web client
159      * @return The log object
160      */

161     protected final Log getLog() {
162         return LogFactory.getLog(getClass());
163     }
164
165
166     /**
167      * Return the web response that was originally used to create this page.
168      *
169      * @return The web response
170      */

171     public WebResponse getWebResponse() {
172         return webResponse_;
173     }
174
175     /**
176      * Gets the DOM representation of the xml content
177      * @return <code>null</code> if the content couldn't be parsed.
178      */

179     public Document JavaDoc getXmlDocument() {
180         return document_;
181     }
182
183     /**
184      * Initialize this page.
185      */

186     public void initialize() {
187     }
188 }
189
190
Popular Tags