1 32 33 package websphinx; 34 35 import java.net.URL ; 36 37 59 public class StandardClassifier implements Classifier { 60 61 64 public StandardClassifier () { 65 } 66 67 71 public void classify (Page page) { 73 Link origin = page.getOrigin (); 74 String pageHost = origin.getHost (); 75 int pagePort = origin.getPort (); 76 String pagePath = origin.getFile(); 77 String pageFilename = origin.getFilename(); 78 79 URL base = page.getBase (); 80 String baseHost = base.getHost (); 81 int basePort = base.getPort (); 82 String basePath = base.getFile (); 83 84 if (pageFilename.equals ("") || pageFilename.startsWith ("index.htm")) 85 page.setLabel ("root"); 86 87 Link[] links = page.getLinks (); 90 if (links != null) { 91 for (int i=0; i<links.length; ++i) { 92 Link link = links[i]; 93 94 if ((link.getHost().equals (pageHost) 95 && link.getPort() == pagePort) 96 || (link.getHost().equals (baseHost) 97 && link.getPort() == basePort)) { 98 link.setLabel ("local"); 99 100 String linkPath = link.getFile (); 101 102 if (linkPath.equals (pagePath) 103 || linkPath.equals (basePath)) 104 link.setLabel ("same-page"); 105 else if (link.getDirectory ().equals (origin.getDirectory ())) 106 link.setLabel ("sibling"); 107 else if (descendsFrom (linkPath, pagePath) 108 || descendsFrom (linkPath, basePath)) 109 link.setLabel ("descendent"); 110 else if (descendsFrom (pagePath, linkPath) 111 || descendsFrom (basePath, linkPath)) 112 link.setLabel ("ancestor"); 113 } 115 else 116 link.setLabel ("remote"); 117 118 String tagName = link.getTagName(); 120 121 if (tagName == Tag.IMG) 122 link.setLabel ("image"); 123 else if (tagName == Tag.APPLET || tagName == Tag.EMBED || tagName == Tag.SCRIPT) 124 link.setLabel ("code"); 125 else if (tagName == Tag.FORM) 126 link.setLabel ("form"); 127 else if (tagName == Tag.A || tagName == Tag.AREA || tagName == Tag.FRAME) { 128 String protocol = link.getProtocol (); 129 130 if ((protocol.equals ("http") 131 || protocol.equals ("ftp") 132 || protocol.equals ("file") 133 || protocol.equals ("gopher")) 134 && link.getMethod() == Link.GET) 135 link.setLabel ("hyperlink"); 136 } 137 } 138 } 139 } 140 141 private boolean descendsFrom (String path1, String path2) { 142 return path1.startsWith (path2.endsWith ("/") 143 ? path2 144 : path2 + "/"); 145 } 146 147 150 public static final float priority = 0.0F; 151 152 156 public float getPriority () { 157 return priority; 158 } 159 } 160 | Popular Tags |