1 27 package org.htmlparser.beans; 28 29 import java.beans.PropertyChangeListener ; 30 import java.beans.PropertyChangeSupport ; 31 import java.io.Serializable ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.util.Vector ; 36 37 import org.htmlparser.Node; 38 import org.htmlparser.NodeFilter; 39 import org.htmlparser.Parser; 40 import org.htmlparser.filters.NodeClassFilter; 41 import org.htmlparser.tags.LinkTag; 42 import org.htmlparser.util.EncodingChangeException; 43 import org.htmlparser.util.NodeList; 44 import org.htmlparser.util.ParserException; 45 46 49 public class LinkBean extends Object implements Serializable 50 { 51 54 public static final String PROP_LINKS_PROPERTY = "links"; 55 56 59 public static final String PROP_URL_PROPERTY = "URL"; 60 61 64 protected PropertyChangeSupport mPropertySupport; 65 66 69 protected URL [] mLinks; 70 71 74 protected Parser mParser; 75 76 77 public LinkBean () 78 { 79 mPropertySupport = new PropertyChangeSupport (this); 80 mLinks = null; 81 mParser = new Parser (); 82 } 83 84 88 protected URL [] extractLinks () throws ParserException 89 { 90 NodeFilter filter; 91 NodeList list; 92 Vector vector; 93 LinkTag link; 94 URL [] ret; 95 96 mParser.reset (); 97 filter = new NodeClassFilter (LinkTag.class); 98 try 99 { 100 list = mParser.extractAllNodesThatMatch (filter); 101 } 102 catch (EncodingChangeException ece) 103 { 104 mParser.reset (); 105 list = mParser.extractAllNodesThatMatch (filter); 106 } 107 vector = new Vector (); 108 for (int i = 0; i < list.size (); i++) 109 try 110 { 111 link = (LinkTag)list.elementAt (i); 112 vector.add(new URL (link.getLink ())); 113 } 114 catch (MalformedURLException murle) 115 { 116 } 119 ret = new URL [vector.size ()]; 120 vector.copyInto (ret); 121 122 return (ret); 123 } 124 125 132 protected boolean equivalent (URL [] array1, URL [] array2) 133 { 134 boolean ret; 135 136 ret = false; 137 if ((null == array1) && (null == array2)) 138 ret = true; 139 else if ((null != array1) && (null != array2)) 140 if (array1.length == array2.length) 141 { 142 ret = true; 143 for (int i = 0; i < array1.length && ret; i++) 144 if (!(array1[i] == array2[i])) 145 ret = false; 146 } 147 148 return (ret); 149 } 150 151 155 160 public void addPropertyChangeListener (PropertyChangeListener listener) 161 { 162 mPropertySupport.addPropertyChangeListener (listener); 163 } 164 165 170 public void removePropertyChangeListener (PropertyChangeListener listener) 171 { 172 mPropertySupport.removePropertyChangeListener (listener); 173 } 174 175 179 182 private void setLinks () 183 { 184 String url; 185 URL [] urls; 186 URL [] oldValue; 187 188 url = getURL (); 189 if (null != url) 190 try 191 { 192 urls = extractLinks (); 193 if (!equivalent (mLinks, urls)) 194 { 195 oldValue = mLinks; 196 mLinks = urls; 197 mPropertySupport.firePropertyChange (PROP_LINKS_PROPERTY, oldValue, mLinks); 198 } 199 } 200 catch (ParserException hpe) 201 { 202 mLinks = null; 203 } 204 } 205 206 210 public URL [] getLinks () 211 { 212 if (null == mLinks) 213 try 214 { 215 mLinks = extractLinks (); 216 mPropertySupport.firePropertyChange (PROP_LINKS_PROPERTY, null, mLinks); 217 } 218 catch (ParserException hpe) 219 { 220 mLinks = null; 221 } 222 223 return (mLinks); 224 } 225 226 227 231 public String getURL () 232 { 233 return (mParser.getURL ()); 234 } 235 236 240 public void setURL (String url) 241 { 242 String old; 243 244 old = getURL (); 245 if (((null == old) && (null != url)) || ((null != old) && !old.equals (url))) 246 { 247 try 248 { 249 mParser.setURL (url); 250 mPropertySupport.firePropertyChange (PROP_URL_PROPERTY, old, getURL ()); 251 setLinks (); 252 } 253 catch (ParserException hpe) 254 { 255 } 257 } 258 } 259 260 264 public URLConnection getConnection () 265 { 266 return (mParser.getConnection ()); 267 } 268 269 273 public void setConnection (URLConnection connection) 274 { 275 try 276 { 277 mParser.setConnection (connection); 278 setLinks (); 279 } 280 catch (ParserException hpe) 281 { 282 } 284 } 285 286 290 public static void main (String [] args) 291 { 292 if (0 >= args.length) 293 System.out.println ("Usage: java -classpath htmlparser.jar org.htmlparser.beans.LinkBean <http://whatever_url>"); 294 else 295 { 296 LinkBean lb = new LinkBean (); 297 lb.setURL (args[0]); 298 URL [] urls = lb.getLinks (); 299 for (int i = 0; i < urls.length; i++) 300 System.out.println (urls[i]); 301 } 302 } 303 } 304 305 306 | Popular Tags |