1 25 26 package net.killingar.wiki.parser; 27 28 import net.killingar.EscapedString; 29 import net.killingar.wiki.Node; 30 import net.killingar.wiki.Parser; 31 import net.killingar.wiki.ParserException; 32 import net.killingar.wiki.Reader; 33 import net.killingar.wiki.impl.LineNode; 34 import net.killingar.wiki.impl.SimpleNode; 35 import net.killingar.wiki.impl.TagNode; 36 37 import java.io.FileNotFoundException ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 public class ParserQueue implements Parser 42 { 43 private List parsers; 44 private int currentParser = 0; 45 private Object source; 46 47 50 public ParserQueue() 51 { 52 parsers = new ArrayList (); 53 } 54 55 public void queueParser(Parser inParser) 56 { 57 parsers.add(inParser); 58 if (parsers.size() == 1) 59 inParser.setSource(source); 60 } 61 62 public void setSource(Object o) 63 { 64 source = o; 65 } 66 67 70 public boolean hasMore() 71 { 72 if (currentParser == 0 && parsers.size() == 0) 73 return false; 74 75 if (currentParser >= parsers.size()) 76 throw new ParserException("parser depth error"); 77 78 boolean hasMore = ((Parser)parsers.get(currentParser)).hasMore(); 79 80 while (!hasMore && currentParser != 0) 81 { 82 currentParser--; 83 hasMore = ((Parser)parsers.get(currentParser)).hasMore(); 84 } 85 86 return hasMore; 87 } 88 89 public Node next() 90 { 91 if (currentParser >= parsers.size()) 92 throw new ParserException("parser depth error"); 93 94 Parser p = (Parser)parsers.get(currentParser); 95 96 Node node = (Node)p.next(); 97 98 while ( 99 currentParser < parsers.size()-1 && 100 (node.getClass() == SimpleNode.class || node.getClass() == LineNode.class)) 101 { 102 currentParser++; 103 p = (Parser)parsers.get(currentParser); 104 105 if (p.hasMore()) 107 throw new IllegalStateException ("parser "+p+" has nodes left, cannot continue"); 108 109 p.setSource(node); 110 if (p.hasMore()) 111 node = (Node)p.next(); 112 } 113 114 return node; 115 } 116 117 public static final void main(String [] args) throws FileNotFoundException 118 { 119 ParserQueue q = new ParserQueue(); 120 q.setSource(new java.io.FileReader ("C:\\foo.txt")); 122 123 q.queueParser(new Reader()); 124 q.queueParser(new WikiLinkParser()); 125 q.queueParser(new StringTagParser("__", "__") 126 { 127 protected Node createNode(int line, int startIndex, int endIndex, boolean start) 128 { 129 return new TagNode("u", start, line, startIndex, endIndex); 130 } 131 }); 132 q.queueParser(new StringTagParser("**", "**") 133 { 134 protected Node createNode(int line, int startIndex, int endIndex, boolean start) 135 { 136 return new TagNode("b", start, line, startIndex, endIndex); 137 } 138 }); 139 q.queueParser(new StringTagParser("~~", "~~") 140 { 141 protected Node createNode(int line, int startIndex, int endIndex, boolean start) 142 { 143 return new TagNode("i", start, line, startIndex, endIndex); 144 } 145 }); 146 q.queueParser(new ReplaceParser("<", "<")); 147 q.queueParser(new ReplaceParser(">", ">")); 148 149 150 152 while (q.hasMore()) 153 { 154 Node n = q.next(); 155 164 165 System.out.print(EscapedString.strip(n.getText())); 167 } 168 } 169 } 170 | Popular Tags |