1 32 33 package websphinx; 34 35 import java.util.Enumeration ; 36 import java.io.File ; 37 import java.net.URL ; 38 import java.net.MalformedURLException ; 39 import rcm.util.Prioritized; 40 41 47 public class Link extends Element implements Prioritized { 48 49 protected URL url; 50 51 private String directory; 52 private String filename; 53 private String query; 54 private String ref; 55 private Page page; 56 private int depth; 57 private String text = ""; private int status = LinkEvent.NONE; 59 private float priority; 60 private DownloadParameters dp; 61 63 70 public Link (Tag startTag, Tag endTag, URL base) throws MalformedURLException { 71 super (startTag, endTag); 72 url = urlFromHref (startTag, base); 73 depth = startTag.getSource().getDepth() + 1; 74 } 75 76 79 public Link (URL url) { 80 super (new Tag (new Page (""), 0, 0, "", true), null); 81 this.url = url; 82 depth = 0; 83 } 84 85 88 public Link (File file) throws MalformedURLException { 89 this (FileToURL (file)); 90 } 91 92 96 public Link (String href) throws MalformedURLException { 97 this (new URL (href)); 98 depth = 0; 99 } 100 101 104 public void discardContent () { 105 parent = null; 106 child = null; 107 sibling = null; 108 } 109 110 113 public void disconnect () { 114 page = null; 115 status = LinkEvent.NONE; 116 } 117 118 122 public int getDepth () { 123 return depth; 124 } 125 126 130 public URL getURL () { 131 return url; 132 } 133 134 138 public String getProtocol () { 139 return getURL().getProtocol (); 140 } 141 142 146 public String getHost () { 147 return getURL().getHost (); 148 } 149 150 155 public int getPort () { 156 return getURL().getPort (); 157 } 158 159 165 public String getFile () { 166 return getURL().getFile (); 167 } 168 169 174 public String getDirectory () { 175 if (directory == null) 176 parseURL (); 177 return directory; 178 } 179 180 185 public String getFilename () { 186 if (filename == null) 187 parseURL (); 188 return filename; 189 } 190 191 196 public String getQuery () { 197 if (query == null) 198 parseURL (); 199 return query; 200 } 201 202 207 public String getRef () { 208 if (ref == null) 209 parseURL (); 210 return ref; 211 } 212 213 217 public URL getPageURL () { 218 return getPageURL (getURL()); 219 } 220 221 225 public static URL getPageURL (URL url) { 226 String href = url.toExternalForm (); 227 int i = href.indexOf ('#'); 228 try { 229 return (i != -1) ? new URL (href.substring (0, i)) : url; 230 } catch (MalformedURLException e) { 231 return url; 232 } 233 } 234 235 239 public URL getServiceURL () { 240 return getServiceURL (getURL()); 241 } 242 243 244 248 public static URL getServiceURL (URL url) { 249 String href = url.toExternalForm (); 250 int i = href.indexOf ('?'); 251 try { 252 return (i != -1 && url.getProtocol().equals ("http")) 253 ? new URL (href.substring (0, i)) 254 : getPageURL(url); 255 } catch (MalformedURLException e) { 256 return url; 257 } 258 } 259 260 264 public URL getDirectoryURL () { 265 return getDirectoryURL (getURL()); 266 } 267 268 269 273 public static URL getDirectoryURL (URL url) { 274 String file = url.getFile(); 275 int qmark = file.indexOf ('?'); 276 if (qmark == -1 || !url.getProtocol().equals ("http")) 277 qmark = file.length(); 278 int pivot = file.lastIndexOf ('/', Math.max(qmark-1, 0)); 280 try { 281 if (pivot == -1) 282 return new URL (url, "/"); 283 else if (pivot == file.length()-1) 284 return url; 285 else 286 return new URL (url, file.substring (0, pivot+1)); 287 } catch (MalformedURLException e) { 288 return url; 289 } 290 } 291 292 296 public URL getParentURL () { 297 return getParentURL (getURL()); 298 } 299 300 301 305 public static URL getParentURL (URL url) { 306 URL dirURL = getDirectoryURL (url); 307 if (!dirURL.equals (url)) 308 return dirURL; 309 310 String dir = dirURL.getFile (); 311 int lastSlash = dir.length()-1; 312 if (lastSlash == 0) 313 return dirURL; 314 315 int penultSlash = dir.lastIndexOf ('/', lastSlash-1); 316 317 if (penultSlash == -1) 318 return dirURL; 319 320 try { 321 return new URL (url, dir.substring (0, penultSlash+1)); 322 } catch (MalformedURLException e) { 323 return dirURL; 324 } 325 } 326 327 public static String relativeTo (URL here, URL there) { 330 if (here == null) 331 return there.toString(); 332 if (here.getProtocol().equals (there.getProtocol()) 335 && here.getHost().equals (there.getHost ()) 336 && here.getPort() == there.getPort ()) { 337 String fn = relativeTo (here.getFile (), 338 there.getFile ()); 339 String ref = there.getRef (); 340 return (ref != null) ? fn+ref : fn; 341 } 342 else { 343 return there.toString (); 345 } 346 } 347 348 public static String relativeTo (URL here, String there) { 351 if (here == null) 352 return there; 353 try { 354 return relativeTo (here, new URL (here, there)); 355 } catch (MalformedURLException e) { 356 return there; 357 } 358 } 359 360 private static String relativeTo (String here, String there) { 363 StringBuffer result = new StringBuffer (); 364 365 int lcp = 0; 366 367 while (true) { 368 int i = here.indexOf ('/', lcp); 369 int j = there.indexOf ('/', lcp); 370 371 if (i == -1 || i != j || !here.regionMatches (lcp, there, lcp, i-lcp)) 372 break; 373 lcp = i+1; 374 } 375 376 379 381 for (int i = here.indexOf ('/', lcp); 383 i != -1; 384 i = here.indexOf ('/', i+1)) { 385 result.append (".."); 386 result.append ('/'); 387 } 388 389 result.append (there.substring (lcp)); 391 392 395 return result.toString (); 396 } 397 398 405 public static URL FileToURL (File file) throws MalformedURLException { 406 return new URL ("file:" + toURLDelimiters (file.getAbsolutePath ())); 407 } 408 409 419 public static File URLToFile (URL url) throws MalformedURLException { 420 if (!url.getProtocol().equals ("file")) 421 throw new MalformedURLException (); 422 423 String path = url.getFile (); 424 path = path.replace ('/', File.separatorChar); 425 if (path.length () > 3 428 && path.charAt (0) == File.separatorChar 429 && path.charAt(2) == ':' 430 && path.charAt (3) == File.separatorChar) 431 path = path.substring (1); 432 433 return new File (path); 434 } 435 436 public static String toURLDelimiters (String path) { 437 path = path.replace ('\\', '/'); 438 if (!path.startsWith ("/")) 439 path = "/" + path; 440 return path; 441 } 442 443 447 public Page getPage () { 448 return page; 449 } 450 454 public void setPage (Page page) { 455 this.page = page; 456 } 457 458 461 public static final int GET = 0; 462 465 public static final int POST = 1; 466 467 471 public int getMethod () { 472 return GET; 473 } 474 475 479 public String toURL () { 480 return getURL().toExternalForm (); 481 } 482 483 487 public String toDescription () { 488 return (text.length() > 0 ? text + " " : "") + "[" + getURL() + "]"; 489 } 490 491 495 public String toText () { 496 return text; 497 } 498 499 503 public void setText (String text) { 504 this.text = text; 505 } 506 507 private void parseURL () { 508 String protocol = getProtocol(); 509 String file = getFile(); 510 511 int qmark = file.indexOf ('?'); 512 if (qmark == -1 || !protocol.equals ("http")) { 513 query = ""; 514 qmark = file.length(); 515 } 516 else { 517 query = file.substring (qmark+1); 518 file = file.substring (0, qmark); 519 } 520 521 int slash = file.lastIndexOf ('/', Math.max(qmark-1, 0)); 522 if (slash == -1) { 523 directory = ""; 524 filename = file; 525 } 526 else { 527 directory = file.substring (0, slash+1); 528 filename = file.substring (slash+1); 529 } 530 531 ref = getURL().getRef (); 532 if (ref == null) 533 ref = ""; 534 } 535 536 542 protected URL urlFromHref (Tag tag, URL base) throws MalformedURLException { 543 String hrefAttr = getHrefAttributeName (tag); 545 String href = tag.getHTMLAttribute (hrefAttr); 546 if (tag.tagName == Tag.APPLET) { 547 String codebase = tag.getHTMLAttribute ("codebase"); 548 if (codebase != null) 549 base = new URL (base, codebase); 550 } 551 return new URL (base, href); 552 } 553 554 562 public Tag replaceHref (String newHref) { 563 Tag tag = startTag; 564 565 if (tag.getTagName() == Tag.APPLET) { 566 int i = newHref.lastIndexOf ('/'); 567 if (i != -1) { 568 tag = startTag.replaceHTMLAttribute ("codebase", newHref.substring (0, i+1)); 569 newHref = newHref.substring (i+1); 570 } 571 } 572 String hrefAttrName = getHrefAttributeName (tag); 573 if (hrefAttrName == null) 574 return tag; 575 return tag.replaceHTMLAttribute (hrefAttrName, newHref); 576 } 577 578 private static String getHrefAttributeName (Tag tag) { 579 return (String )HTMLParser.linktag.get (tag.getTagName ()); 580 } 581 582 586 public int getStatus () { 587 return status; 588 } 589 590 594 public void setStatus (int event) { 595 status = event; 596 } 597 598 601 public float getPriority () { 602 return priority; 603 } 604 605 608 public void setPriority (float priority) { 609 this.priority = priority; 610 } 611 612 615 public DownloadParameters getDownloadParameters () { 616 return dp; 617 } 618 619 622 public void setDownloadParameters (DownloadParameters dp) { 623 this.dp = dp; 624 } 625 626 639 } 640 | Popular Tags |