KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > js > Location


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 JavaDoc;
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 JavaDoc document = this.window.getDocument();
23             url = document == null ? null : new URL(document.getDocumentURI());
24         } catch(java.net.MalformedURLException JavaDoc mfu) {
25             url = null;
26         }
27         return url;
28     }
29
30     public String JavaDoc getHash() {
31         URL url = this.getURL();
32         return url == null ? null : url.getRef();
33     }
34     
35     public String JavaDoc 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 JavaDoc getHostname() {
44         URL url = this.getURL();
45         if(url == null) {
46             return null;
47         }
48         return url.getHost();
49     }
50
51     public String JavaDoc getPathname() {
52         URL url = this.getURL();
53         return url == null ? null : url.getPath();
54     }
55     
56     public String JavaDoc 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 JavaDoc getProtocol() {
66         URL url = this.getURL();
67         if(url == null) {
68             return null;
69         }
70         return url.getProtocol() + ":";
71     }
72
73     public String JavaDoc getSearch() {
74         URL url = this.getURL();
75         String JavaDoc query = url == null ? null : url.getQuery();
76         // Javascript requires "?" in its search string.
77
return query == null ? "" : "?" + query;
78     }
79     
80     private String JavaDoc target;
81     
82     public String JavaDoc getTarget() {
83         return this.target;
84     }
85
86     public void setTarget(String JavaDoc value) {
87         this.target = value;
88     }
89     
90     public String JavaDoc getHref() {
91         Document JavaDoc document = this.window.getDocument();
92         return document == null ? null : document.getDocumentURI();
93     }
94     
95     public void setHref(String JavaDoc uri) {
96         HtmlRendererContext rcontext = this.window.getHtmlRendererContext();
97         if(rcontext != null) {
98             try {
99                 URL url;
100                 Document JavaDoc 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 JavaDoc mfu) {
109                 logger.log(Level.WARNING, "setHref(): Malformed location: [" + uri + "].", mfu);
110             }
111         }
112     }
113     
114     public void reload() {
115         //TODO: This is not really reload.
116
Document JavaDoc 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 JavaDoc href) {
130         this.setHref(href);
131     }
132     
133     public String JavaDoc toString() {
134         // This needs to be href. Callers
135
// rely on that.
136
return this.getHref();
137     }
138 }
139
Popular Tags