1 52 53 package com.go.trove.io; 54 55 import java.io.*; 56 57 75 public class SourceReader extends PushbackPositionReader { 76 public static final int ENTER_CODE = -2; 77 public static final int ENTER_TEXT = -3; 78 79 private static Reader createReader(Reader source, 80 String beginTag, String endTag) { 81 String [] tags = new String [4]; 82 int[] codes = new int[4]; 83 84 int i = 0; 85 tags[i] = "\r\n"; codes[i++] = '\n'; 87 tags[i] = "\r"; codes[i++] = '\n'; 88 89 if (beginTag != null && beginTag.length() > 0) { 90 tags[i] = beginTag; codes[i++] = ENTER_CODE; 91 } 92 93 if (endTag != null && endTag.length() > 0) { 94 tags[i] = endTag; codes[i++] = ENTER_TEXT; 95 } 96 97 if (i < 4) { 98 String [] newTags = new String [i]; 99 System.arraycopy(tags, 0, newTags, 0, i); 100 tags = newTags; 101 102 int[] newCodes = new int[i]; 103 System.arraycopy(codes, 0, newCodes, 0, i); 104 codes = newCodes; 105 } 106 107 return new UnicodeReader(new TagReader(source, tags, codes)); 108 } 109 110 private UnicodeReader mUnicodeReader; 111 private TagReader mTagReader; 112 private boolean mClosed = false; 113 114 private int mLine = 1; 116 117 private int mFirst; 118 119 private String mBeginTag; 120 private String mEndTag; 121 122 134 public SourceReader(Reader source, String beginTag, String endTag) { 135 this(source, beginTag, endTag, false); 136 } 137 138 151 public SourceReader(Reader source, String beginTag, String endTag, 152 boolean inCode) { 153 super(createReader(source, beginTag, endTag), 2); 154 mUnicodeReader = (UnicodeReader)in; 155 mTagReader = (TagReader)mUnicodeReader.getOriginalSource(); 156 157 boolean codeMode = ((beginTag == null || beginTag.length() == 0) || 158 inCode); 159 mFirst = (codeMode) ? ENTER_CODE : ENTER_TEXT; 160 161 mBeginTag = beginTag; 162 mEndTag = endTag; 163 } 164 165 public String getBeginTag() { 166 return mBeginTag; 167 } 168 169 public String getEndTag() { 170 return mEndTag; 171 } 172 173 176 public int read() throws IOException { 177 int c; 178 179 if (mFirst != 0) { 180 c = mFirst; 181 mFirst = 0; 182 } 183 else { 184 c = super.read(); 185 } 186 187 if (c == '\n') { 188 mLine++; 189 } 190 else if (c == ENTER_CODE) { 191 mUnicodeReader.setEscapesEnabled(true); 192 } 193 else if (c == ENTER_TEXT) { 194 mUnicodeReader.setEscapesEnabled(false); 195 } 196 197 return c; 198 } 199 200 public int getLineNumber() { 201 return mLine; 202 } 203 204 214 public int getEndPosition() { 215 int e = getNextPosition() - 1; 216 return (e < getStartPosition()) ? getStartPosition() : e; 217 } 218 219 public void ignoreTags(boolean ignore) { 220 mTagReader.setEscapesEnabled(!ignore); 221 } 222 223 public boolean isClosed() { 224 return mClosed; 225 } 226 227 public void close() throws IOException { 228 mClosed = true; 229 super.close(); 230 } 231 232 protected void unreadHook(int c) { 233 if (c == '\n') { 234 mLine--; 235 } 236 else if (c == ENTER_CODE) { 237 mUnicodeReader.setEscapesEnabled(false); 238 } 239 else if (c == ENTER_TEXT) { 240 mUnicodeReader.setEscapesEnabled(true); 241 } 242 } 243 244 247 public static void main(String [] arg) throws Exception { 248 Tester.test(arg); 249 } 250 251 private static class Tester { 252 public static void test(String [] arg) throws Exception { 253 String str = 254 "This is \\" + "u0061 test.\n" + 255 "This is \\" + "u00612 test.\n" + 256 "This is \\" + "u0061" + "\\" + "u0061" + " test.\n" + 257 "This is \\" + "u061 test.\n" + 258 "This is \\\\" + "u0061 test.\n" + 259 "This is \\" + "a test.\n" + 260 "Plain text <%put code here%> plain text.\n" + 261 "Plain text <\\" + "u0025put code here%> plain text.\n" + 262 "Plain text <%put code here\\" + "u0025> plain text.\n"; 263 264 Reader reader; 265 266 if (arg.length > 0) { 267 reader = new java.io.FileReader (arg[0]); 268 } 269 else { 270 System.out.println("\nOriginal:\n"); 271 272 reader = new StringReader(str); 273 274 int c; 275 while ( (c = reader.read()) != -1 ) { 276 System.out.print((char)c); 277 } 278 } 279 280 System.out.println("\nTest 1:\n"); 281 282 if (arg.length > 0) { 283 reader = new java.io.FileReader (arg[0]); 284 } 285 else { 286 reader = new StringReader(str); 287 } 288 289 SourceReader sr = new SourceReader(reader, "<%", "%>"); 290 291 int c; 292 while ( (c = sr.read()) != -1 ) { 293 System.out.print((char)c); 294 System.out.print("\t" + c); 295 System.out.print("\t" + sr.getLineNumber()); 296 System.out.print("\t" + sr.getStartPosition()); 297 System.out.println("\t" + sr.getEndPosition()); 298 } 299 300 System.out.println("\nTest 2:\n"); 301 if (arg.length > 0) { 302 reader = new java.io.FileReader (arg[0]); 303 } 304 else { 305 reader = new StringReader(str); 306 } 307 308 sr = new SourceReader(reader, null, null); 309 310 while ( (c = sr.read()) != -1 ) { 311 System.out.print((char)c); 312 System.out.print("\t" + c); 313 System.out.print("\t" + sr.getLineNumber()); 314 System.out.print("\t" + sr.getStartPosition()); 315 System.out.println("\t" + sr.getEndPosition()); 316 } 317 } 318 } 319 } 320 | Popular Tags |