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.impl.AbstractNode; 32 import net.killingar.wiki.impl.LineNode; 33 import net.killingar.wiki.impl.SimpleNode; 34 35 public abstract class StringTagParser implements Parser 36 { 37 private String parsableString; 38 private EscapedString data; 39 private int currentOffset = 0; 40 private int line = -1; 41 42 String startKeyword, endKeyword; 43 44 private int stage = 0; 45 46 49 public StringTagParser(String startKeyword, String endKeyword) 50 { 51 this.startKeyword = startKeyword; 52 this.endKeyword = endKeyword; 53 } 54 55 public StringTagParser(String startKeyword) 56 { 57 this(startKeyword, startKeyword); 58 } 59 60 private void setParsableString(String inParsableString) 61 { 62 parsableString = inParsableString; 63 data = new EscapedString(parsableString); 64 currentOffset = 0; 65 } 66 67 public void setSource(Object o) 68 { 69 if (o instanceof String ) 70 setParsableString((String )o); 71 else if (o.getClass() == SimpleNode.class || o.getClass() == LineNode.class) 72 { 73 setParsableString(((AbstractNode)o).getText()); 74 line = ((AbstractNode)o).getLineNumber(); 75 } 76 else 77 throw new IllegalArgumentException ("unsupported type "+o.getClass()); 78 } 79 80 public boolean hasMore() 81 { 82 if (parsableString == null) 83 return false; 84 85 return currentOffset < parsableString.length(); 86 } 87 88 public Node next() 89 { 90 if (currentOffset >= data.length()) 91 throw new IllegalStateException ("past end"); 92 93 Node n = null; 94 95 int foo = currentOffset; 96 if (stage == 0) 97 foo = data.indexOf(startKeyword, currentOffset); 98 else if (stage == 2) 99 foo = data.indexOf(endKeyword, currentOffset); 100 101 if (foo != -1) 102 { 103 if (stage == 0) 104 { 105 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo); 106 stage++; 107 108 } 109 else if (stage == 1) 110 { 111 if (data.indexOf(endKeyword, currentOffset) == -1) { 113 stage = 0; 114 n = new SimpleNode(startKeyword, line, currentOffset, foo); 115 116 foo += startKeyword.length(); 117 } 118 else 119 { 120 n = createNode(line, currentOffset, foo-1, true); 121 122 foo += endKeyword.length(); 123 124 stage++; 125 } 126 } 127 else if (stage == 2) 128 { 129 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo); 130 131 stage++; 132 } 133 else if (stage == 3) 134 { 135 n = createNode(line, currentOffset, foo-1, false); 136 137 foo += endKeyword.length(); 138 139 stage = 0; 140 } 141 else 142 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo); 143 144 currentOffset = foo; 145 } 146 else 147 { 148 foo = parsableString.length(); 150 n = new SimpleNode(parsableString.substring(currentOffset, foo), line, currentOffset, foo); 151 152 currentOffset = foo+1; 153 } 154 155 return n; 156 } 157 158 abstract protected Node createNode(int line, int startIndex, int endIndex, boolean start); 159 } 160 | Popular Tags |