8
|
1 package howtouse;
|
|
2
|
10
|
3 import java.util.HashMap;
|
|
4 import java.util.HashSet;
|
8
|
5 import java.util.Stack;
|
|
6
|
|
7 import org.xml.sax.Attributes;
|
|
8 import org.xml.sax.SAXException;
|
|
9 import org.xml.sax.helpers.DefaultHandler;
|
|
10
|
16
|
11 public class TextTagParser extends DefaultHandler {
|
8
|
12
|
|
13 protected Stack<MyObject> stack;
|
|
14 private MyObject currentObj;
|
|
15 private Attributes currentAttr;
|
|
16 private String currentTag;
|
10
|
17 private String currentTitleName;
|
8
|
18 final static String TAGNAME_TITLE = "title";
|
|
19 final static String TAGNAME_TEXT = "text";
|
10
|
20 WikiLinkParser linkParser = new WikiLinkParser();
|
|
21
|
|
22 HashMap<String,HashSet<String>> hash = new HashMap<String,HashSet<String>>();
|
|
23 HashSet<String> currentLinkHash = new HashSet<String>();
|
8
|
24
|
16
|
25 public TextTagParser() {
|
8
|
26 stack = new Stack<MyObject>();
|
|
27 }
|
10
|
28
|
|
29 public HashMap<String,HashSet<String>> getHash() {
|
|
30 return hash;
|
|
31 }
|
8
|
32
|
|
33 public void startDocument() {
|
13
|
34 // System.out.println("read start");
|
8
|
35 }
|
|
36
|
|
37 public void startElement(String uri, String localName, String qName,
|
|
38 Attributes attributes) throws SAXException {
|
|
39 currentTag = qName;
|
|
40 if (qName.equals(TAGNAME_TITLE)) {
|
|
41 currentObj = new MyObject(attributes);
|
|
42 stack.push(currentObj);
|
|
43 } else if (qName.equals(TAGNAME_TEXT)) {
|
|
44 currentObj = new MyObject(attributes);
|
|
45 stack.push(currentObj);
|
|
46 } else {
|
|
47
|
|
48 }
|
|
49 }
|
|
50
|
|
51 public void characters(char[] ch, int offset, int length) {
|
|
52
|
|
53 String value = new String(ch, offset, length);
|
|
54 if (currentObj != null) {
|
|
55 currentObj.setValue(currentTag, currentAttr, value);
|
10
|
56
|
|
57 if(currentTag.equals(TAGNAME_TITLE)) {
|
|
58 currentTitleName = value;
|
|
59 }
|
|
60 if(currentTag.equals(TAGNAME_TEXT)) {
|
|
61 HashSet<String> tmpHash = linkParser.parse(value);
|
|
62 if (tmpHash.size() <= 0) return;
|
|
63 for (String link: tmpHash) {
|
|
64 currentLinkHash.add(link);
|
|
65 }
|
8
|
66 }
|
|
67 }
|
|
68 }
|
|
69
|
|
70 public void endElement(String uri, String localName, String qName) {
|
10
|
71
|
8
|
72 if (currentObj == null)
|
|
73 return;
|
|
74 if (qName.equals(TAGNAME_TITLE)) {
|
|
75 stack.pop();
|
|
76 } else if (qName.equals(TAGNAME_TEXT)) {
|
10
|
77 hash.put(currentTitleName, currentLinkHash);
|
|
78 currentLinkHash = new HashSet<String>();
|
8
|
79 stack.pop();
|
|
80 } else {
|
|
81
|
|
82 }
|
|
83 if (stack.empty())
|
|
84 currentObj = null;
|
|
85 else
|
|
86 currentObj = (MyObject) stack.peek();
|
|
87 }
|
|
88
|
|
89 public void endDocument() {
|
13
|
90 // System.out.println("end reading file.");
|
8
|
91 }
|
|
92
|
|
93 }
|