1 21 package proguard; 22 23 import java.io.*; 24 25 26 34 public abstract class WordReader 35 { 36 private static final char COMMENT_CHARACTER = '#'; 37 38 39 private File baseDir; 40 private WordReader includeWordReader; 41 private String currentLine; 42 private int currentLineLength; 43 private int currentIndex; 44 private String currentWord; 45 private String currentComments; 46 47 48 51 protected WordReader(File baseDir) 52 { 53 this.baseDir = baseDir; 54 } 55 56 57 60 public void setBaseDir(File baseDir) 61 { 62 if (includeWordReader != null) 63 { 64 includeWordReader.setBaseDir(baseDir); 65 } 66 else 67 { 68 this.baseDir = baseDir; 69 } 70 } 71 72 73 76 public File getBaseDir() 77 { 78 return includeWordReader != null ? 79 includeWordReader.getBaseDir() : 80 baseDir; 81 } 82 83 84 90 public void includeWordReader(WordReader newIncludeWordReader) 91 { 92 if (includeWordReader == null) 93 { 94 includeWordReader = newIncludeWordReader; 95 } 96 else 97 { 98 includeWordReader.includeWordReader(newIncludeWordReader); 99 } 100 } 101 102 103 109 public String nextWord() throws IOException 110 { 111 currentWord = null; 112 113 if (includeWordReader != null) 115 { 116 currentWord = includeWordReader.nextWord(); 118 if (currentWord != null) 119 { 120 return currentWord; 122 } 123 124 includeWordReader.close(); 126 includeWordReader = null; 127 } 128 129 131 while (currentLine != null && 133 currentIndex < currentLineLength && 134 Character.isWhitespace(currentLine.charAt(currentIndex))) 135 { 136 currentIndex++; 137 } 138 139 while (currentLine == null || currentIndex == currentLineLength) 141 { 142 currentLine = nextLine(); 143 if (currentLine == null) 144 { 145 return null; 146 } 147 148 int comments_start = currentLine.indexOf(COMMENT_CHARACTER); 150 if (comments_start >= 0) 151 { 152 currentLineLength = comments_start; 153 154 String comment = currentLine.substring(comments_start + 1); 156 currentComments = currentComments == null ? 157 comment : 158 currentComments + '\n' + comment; 159 } 160 else 161 { 162 currentLineLength = currentLine.length(); 163 } 164 165 currentIndex = 0; 167 while (currentIndex < currentLineLength && 168 Character.isWhitespace(currentLine.charAt(currentIndex))) 169 { 170 currentIndex++; 171 } 172 } 173 174 int startIndex = currentIndex; 176 int endIndex; 177 178 char startChar = currentLine.charAt(startIndex); 179 180 if (isDelimiter(startChar)) 181 { 182 endIndex = ++currentIndex; 184 } 185 else if (isQuote(startChar)) 186 { 187 startIndex++; 190 191 do 194 { 195 currentIndex++; 196 197 if (currentIndex == currentLineLength) 198 { 199 currentWord = currentLine.substring(startIndex-1, currentIndex); 200 throw new IOException("Missing closing quote for "+locationDescription()); 201 } 202 } 203 while (currentLine.charAt(currentIndex) != startChar); 204 205 endIndex = currentIndex++; 206 } 207 else 208 { 209 while (currentIndex < currentLineLength) 213 { 214 char currentCharacter = currentLine.charAt(currentIndex); 215 if (isDelimiter(currentCharacter) || 216 Character.isWhitespace(currentCharacter)) 217 { 218 break; 219 } 220 221 currentIndex++; 222 } 223 224 endIndex = currentIndex; 225 } 226 227 currentWord = currentLine.substring(startIndex, endIndex); 229 230 return currentWord; 231 } 232 233 234 240 public String lastComments() throws IOException 241 { 242 if (includeWordReader == null) 243 { 244 String comments = currentComments; 245 currentComments = null; 246 return comments; 247 } 248 else 249 { 250 return includeWordReader.lastComments(); 251 } 252 } 253 254 255 261 public String locationDescription() 262 { 263 return 264 (includeWordReader == null ? 265 (currentWord == null ? 266 "end of " : 267 "'" + currentWord + "' in " ) : 268 (includeWordReader.locationDescription() + ",\n" + 269 " included from ")) + 270 lineLocationDescription(); 271 } 272 273 274 280 protected abstract String nextLine() throws IOException; 281 282 283 288 protected abstract String lineLocationDescription(); 289 290 291 294 public void close() throws IOException 295 { 296 if (includeWordReader != null) 298 { 299 includeWordReader.close(); 300 includeWordReader = null; 301 } 302 } 303 304 305 307 private boolean isDelimiter(char character) 308 { 309 return character == '@' || 310 character == '{' || 311 character == '}' || 312 character == '(' || 313 character == ')' || 314 character == ',' || 315 character == ';' || 316 character == File.pathSeparatorChar; 317 } 318 319 320 private boolean isQuote(char character) 321 { 322 return character == '\'' || 323 character == '"'; 324 } 325 } 326 | Popular Tags |