1 18 package org.apache.tools.ant.util; 19 20 import java.io.Reader ; 21 import java.io.IOException ; 22 23 import org.apache.tools.ant.ProjectComponent; 24 25 30 public class LineTokenizer extends ProjectComponent 31 implements Tokenizer { 32 private String lineEnd = ""; 33 private int pushed = -2; 34 private boolean includeDelims = false; 35 36 43 44 public void setIncludeDelims(boolean includeDelims) { 45 this.includeDelims = includeDelims; 46 } 47 48 55 public String getToken(Reader in) throws IOException { 56 int ch = -1; 57 if (pushed != -2) { 58 ch = pushed; 59 pushed = -2; 60 } else { 61 ch = in.read(); 62 } 63 if (ch == -1) { 64 return null; 65 } 66 67 lineEnd = ""; 68 StringBuffer line = new StringBuffer (); 69 70 int state = 0; 71 while (ch != -1) { 72 if (state == 0) { 73 if (ch == '\r') { 74 state = 1; 75 } else if (ch == '\n') { 76 lineEnd = "\n"; 77 break; 78 } else { 79 line.append((char) ch); 80 } 81 } else { 82 state = 0; 83 if (ch == '\n') { 84 lineEnd = "\r\n"; 85 } else { 86 pushed = ch; 87 lineEnd = "\r"; 88 } 89 break; 90 } 91 ch = in.read(); 92 } 93 if (ch == -1 && state == 1) { 94 lineEnd = "\r"; 95 } 96 97 if (includeDelims) { 98 line.append(lineEnd); 99 } 100 return line.toString(); 101 } 102 103 106 public String getPostToken() { 107 if (includeDelims) { 108 return ""; 109 } 110 return lineEnd; 111 } 112 113 } 114 115 | Popular Tags |