KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > HTMLLinkElementImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.html.domimpl;
22
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.w3c.dom.UserDataHandler JavaDoc;
26 import org.w3c.dom.css.CSSStyleSheet;
27 import org.w3c.dom.html2.*;
28 import org.lobobrowser.html.HtmlRendererContext;
29 import org.lobobrowser.html.style.*;
30 import org.lobobrowser.util.gui.*;
31
32 import java.util.logging.*;
33
34 public class HTMLLinkElementImpl extends HTMLAbstractUIElement implements
35         HTMLLinkElement {
36     private static final Logger logger = Logger.getLogger(HTMLLinkElementImpl.class.getName());
37     private static final boolean loggableInfo = logger.isLoggable(Level.INFO);
38     
39     public HTMLLinkElementImpl(String JavaDoc name) {
40         super(name);
41     }
42
43     private boolean disabled;
44     
45     public boolean getDisabled() {
46         return this.disabled;
47     }
48
49     public void setDisabled(boolean disabled) {
50         this.disabled = disabled;
51     }
52     
53     public String JavaDoc getHref() {
54         return this.getAttribute("href");
55     }
56
57     public void setHref(String JavaDoc href) {
58         this.setAttribute("href", href);
59     }
60
61     public String JavaDoc getHreflang() {
62         return this.getAttribute("hreflang");
63     }
64
65     public void setHreflang(String JavaDoc hreflang) {
66         this.setAttribute("hreflang", hreflang);
67     }
68
69     public String JavaDoc getMedia() {
70         return this.getAttribute("media");
71     }
72
73     public void setMedia(String JavaDoc media) {
74         this.setAttribute("media", media);
75     }
76
77     public String JavaDoc getRel() {
78         return this.getAttribute("rel");
79     }
80
81     public void setRel(String JavaDoc rel) {
82         this.setAttribute("rel", rel);
83     }
84
85     public String JavaDoc getRev() {
86         return this.getAttribute("rev");
87     }
88
89     public void setRev(String JavaDoc rev) {
90         this.setAttribute("rev", rev);
91     }
92
93     public String JavaDoc getTarget() {
94         String JavaDoc target = this.getAttribute("target");
95         if(target != null) {
96             return target;
97         }
98         HTMLDocumentImpl doc = (HTMLDocumentImpl) this.document;
99         return doc == null ? null : doc.getDefaultTarget();
100     }
101
102     public void setTarget(String JavaDoc target) {
103         this.setAttribute("target", target);
104     }
105
106     public String JavaDoc getType() {
107         return this.getAttribute("type");
108     }
109
110     public void setType(String JavaDoc type) {
111         this.setAttribute("type", type);
112     }
113
114     public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc data, UserDataHandler JavaDoc handler) {
115         if(org.lobobrowser.html.parser.HtmlParser.MODIFYING_KEY.equals(key) && data != Boolean.TRUE) {
116             this.processLink();
117         }
118         return super.setUserData(key, data, handler);
119     }
120
121     protected void processLink() {
122         String JavaDoc rel = this.getAttribute("rel");
123         if(rel != null && rel.toLowerCase().indexOf("stylesheet") != -1) {
124             String JavaDoc media = this.getMedia();
125             if(CSSUtilities.matchesMedia(media, this.getHtmlRendererContext())) {
126                 HTMLDocumentImpl doc = (HTMLDocumentImpl) this.getOwnerDocument();
127                 try {
128                     boolean liflag = loggableInfo;
129                     long time1 = liflag ? System.currentTimeMillis() : 0;
130                     try {
131                         CSSStyleSheet sheet = CSSUtilities.parse(this.getHref(), doc, doc.getBaseURI(), false);
132                         if(sheet != null) {
133                             doc.addStyleSheet(sheet);
134                         }
135                     } finally {
136                         if(liflag) {
137                             long time2 = System.currentTimeMillis();
138                             logger.info("processLink(): Loaded and parsed CSS (or attempted to) at URI=[" + this.getHref() + "] in " + (time2 - time1) + " ms.");
139                         }
140                     }
141
142                 } catch(MalformedURLException JavaDoc mfe) {
143                     this.warn("Will not parse CSS. URI=[" + this.getHref() + "] with BaseURI=[" + doc.getBaseURI() + "] does not appear to be a valid URI.");
144                 } catch(Throwable JavaDoc err) {
145                     this.warn("Unable to parse CSS. URI=[" + this.getHref() + "].", err);
146                 }
147             }
148         }
149     }
150
151     public void navigate() {
152         if(this.disabled) {
153             return;
154         }
155         HtmlRendererContext rcontext = this.getHtmlRendererContext();
156         if(rcontext != null) {
157             String JavaDoc href = this.getHref();
158             if(href != null && !"".equals(href)) {
159                 String JavaDoc target = this.getTarget();
160                 try {
161                     URL JavaDoc url = this.getFullURL(href);
162                     if(url == null) {
163                         this.warn("Unable to resolve URI: [" + href + "].");
164                     }
165                     else {
166                         rcontext.navigate(url, target);
167                     }
168                 } catch(MalformedURLException JavaDoc mfu) {
169                     this.warn("Malformed URI: [" + href + "].", mfu);
170                 }
171             }
172         }
173     }
174
175     private java.awt.Color JavaDoc getLinkColor() {
176         HTMLDocument doc = (HTMLDocument) this.document;
177         if(doc != null) {
178             HTMLBodyElement body = (HTMLBodyElement) doc.getBody();
179             if(body != null) {
180                 String JavaDoc vlink = body.getVLink();
181                 String JavaDoc link = body.getLink();
182                 if(vlink != null || link != null) {
183                     HtmlRendererContext rcontext = this.getHtmlRendererContext();
184                     if(rcontext != null) {
185                         boolean visited = rcontext.isVisitedLink(this);
186                         String JavaDoc colorText = visited ? vlink : link;
187                         if(colorText != null) {
188                             return ColorFactory.getInstance().getColor(colorText);
189                         }
190                     }
191                 }
192             }
193         }
194         return java.awt.Color.BLUE;
195     }
196     
197     protected RenderState createRenderState(RenderState prevRenderState) {
198         prevRenderState = new TextDecorationRenderState(prevRenderState, RenderState.MASK_TEXTDECORATION_UNDERLINE);
199         prevRenderState = new ColorRenderState(prevRenderState, this.getLinkColor());
200         return super.createRenderState(prevRenderState);
201     }
202 }
203
Popular Tags