View Javadoc

1   package com.quiotix.html.example;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.io.OutputStreamWriter;
6   import java.io.PrintWriter;
7   import java.io.UnsupportedEncodingException;
8   import java.util.Iterator;
9   
10  import com.quiotix.html.parser.HtmlDocument;
11  import com.quiotix.html.parser.HtmlParser;
12  import com.quiotix.html.parser.HtmlVisitor;
13  import com.quiotix.html.parser.ParseException;
14  import com.quiotix.html.parser.HtmlDocument.Attribute;
15  
16  /** 
17   * Example visitor to dump out the links from an HTML document.
18   *
19   * @author Brian Goetz, Quiotix
20   */
21  
22  public class DumpLinks extends HtmlVisitor {
23  
24    protected PrintWriter out;
25  
26    /**
27     * Constructor.
28     * 
29     * @param os OutputStream to dump to
30     */
31    public DumpLinks(OutputStream os)     { out = new PrintWriter(os); }
32  
33    /**
34     * Constructor.
35     * 
36     * @param os OutputStream to dump to
37     */
38    public DumpLinks(OutputStream os, String encoding)
39      throws UnsupportedEncodingException {
40      out = new PrintWriter( new OutputStreamWriter(os, encoding) );
41    }
42  
43    public void finish() { 
44        out.flush();
45    }
46  
47    public void visit(HtmlDocument.Tag t) { 
48      if (t.tagName.equalsIgnoreCase("A")) {
49        for (Iterator i=t.attributeList.attributes.iterator(); i.hasNext(); ) {
50          Attribute a = (Attribute) i.next();
51          if (a.name.equalsIgnoreCase("HREF"))
52            out.println(a.getValue());
53        }
54      }
55    }
56  
57    /**
58     * Runnable.
59     */
60    public static void main (String args[]) throws ParseException, IOException {
61      HtmlDocument document;
62  
63      document = new HtmlParser(System.in).HtmlDocument();
64      document.accept(new DumpLinks(System.out));
65    }
66  }
67