KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Location


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.javascript.host;
39
40 import com.gargoylesoftware.htmlunit.Page;
41 import com.gargoylesoftware.htmlunit.WebWindow;
42 import com.gargoylesoftware.htmlunit.html.HtmlPage;
43 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
44
45 import java.io.IOException JavaDoc;
46 import java.net.URL JavaDoc;
47
48 /**
49  * A javascript object for a Location
50  *
51  * @version $Revision: 100 $
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  * @author Michael Ottati
54  * @author Marc Guillemot
55  * @author Chris Erskine
56  */

57 public class Location extends SimpleScriptable {
58     private static final long serialVersionUID = -2907220432378132233L;
59     private static final String JavaDoc UNKNOWN = "Unknown";
60     private Window window_;
61
62     /**
63      * Create an instance. Javascript objects must have a default constructor.
64      */

65     public Location() {
66     }
67
68
69     /**
70      * Initialize the object
71      * @param window The window that this location belongs to.
72      */

73     public void initialize( final Window window ) {
74         window_ = window;
75     }
76
77
78     /**
79      * Set the "href" property
80      * @param href The new location
81      * @throws IOException when location loading fails
82      */

83     public void jsxSet_href( final String JavaDoc href ) throws IOException JavaDoc {
84         window_.jsxSet_location(href);
85     }
86
87
88     /**
89      * Return the value of the href property;
90      * @return the value of the href property
91      */

92     public String JavaDoc jsxGet_href() {
93         final Page page = window_.getWebWindow().getEnclosedPage();
94         if( page == null ) {
95             return UNKNOWN;
96         }
97         else {
98             return page.getWebResponse().getUrl().toExternalForm();
99         }
100     }
101
102
103     /**
104      * Reloads the current page, possibly forcing retrieval from the server even if
105      * the browser cache contains the latest version of the document.
106      * @param force If <tt>true</tt>, force reload from server; otherwise, may reload from cache.
107      * @throws IOException When an exception occurs reloading the page.
108      */

109     public void jsxFunction_reload( final boolean force ) throws IOException JavaDoc {
110         String JavaDoc url = jsxGet_href();
111         if( UNKNOWN.equals( url ) ) {
112             getLog().error( "Unable to reload location: current url is unknown." );
113         }
114         else {
115             jsxSet_href( url );
116         }
117     }
118
119
120     /**
121      * Reload the window with the specified url
122      * @param href The new url
123      * @throws IOException when exception occurs loading the new page
124      */

125     public void jsxFunction_replace( final String JavaDoc href ) throws IOException JavaDoc {
126         final WebWindow webWindow = window_.getWebWindow();
127         final URL JavaDoc url = ((HtmlPage) webWindow.getEnclosedPage()).getFullyQualifiedUrl( href );
128         webWindow.getWebClient().getPage(url);
129     }
130
131
132     private URL JavaDoc getUrl() {
133         return window_.getWebWindow().getEnclosedPage().getWebResponse().getUrl();
134     }
135
136     /**
137      * Return the hostname that is part of the location url
138      * @return The hostname
139      */

140     public String JavaDoc jsxGet_hostname() {
141         return getUrl().getHost();
142     }
143
144
145     /**
146      * Return the string value of the location, which is the full URL.
147      * @return The string URL
148      */

149     public String JavaDoc toString() {
150         return jsxGet_href();
151     }
152
153     /**
154      * Return the search string
155      * @return The value.
156      */

157     public String JavaDoc jsxGet_search() {
158         final String JavaDoc search = getUrl().getQuery();
159         if( search == null ) {
160             return "";
161         }
162         else {
163             return "?"+search;
164         }
165     }
166
167     /**
168      * Return the value of "hash"
169      * @return The value.
170      */

171     public String JavaDoc jsxGet_hash() {
172         final String JavaDoc hash = getUrl().getRef();
173         if( hash == null ) {
174             return "";
175         }
176         else {
177             return hash;
178         }
179     }
180
181     /**
182      * Return the value of "host"
183      * @return The value.
184      */

185     public String JavaDoc jsxGet_host() {
186         final URL JavaDoc url = getUrl();
187         final int port = url.getPort();
188         final String JavaDoc host = url.getHost();
189
190         if( port == - 1 ) {
191             return host;
192         }
193         else {
194             return host+":"+port;
195         }
196     }
197
198     /**
199      * Return the value of "pathname"
200      * @return The value.
201      */

202     public String JavaDoc jsxGet_pathname() {
203         return getUrl().getPath();
204     }
205
206     /**
207      * Return the value of "port"
208      * @return The value.
209      */

210     public String JavaDoc jsxGet_port() {
211         final int port = getUrl().getPort();
212         if( port == -1 ) {
213             return "";
214         }
215         else {
216             return String.valueOf(port);
217         }
218     }
219
220     /**
221      * Return the value of "protocol" + ":" like what browser do
222      * @return The value.
223      */

224     public String JavaDoc jsxGet_protocol() {
225         return getUrl().getProtocol() + ":";
226     }
227
228 }
229
230
Popular Tags