1 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 ; 46 import java.net.URL ; 47 48 57 public class Location extends SimpleScriptable { 58 private static final long serialVersionUID = -2907220432378132233L; 59 private static final String UNKNOWN = "Unknown"; 60 private Window window_; 61 62 65 public Location() { 66 } 67 68 69 73 public void initialize( final Window window ) { 74 window_ = window; 75 } 76 77 78 83 public void jsxSet_href( final String href ) throws IOException { 84 window_.jsxSet_location(href); 85 } 86 87 88 92 public String 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 109 public void jsxFunction_reload( final boolean force ) throws IOException { 110 String 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 125 public void jsxFunction_replace( final String href ) throws IOException { 126 final WebWindow webWindow = window_.getWebWindow(); 127 final URL url = ((HtmlPage) webWindow.getEnclosedPage()).getFullyQualifiedUrl( href ); 128 webWindow.getWebClient().getPage(url); 129 } 130 131 132 private URL getUrl() { 133 return window_.getWebWindow().getEnclosedPage().getWebResponse().getUrl(); 134 } 135 136 140 public String jsxGet_hostname() { 141 return getUrl().getHost(); 142 } 143 144 145 149 public String toString() { 150 return jsxGet_href(); 151 } 152 153 157 public String jsxGet_search() { 158 final String search = getUrl().getQuery(); 159 if( search == null ) { 160 return ""; 161 } 162 else { 163 return "?"+search; 164 } 165 } 166 167 171 public String jsxGet_hash() { 172 final String hash = getUrl().getRef(); 173 if( hash == null ) { 174 return ""; 175 } 176 else { 177 return hash; 178 } 179 } 180 181 185 public String jsxGet_host() { 186 final URL url = getUrl(); 187 final int port = url.getPort(); 188 final String host = url.getHost(); 189 190 if( port == - 1 ) { 191 return host; 192 } 193 else { 194 return host+":"+port; 195 } 196 } 197 198 202 public String jsxGet_pathname() { 203 return getUrl().getPath(); 204 } 205 206 210 public String 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 224 public String jsxGet_protocol() { 225 return getUrl().getProtocol() + ":"; 226 } 227 228 } 229 230 | Popular Tags |