Mercurial > hg > Members > nobuyasu > TestNeo4j
changeset 10:e98ca9548c78 draft
fix TestSaxParser.java
author | one |
---|---|
date | Wed, 22 Aug 2012 03:18:41 +0900 |
parents | d655c8fef734 |
children | b56ff507cdb4 |
files | src/howtouse/TestSaxParser.java |
diffstat | 1 files changed, 38 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/src/howtouse/TestSaxParser.java Wed Aug 22 01:58:54 2012 +0900 +++ b/src/howtouse/TestSaxParser.java Wed Aug 22 03:18:41 2012 +0900 @@ -1,6 +1,9 @@ package howtouse; import java.io.FileInputStream; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import java.util.Stack; import javax.xml.parsers.SAXParser; @@ -22,7 +25,19 @@ SAXParser parser = factory.newSAXParser(); parser.parse(fis, sample); - + HashMap<String,HashSet<String>> hash; + hash = sample.getHash(); + + + for (Map.Entry<String,HashSet<String>> entry : hash.entrySet()) { + String title = entry.getKey(); + System.out.println("title: "+title); + for (String link : entry.getValue()) { + System.out.println("\t"+link); + } + } + + } catch (Exception e) { e.printStackTrace(); } @@ -32,12 +47,21 @@ private MyObject currentObj; private Attributes currentAttr; private String currentTag; + private String currentTitleName; final static String TAGNAME_TITLE = "title"; final static String TAGNAME_TEXT = "text"; + WikiLinkParser linkParser = new WikiLinkParser(); + + HashMap<String,HashSet<String>> hash = new HashMap<String,HashSet<String>>(); + HashSet<String> currentLinkHash = new HashSet<String>(); public TestSaxParser() { stack = new Stack<MyObject>(); } + + public HashMap<String,HashSet<String>> getHash() { + return hash; + } public void startDocument() { System.out.println("read start"); @@ -62,20 +86,29 @@ String value = new String(ch, offset, length); if (currentObj != null) { currentObj.setValue(currentTag, currentAttr, value); - if (currentTag.equals(TAGNAME_TITLE) || - currentTag.equals(TAGNAME_TEXT)) { - System.out.println("\ntag:"+currentTag); - System.out.println("value:\n"+value); + + if(currentTag.equals(TAGNAME_TITLE)) { + currentTitleName = value; + } + if(currentTag.equals(TAGNAME_TEXT)) { + HashSet<String> tmpHash = linkParser.parse(value); + if (tmpHash.size() <= 0) return; + for (String link: tmpHash) { + currentLinkHash.add(link); + } } } } public void endElement(String uri, String localName, String qName) { + if (currentObj == null) return; if (qName.equals(TAGNAME_TITLE)) { stack.pop(); } else if (qName.equals(TAGNAME_TEXT)) { + hash.put(currentTitleName, currentLinkHash); + currentLinkHash = new HashSet<String>(); stack.pop(); } else { @@ -84,8 +117,6 @@ currentObj = null; else currentObj = (MyObject) stack.peek(); - - } public void endDocument() {