1 21 package org.lobobrowser.html.domimpl; 22 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import org.w3c.dom.UserDataHandler ; 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 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 getHref() { 54 return this.getAttribute("href"); 55 } 56 57 public void setHref(String href) { 58 this.setAttribute("href", href); 59 } 60 61 public String getHreflang() { 62 return this.getAttribute("hreflang"); 63 } 64 65 public void setHreflang(String hreflang) { 66 this.setAttribute("hreflang", hreflang); 67 } 68 69 public String getMedia() { 70 return this.getAttribute("media"); 71 } 72 73 public void setMedia(String media) { 74 this.setAttribute("media", media); 75 } 76 77 public String getRel() { 78 return this.getAttribute("rel"); 79 } 80 81 public void setRel(String rel) { 82 this.setAttribute("rel", rel); 83 } 84 85 public String getRev() { 86 return this.getAttribute("rev"); 87 } 88 89 public void setRev(String rev) { 90 this.setAttribute("rev", rev); 91 } 92 93 public String getTarget() { 94 String 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 target) { 103 this.setAttribute("target", target); 104 } 105 106 public String getType() { 107 return this.getAttribute("type"); 108 } 109 110 public void setType(String type) { 111 this.setAttribute("type", type); 112 } 113 114 public Object setUserData(String key, Object data, UserDataHandler 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 rel = this.getAttribute("rel"); 123 if(rel != null && rel.toLowerCase().indexOf("stylesheet") != -1) { 124 String 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 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 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 href = this.getHref(); 158 if(href != null && !"".equals(href)) { 159 String target = this.getTarget(); 160 try { 161 URL 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 mfu) { 169 this.warn("Malformed URI: [" + href + "].", mfu); 170 } 171 } 172 } 173 } 174 175 private java.awt.Color getLinkColor() { 176 HTMLDocument doc = (HTMLDocument) this.document; 177 if(doc != null) { 178 HTMLBodyElement body = (HTMLBodyElement) doc.getBody(); 179 if(body != null) { 180 String vlink = body.getVLink(); 181 String 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 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 |