1 package org.lobobrowser.html.js; 2 3 import org.lobobrowser.html.*; 4 import org.lobobrowser.html.domimpl.*; 5 import org.lobobrowser.js.*; 6 import org.w3c.dom.Document ; 7 import java.net.*; 8 import java.util.logging.*; 9 10 public class Location extends AbstractScriptableDelegate { 11 private static final Logger logger = Logger.getLogger(Location.class.getName()); 12 private final Window window; 13 14 public Location(final Window window) { 15 super(); 16 this.window = window; 17 } 18 19 private URL getURL() { 20 URL url; 21 try { 22 Document document = this.window.getDocument(); 23 url = document == null ? null : new URL(document.getDocumentURI()); 24 } catch(java.net.MalformedURLException mfu) { 25 url = null; 26 } 27 return url; 28 } 29 30 public String getHash() { 31 URL url = this.getURL(); 32 return url == null ? null : url.getRef(); 33 } 34 35 public String getHost() { 36 URL url = this.getURL(); 37 if(url == null) { 38 return null; 39 } 40 return url.getHost() + (url.getPort() == -1 ? "" : ":" + url.getPort()); 41 } 42 43 public String getHostname() { 44 URL url = this.getURL(); 45 if(url == null) { 46 return null; 47 } 48 return url.getHost(); 49 } 50 51 public String getPathname() { 52 URL url = this.getURL(); 53 return url == null ? null : url.getPath(); 54 } 55 56 public String getPort() { 57 URL url = this.getURL(); 58 if(url == null) { 59 return null; 60 } 61 int port = url.getPort(); 62 return port == -1 ? null : String.valueOf(port); 63 } 64 65 public String getProtocol() { 66 URL url = this.getURL(); 67 if(url == null) { 68 return null; 69 } 70 return url.getProtocol() + ":"; 71 } 72 73 public String getSearch() { 74 URL url = this.getURL(); 75 String query = url == null ? null : url.getQuery(); 76 return query == null ? "" : "?" + query; 78 } 79 80 private String target; 81 82 public String getTarget() { 83 return this.target; 84 } 85 86 public void setTarget(String value) { 87 this.target = value; 88 } 89 90 public String getHref() { 91 Document document = this.window.getDocument(); 92 return document == null ? null : document.getDocumentURI(); 93 } 94 95 public void setHref(String uri) { 96 HtmlRendererContext rcontext = this.window.getHtmlRendererContext(); 97 if(rcontext != null) { 98 try { 99 URL url; 100 Document document = this.window.getDocument(); 101 if(document instanceof HTMLDocumentImpl) { 102 HTMLDocumentImpl docImpl = (HTMLDocumentImpl) document; 103 url = docImpl.getFullURL(uri); 104 } else { 105 url = new URL(uri); 106 } 107 rcontext.navigate(url, this.target); 108 } catch(java.net.MalformedURLException mfu) { 109 logger.log(Level.WARNING, "setHref(): Malformed location: [" + uri + "].", mfu); 110 } 111 } 112 } 113 114 public void reload() { 115 Document document = this.window.getDocument(); 117 if(document instanceof HTMLDocumentImpl) { 118 HTMLDocumentImpl docImpl = (HTMLDocumentImpl) document; 119 HtmlRendererContext rcontext = docImpl.getHtmlRendererContext(); 120 if(rcontext != null) { 121 rcontext.reload(); 122 } 123 else { 124 docImpl.warn("reload(): No renderer context in Location's document."); 125 } 126 } 127 } 128 129 public void replace(String href) { 130 this.setHref(href); 131 } 132 133 public String toString() { 134 return this.getHref(); 137 } 138 } 139 | Popular Tags |