KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > DefaultPageCreator


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;
39
40 import java.io.IOException JavaDoc;
41
42 import com.gargoylesoftware.htmlunit.html.HTMLParser;
43 import com.gargoylesoftware.htmlunit.html.HtmlPage;
44 import com.gargoylesoftware.htmlunit.xml.XmlPage;
45
46 /**
47  * The default implementation of PageCreator. Designed to be extented for easier
48  * handling of new content types. Just check the content type in createPage()
49  * and call super(createPage()) if your custom type isn't found. There are
50  * also protected createXXXXPage() methods for creating the Page types HtmlUnit
51  * already knows about for your custom content types.
52  *
53  * @version $Revision: 100 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
56  * @author <a HREF="mailto:yourgod@users.sourceforge.net">Brad Clarke</a>
57  * @author Marc Guillemot
58  */

59 public class DefaultPageCreator implements PageCreator {
60
61     /**
62      * Create an instance.
63      */

64     public DefaultPageCreator() {
65     }
66
67     /**
68      * Create a Page object for the specified web response.
69      *
70      * @param webResponse The response from the server
71      * @param webWindow The window that this page will be loaded into.
72      * @exception IOException If an io problem occurs
73      * @return The new page object
74      */

75     public Page createPage(
76             final WebResponse webResponse,
77             final WebWindow webWindow )
78         throws
79             IOException JavaDoc {
80         final String JavaDoc contentType = webResponse.getContentType();
81         final Page newPage;
82         
83         if( contentType.equals( "text/html" ) || contentType.equals( "text/xhtml" ) ) {
84             newPage = createHtmlPage(webResponse, webWindow);
85         }
86         else if( contentType.equals("application/xhtml+xml") ) {
87             //Should create a validated XML document but for now just make what we can
88
newPage = createHtmlPage(webResponse, webWindow);
89         }
90         else if( contentType.equals("text/javascript") || contentType.equals("application/x-javascript") ) {
91             newPage = createJavaScriptPage(webResponse, webWindow);
92         }
93         else if (contentType.equals("text/xml") || contentType.equals("application/xml")) {
94             newPage = createXmlPage(webResponse, webWindow);
95         }
96         else if( contentType.startsWith( "text/" ) ) {
97             newPage = createTextPage(webResponse, webWindow);
98         }
99         else {
100             newPage = createUnexpectedPage(webResponse, webWindow);
101         }
102         return newPage;
103
104     }
105     /**
106      * Create a HtmlPage for this WebResponse
107      *
108      * @param webResponse The page's source
109      * @param webWindow The WebWindow to place the HtmlPage in
110      * @return The newly created HtmlPage
111      * @throws IOException If the page could not be created
112      */

113     protected HtmlPage createHtmlPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException JavaDoc {
114         final HtmlPage newPage;
115         newPage = HTMLParser.parse(webResponse, webWindow);
116         return newPage;
117     }
118     /**
119      * Create a JavaScriptPage for this WebResponse
120      *
121      * @param webResponse The page's source
122      * @param webWindow The WebWindow to place the JavaScriptPage in
123      * @return The newly created JavaScriptPage
124      */

125     protected JavaScriptPage createJavaScriptPage(final WebResponse webResponse, final WebWindow webWindow) {
126         final JavaScriptPage newPage;
127         newPage = new JavaScriptPage( webResponse, webWindow );
128         webWindow.setEnclosedPage(newPage);
129         return newPage;
130     }
131
132     /**
133      * Create a TextPage for this WebResponse
134      *
135      * @param webResponse The page's source
136      * @param webWindow The WebWindow to place the TextPage in
137      * @return The newly created TextPage
138      */

139     protected TextPage createTextPage(final WebResponse webResponse, final WebWindow webWindow) {
140         final TextPage newPage;
141         newPage = new TextPage( webResponse, webWindow );
142         webWindow.setEnclosedPage(newPage);
143         return newPage;
144     }
145
146     /**
147      * Create an UnexpectedPage for this WebResponse
148      *
149      * @param webResponse The page's source
150      * @param webWindow The WebWindow to place the UnexpectedPage in
151      * @return The newly created UnexpectedPage
152      */

153     protected UnexpectedPage createUnexpectedPage(final WebResponse webResponse, final WebWindow webWindow) {
154         final UnexpectedPage newPage;
155         newPage = new UnexpectedPage( webResponse, webWindow );
156         webWindow.setEnclosedPage(newPage);
157         return newPage;
158     }
159
160     /**
161      * Create an XmlPage for this WebResponse
162      *
163      * @param webResponse The page's source
164      * @param webWindow The WebWindow to place the TextPage in
165      * @return The newly created TextPage
166      * @throws IOException If the page could not be created
167      */

168     protected XmlPage createXmlPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException JavaDoc {
169         final XmlPage newPage = new XmlPage( webResponse, webWindow );
170         webWindow.setEnclosedPage(newPage);
171         return newPage;
172     }
173 }
174
Popular Tags