1 25 26 package net.killingar.wiki; 27 28 import net.killingar.wiki.impl.LineNode; 29 30 import java.io.BufferedReader ; 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.util.NoSuchElementException ; 34 35 public class Reader implements Parser 36 { 37 private BufferedReader inputReader; 38 private String nextBuffer; 39 private Throwable chucker; 40 private int lineCounter = 0; 41 42 private boolean cued; 43 44 public void setSource(Object o) 45 { 46 inputReader = new BufferedReader ((InputStreamReader )o); 47 } 48 49 public Reader(InputStreamReader reader) 50 { 51 inputReader = new BufferedReader (reader); 52 } 53 54 public Reader() 55 { 56 } 57 58 public boolean hasMore() 59 { 60 cued = true; 61 if (nextBuffer != null) 62 { 63 return true; 64 } 65 try 66 { 67 nextBuffer = inputReader.readLine(); 68 if (nextBuffer == null) 69 { 70 inputReader.close(); 71 return false; 72 } 73 nextBuffer += "\n"; } 75 catch (IOException e) 76 { 77 chucker = e; 78 } 79 return true; 84 85 } 86 87 public Node next() 88 { 89 if (cued == false) 90 { 91 throw new IllegalStateException ("Please call hasMore() before calling next()"); 92 } 93 94 cued = false; 95 96 if (chucker != null) 97 { 98 throw new ParserException(chucker); 99 } 100 101 if (nextBuffer == null) 102 { 103 throw new NoSuchElementException ("end of stream"); 104 } 105 106 Node node = new LineNode(nextBuffer, ++lineCounter); 107 nextBuffer = null; 108 return node; 109 } 110 } 111 | Popular Tags |