1 18 package org.apache.tools.ant.util; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import org.apache.tools.ant.ProjectComponent; 23 24 33 public class StringTokenizer extends ProjectComponent implements Tokenizer { 34 private String intraString = ""; 35 private int pushed = -2; 36 private char[] delims = null; 37 private boolean delimsAreTokens = false; 38 private boolean suppressDelims = false; 39 private boolean includeDelims = false; 40 41 45 public void setDelims(String delims) { 46 this.delims = StringUtils.resolveBackSlash(delims).toCharArray(); 47 } 48 49 54 55 public void setDelimsAreTokens(boolean delimsAreTokens) { 56 this.delimsAreTokens = delimsAreTokens; 57 } 58 63 public void setSuppressDelims(boolean suppressDelims) { 64 this.suppressDelims = suppressDelims; 65 } 66 67 73 public void setIncludeDelims(boolean includeDelims) { 74 this.includeDelims = includeDelims; 75 } 76 77 84 public String getToken(Reader in) throws IOException { 85 int ch = -1; 86 if (pushed != -2) { 87 ch = pushed; 88 pushed = -2; 89 } else { 90 ch = in.read(); 91 } 92 if (ch == -1) { 93 return null; 94 } 95 boolean inToken = true; 96 intraString = ""; 97 StringBuffer word = new StringBuffer (); 98 StringBuffer padding = new StringBuffer (); 99 while (ch != -1) { 100 char c = (char) ch; 101 boolean isDelim = isDelim(c); 102 if (inToken) { 103 if (isDelim) { 104 if (delimsAreTokens) { 105 if (word.length() == 0) { 106 word.append(c); 107 } else { 108 pushed = ch; 109 } 110 break; 111 } 112 padding.append(c); 113 inToken = false; 114 } else { 115 word.append(c); 116 } 117 } else { 118 if (isDelim) { 119 padding.append(c); 120 } else { 121 pushed = ch; 122 break; 123 } 124 } 125 ch = in.read(); 126 } 127 intraString = padding.toString(); 128 if (includeDelims) { 129 word.append(intraString); 130 } 131 return word.toString(); 132 } 133 134 137 public String getPostToken() { 138 return suppressDelims || includeDelims ? "" : intraString; 139 } 140 141 private boolean isDelim(char ch) { 142 if (delims == null) { 143 return Character.isWhitespace(ch); 144 } 145 for (int i = 0; i < delims.length; ++i) { 146 if (delims[i] == ch) { 147 return true; 148 } 149 } 150 return false; 151 } 152 } 153 | Popular Tags |