Mercurial > hg > Members > nobuyasu > TestNeo4j
comparison src/howtouse/TextTagParser.java @ 16:5c8c554ee3ac draft
hg mv TestSaxParser.java TextTagParser.java
author | one |
---|---|
date | Thu, 23 Aug 2012 09:03:33 +0900 |
parents | src/howtouse/TestSaxParser.java@fa38ded4c054 |
children |
comparison
equal
deleted
inserted
replaced
15:fa38ded4c054 | 16:5c8c554ee3ac |
---|---|
1 package howtouse; | |
2 | |
3 import java.util.HashMap; | |
4 import java.util.HashSet; | |
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 | |
11 public class TextTagParser extends DefaultHandler { | |
12 | |
13 protected Stack<MyObject> stack; | |
14 private MyObject currentObj; | |
15 private Attributes currentAttr; | |
16 private String currentTag; | |
17 private String currentTitleName; | |
18 final static String TAGNAME_TITLE = "title"; | |
19 final static String TAGNAME_TEXT = "text"; | |
20 WikiLinkParser linkParser = new WikiLinkParser(); | |
21 | |
22 HashMap<String,HashSet<String>> hash = new HashMap<String,HashSet<String>>(); | |
23 HashSet<String> currentLinkHash = new HashSet<String>(); | |
24 | |
25 public TextTagParser() { | |
26 stack = new Stack<MyObject>(); | |
27 } | |
28 | |
29 public HashMap<String,HashSet<String>> getHash() { | |
30 return hash; | |
31 } | |
32 | |
33 public void startDocument() { | |
34 // System.out.println("read start"); | |
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); | |
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 } | |
66 } | |
67 } | |
68 } | |
69 | |
70 public void endElement(String uri, String localName, String qName) { | |
71 | |
72 if (currentObj == null) | |
73 return; | |
74 if (qName.equals(TAGNAME_TITLE)) { | |
75 stack.pop(); | |
76 } else if (qName.equals(TAGNAME_TEXT)) { | |
77 hash.put(currentTitleName, currentLinkHash); | |
78 currentLinkHash = new HashSet<String>(); | |
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() { | |
90 // System.out.println("end reading file."); | |
91 } | |
92 | |
93 } |