| 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 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
public class DumpLinks extends HtmlVisitor { |
| 23 | |
|
| 24 | |
protected PrintWriter out; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 2 | public DumpLinks(OutputStream os) { out = new PrintWriter(os); } |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public DumpLinks(OutputStream os, String encoding) |
| 39 | 0 | throws UnsupportedEncodingException { |
| 40 | 0 | out = new PrintWriter( new OutputStreamWriter(os, encoding) ); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public void finish() { |
| 44 | 2 | out.flush(); |
| 45 | 2 | } |
| 46 | |
|
| 47 | |
public void visit(HtmlDocument.Tag t) { |
| 48 | 16 | if (t.tagName.equalsIgnoreCase("A")) { |
| 49 | 8 | for (Iterator i=t.attributeList.attributes.iterator(); i.hasNext(); ) { |
| 50 | 4 | Attribute a = (Attribute) i.next(); |
| 51 | 4 | if (a.name.equalsIgnoreCase("HREF")) |
| 52 | 4 | out.println(a.getValue()); |
| 53 | 4 | } |
| 54 | |
} |
| 55 | 16 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static void main (String args[]) throws ParseException, IOException { |
| 61 | |
HtmlDocument document; |
| 62 | |
|
| 63 | 0 | document = new HtmlParser(System.in).HtmlDocument(); |
| 64 | 0 | document.accept(new DumpLinks(System.out)); |
| 65 | 0 | } |
| 66 | |
} |
| 67 | |
|