# HG changeset patch # User one # Date 1345568334 -32400 # Node ID d655c8fef734ba85a9f74965c8db9959ab31e12d # Parent d8d0855bcdfd276ac76c1bf5bdf9bc77c10557e0 fix WikiLinkParser.java diff -r d8d0855bcdfd -r d655c8fef734 src/howtouse/CharReader.java --- a/src/howtouse/CharReader.java Wed Aug 22 01:44:26 2012 +0900 +++ b/src/howtouse/CharReader.java Wed Aug 22 01:58:54 2012 +0900 @@ -1,15 +1,12 @@ package howtouse; -import java.io.BufferedReader; -import java.io.IOException; public class CharReader { final static char EOFchar = (char) 0; - private BufferedReader reader; - private String line; - private int lineLength; + private String text; + private int textLength; private int index; private final char LBRANK = '['; @@ -26,42 +23,25 @@ } - CharReader(BufferedReader r) { - reader = r; - lineLength = 0; - index = 1; - } - CharReader() { - lineLength = 0; - index = 1; } - + public void setText(String str) { + text = str; + textLength = text.length(); + index = 0; + } char nextChar() { - if (index < lineLength) - return line.charAt(index++); - - if (index++ == lineLength) - return '\n'; + if (index < textLength) + return text.charAt(index++); - try { - if ((line = reader.readLine()) != null) { - lineLength = line.length(); - index = 0; - return nextChar(); - } - } catch (IOException e) { - System.out.println("Cannot read line"); - - } return EOFchar; } - String getToken() throws IOException { + String getToken() { int nextState = 1; @@ -100,7 +80,7 @@ return buf.substring(0,index); } } else { - throw new IOException("Faild read string"); + return null; } } diff -r d8d0855bcdfd -r d655c8fef734 src/howtouse/WikiLinkParser.java --- a/src/howtouse/WikiLinkParser.java Wed Aug 22 01:44:26 2012 +0900 +++ b/src/howtouse/WikiLinkParser.java Wed Aug 22 01:58:54 2012 +0900 @@ -1,44 +1,34 @@ package howtouse; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.FileInputStream; -import java.io.File; -import java.io.StringReader; +import java.util.HashSet; public class WikiLinkParser { - public static void main(String[] args) { - -/* - BufferedReader breader = new BufferedReader(new InputStreamReader - (new FileInputStream(new File("./resource/article.xml")))); -*/ - + String text = "asdfadf [[link1]]sadf;lkjadsf[[link2]]\n;lkjsadfkj[[link3|link_err]]"; - StringReader sreader = new StringReader(text); - BufferedReader breader = new BufferedReader(sreader); - - CharReader reader = new CharReader(breader); - String str; - - try { - while ( (str = reader.getToken()) != null) { - System.out.println(str); - } - } catch (IOException e) { - e.printStackTrace(); + WikiLinkParser parser = new WikiLinkParser(); + HashSet hash; + hash = parser.parse(text); + for (String link : hash) { + System.out.println(link); } - - } - - - + private CharReader reader; + + WikiLinkParser() { + reader = new CharReader(); + } - - + public HashSet parse(String text) { + HashSet hash = new HashSet(); + + reader.setText(text); + String str; + while ( (str = reader.getToken()) != null) hash.add(str); + + return hash; + + } }