1 27 package org.htmlparser.parserapplications; 28 29 import javax.swing.JOptionPane ; 30 import org.htmlparser.Node; 31 import org.htmlparser.NodeFilter; 32 import org.htmlparser.Parser; 33 import org.htmlparser.filters.AndFilter; 34 import org.htmlparser.filters.NodeClassFilter; 35 import org.htmlparser.tags.LinkTag; 36 import org.htmlparser.util.NodeList; 37 import org.htmlparser.util.ParserException; 38 39 43 public class LinkExtractor 44 { 45 47 public static void main (String [] args) 48 { 49 String url; 50 Parser parser; 51 NodeFilter filter; 52 NodeList list; 53 54 if (0 >= args.length) 55 { 56 url = (String )JOptionPane.showInputDialog ( 57 null, 58 "Enter the URL to extract links from:", 59 "Web Site", 60 JOptionPane.PLAIN_MESSAGE, 61 null, 62 null, 63 "http://htmlparser.sourceforge.net/wiki/"); 64 if (null == url) 65 System.exit (1); 66 } 67 else 68 url = args[0]; 69 filter = new NodeClassFilter (LinkTag.class); 70 if ((1 < args.length) && args[1].equalsIgnoreCase ("-maillinks")) 71 filter = new AndFilter ( 72 filter, 73 new NodeFilter () 74 { 75 public boolean accept (Node node) 76 { 77 return (((LinkTag)node).isMailLink ()); 78 } 79 } 80 ); 81 try 82 { 83 parser = new Parser (url); 84 list = parser.extractAllNodesThatMatch (filter); 85 for (int i = 0; i < list.size (); i++) 86 System.out.println (list.elementAt (i).toHtml ()); 87 } 88 catch (ParserException e) 89 { 90 e.printStackTrace (); 91 } 92 System.exit (0); 93 } 94 } 95 96 | Popular Tags |