1 27 package org.htmlparser.tags; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.util.ParserUtils; 31 import org.htmlparser.util.SimpleNodeIterator; 32 33 36 public class LinkTag extends CompositeTag 37 { 38 41 private static final String [] mIds = new String [] {"A"}; 42 43 46 private static final String [] mEnders = new String [] {"A", "P", "DIV", "TD", "TR", "FORM", "LI"}; 47 48 51 private static final String [] mEndTagEnders = new String [] {"P", "DIV", "TD", "TR", "FORM", "LI", "BODY", "HTML"}; 52 53 56 protected String mLink; 57 58 61 private boolean mailLink; 62 63 66 private boolean javascriptLink; 67 68 88 public LinkTag () 89 { 90 } 91 92 96 public String [] getIds () 97 { 98 return (mIds); 99 } 100 101 105 public String [] getEnders () 106 { 107 return (mEnders); 108 } 109 110 114 public String [] getEndTagEnders () 115 { 116 return (mEndTagEnders); 117 } 118 119 122 public String getAccessKey() 123 { 124 return (getAttribute("ACCESSKEY")); 125 } 126 127 133 public String getLink() 134 { 135 if (null == mLink) 136 { 137 mailLink=false; 138 javascriptLink = false; 139 mLink = extractLink (); 140 141 int mailto = mLink.indexOf("mailto"); 142 if (mailto==0) 143 { 144 mailto = mLink.indexOf(":"); 146 mLink = mLink.substring(mailto+1); 147 mailLink = true; 148 } 149 int javascript = mLink.indexOf("javascript:"); 150 if (javascript == 0) 151 { 152 mLink = mLink.substring(11); javascriptLink = true; 154 } 155 } 156 return (mLink); 157 } 158 159 162 public String getLinkText() 163 { 164 String ret; 165 166 if (null != getChildren ()) 167 ret = getChildren ().asString (); 168 else 169 ret = ""; 170 171 return (ret); 172 } 173 174 178 public boolean isMailLink() 179 { 180 getLink (); return (mailLink); 182 } 183 184 188 public boolean isJavascriptLink() 189 { 190 getLink (); return (javascriptLink); 192 } 193 194 199 public boolean isFTPLink() { 200 return getLink ().indexOf("ftp://")==0; 201 } 202 203 207 public boolean isIRCLink() { 208 return getLink ().indexOf("irc://")==0; 209 } 210 211 216 public boolean isHTTPLink() 217 { 218 return (!isFTPLink() && !isHTTPSLink() && !isJavascriptLink() && !isMailLink() && !isIRCLink()); 219 } 220 221 226 public boolean isHTTPSLink() { 227 return getLink ().indexOf("https://")==0; 228 } 229 230 235 public boolean isHTTPLikeLink() { 236 return isHTTPLink() || isHTTPSLink(); 237 } 238 239 240 245 public void setMailLink(boolean newMailLink) 246 { 247 mailLink = newMailLink; 248 } 249 250 255 public void setJavascriptLink(boolean newJavascriptLink) 256 { 257 javascriptLink = newJavascriptLink; 258 } 259 260 263 public String toString() 264 { 265 StringBuffer sb = new StringBuffer (); 266 sb.append("Link to : "+ getLink() + "; titled : "+getLinkText ()+"; begins at : "+getStartPosition ()+"; ends at : "+getEndPosition ()+ ", AccessKey="); 267 if (getAccessKey ()==null) 268 sb.append("null\n"); 269 else 270 sb.append(getAccessKey ()+"\n"); 271 if (null != getChildren ()) 272 { 273 sb.append(" "+"LinkData\n"); 274 sb.append(" "+"--------\n"); 275 276 Node node; 277 int i = 0; 278 for (SimpleNodeIterator e=children();e.hasMoreNodes();) 279 { 280 node = e.nextNode(); 281 sb.append(" "+(i++)+ " "); 282 sb.append(node.toString()+"\n"); 283 } 284 } 285 sb.append(" "+"*** END of LinkData ***\n"); 286 return sb.toString(); 287 } 288 289 public void setLink(String link) 290 { 291 mLink = link; 292 setAttribute ("HREF", link); 293 } 294 295 300 public SimpleNodeIterator linkData() { 301 return children(); 302 } 303 304 309 public String extractLink () 310 { 311 String ret; 312 313 ret = getAttribute ("HREF"); 314 if (null != ret) 315 { 316 ret = ParserUtils.removeChars (ret,'\n'); 317 ret = ParserUtils.removeChars (ret,'\r'); 318 } 319 if (null != getPage ()) 320 ret = getPage ().getAbsoluteURL (ret); 321 322 return (ret); 323 } 324 } 325 | Popular Tags |