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.URLConnection ; 33 34 import org.htmlparser.NodeFilter; 35 import org.htmlparser.Parser; 36 import org.htmlparser.util.NodeList; 37 import org.htmlparser.util.ParserException; 38 import org.htmlparser.util.EncodingChangeException; 39 40 47 public class FilterBean 48 implements 49 Serializable 50 { 51 54 public static final String PROP_NODES_PROPERTY = "nodes"; 55 56 59 public static final String PROP_TEXT_PROPERTY = "text"; 60 61 64 public static final String PROP_URL_PROPERTY = "URL"; 65 66 69 public static final String PROP_CONNECTION_PROPERTY = "connection"; 70 71 74 protected PropertyChangeSupport mPropertySupport; 75 76 79 protected Parser mParser; 80 81 84 protected NodeFilter[] mFilters; 85 86 89 protected NodeList mNodes; 90 91 94 public FilterBean () 95 { 96 mPropertySupport = new PropertyChangeSupport (this); 97 mParser = new Parser (); 98 mFilters = null; 99 mNodes = null; 100 } 101 102 106 110 protected void updateNodes (NodeList nodes) 111 { 112 NodeList oldValue; 113 String oldText; 114 String newText; 115 116 if ((null == mNodes) || !mNodes.equals (nodes)) 117 { 118 oldValue = mNodes; 119 if (null != oldValue) 120 oldText = getText (); 121 else 122 oldText = ""; 123 if (null == oldText) 124 oldText = ""; 125 mNodes = nodes; 126 if (null != mNodes) newText = getText (); 128 else 129 newText = ""; 130 if (null == newText) 131 newText = ""; 132 mPropertySupport.firePropertyChange (PROP_NODES_PROPERTY, oldValue, nodes); 133 if (!newText.equals (oldText)) 134 mPropertySupport.firePropertyChange (PROP_TEXT_PROPERTY, oldText, newText); 135 } 136 } 137 138 145 protected NodeList applyFilters () 146 throws 147 ParserException 148 { 149 NodeList ret; 150 151 ret = new NodeList (); 152 153 if (null != getFilters ()) 154 for (int i = 0; i < getFilters ().length; i++) 155 if (0 == i) 156 ret = mParser.parse (getFilters ()[0]); 157 else 158 ret = ret.extractAllNodesThatMatch (getFilters ()[i]); 159 160 return (ret); 161 } 162 163 167 protected void setNodes () 168 { 169 NodeList list; 170 171 if (null != getURL ()) 172 try 173 { 174 list = applyFilters (); 175 updateNodes (list); 176 } 177 catch (EncodingChangeException ece) 178 { 179 try 180 { mParser.reset (); 182 list = applyFilters (); 183 updateNodes (list); 184 } 185 catch (ParserException pe) 186 { 187 updateNodes (new NodeList ()); 188 } 189 } 190 catch (ParserException pe) 191 { 192 updateNodes (new NodeList ()); 193 } 194 } 195 196 200 205 public void addPropertyChangeListener (PropertyChangeListener listener) 206 { 207 mPropertySupport.addPropertyChangeListener (listener); 208 } 209 210 215 public void removePropertyChangeListener (PropertyChangeListener listener) 216 { 217 mPropertySupport.removePropertyChangeListener (listener); 218 } 219 220 224 229 public NodeList getNodes () 230 { 231 if (null == mNodes) 232 setNodes (); 233 234 return (mNodes); 235 } 236 237 242 public String getURL () 243 { 244 return ((null != mParser) ? mParser.getURL () : null); 245 } 246 247 253 public void setURL (String url) 254 { 255 String old; 256 URLConnection conn; 257 258 old = getURL (); 259 conn = getConnection (); 260 if (((null == old) && (null != url)) || ((null != old) && !old.equals (url))) 261 { 262 try 263 { 264 if (null == mParser) 265 mParser = new Parser (url); 266 else 267 mParser.setURL (url); 268 mPropertySupport.firePropertyChange (PROP_URL_PROPERTY, old, getURL ()); 269 mPropertySupport.firePropertyChange (PROP_CONNECTION_PROPERTY, conn, mParser.getConnection ()); 270 setNodes (); 271 } 272 catch (ParserException pe) 273 { 274 updateNodes (new NodeList ()); 275 } 276 } 277 } 278 279 284 public URLConnection getConnection () 285 { 286 return ((null != mParser) ? mParser.getConnection () : null); 287 } 288 289 295 public void setConnection (URLConnection connection) 296 { 297 String url; 298 URLConnection conn; 299 300 url = getURL (); 301 conn = getConnection (); 302 if (((null == conn) && (null != connection)) || ((null != conn) && !conn.equals (connection))) 303 { 304 try 305 { 306 if (null == mParser) 307 mParser = new Parser (connection); 308 else 309 mParser.setConnection (connection); 310 mPropertySupport.firePropertyChange (PROP_URL_PROPERTY, url, getURL ()); 311 mPropertySupport.firePropertyChange (PROP_CONNECTION_PROPERTY, conn, mParser.getConnection ()); 312 setNodes (); 313 } 314 catch (ParserException pe) 315 { 316 updateNodes (new NodeList ()); 317 } 318 } 319 } 320 321 325 public NodeFilter[] getFilters () 326 { 327 return (mFilters); 328 } 329 330 336 public void setFilters (NodeFilter[] filters) 337 { 338 mFilters = filters; 339 if (null != getParser ()) 340 { 341 getParser ().reset (); 342 setNodes (); 343 } 344 } 345 346 350 public Parser getParser () 351 { 352 return (mParser); 353 } 354 355 361 public void setParser (Parser parser) 362 { 363 mParser = parser; 364 if (null != getFilters ()) 365 setNodes (); 366 } 367 368 376 public String getText () 377 { 378 NodeList list; 379 StringBean sb; 380 String ret; 381 382 list = getNodes (); 383 if (0 != list.size ()) 384 { 385 sb = new StringBean (); 386 for (int i = 0; i < list.size (); i++) 387 list.elementAt (i).accept (sb); 388 ret = sb.getStrings (); 389 } 390 else 391 ret = ""; 392 393 return (ret); 394 } 395 396 401 public static void main (String [] args) 402 { 403 if (0 >= args.length) 404 System.out.println ("Usage: java -classpath htmlparser.jar org.htmlparser.beans.FilterBean <http://whatever_url> [node name]"); 405 else 406 { 407 FilterBean fb = new FilterBean (); 408 if (1 < args.length) 409 fb.setFilters (new NodeFilter[] { new org.htmlparser.filters.TagNameFilter (args[1]) }); 410 fb.setURL (args[0]); 411 System.out.println (fb.getText ()); 413 } 414 } 415 } 416 | Popular Tags |