1 17 18 package org.objectweb.jac.util.preparse; 19 20 import java.io.BufferedReader ; 21 import java.io.BufferedWriter ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Writer ; 30 import java.util.Iterator ; 31 import java.util.Vector ; 32 33 public class Preparse 34 { 35 public static final String CFGNAME = "preparse.cfg"; 36 public static final String EXTENSION = ".modified"; 37 38 Vector tokenList = new Vector (); 39 40 boolean isInString = false; 42 43 boolean isInQuote = false; 45 46 boolean isEscaped = false; 48 49 int parenthLvl = 0; 51 52 int endLvl = -1; 54 55 protected void writeToOutput(String str, Writer output) 56 throws Exception 57 { 58 output.write(str); 59 } 60 61 66 protected int treat(String line, int i, Writer output) 67 throws Exception 68 { 69 int lineLength = line.length(); 70 71 73 if (!isInString) 74 { 75 Iterator j = tokenList.iterator(); 76 while (j.hasNext()) 77 { 78 Config token = (Config) j.next(); 79 80 int tokenLength = token.token.length(); 81 if ((tokenLength < lineLength - i + 1) 83 && (line.substring(i, i + tokenLength) 84 .equals(token.token))) 85 { 86 if (token.hasArguments) 87 endLvl = parenthLvl; 88 writeToOutput("/* a " + token.token + " was here */", output); 89 return (i + tokenLength - 1); 91 } 92 } 93 } 94 98 switch (line.charAt(i)) 99 { 100 case '\"' : 101 if ((!isEscaped) && (!isInQuote)) { 102 isInString = !isInString; 103 } 105 isEscaped = false; 106 break; 107 case '\'': 108 if ((!isInString) && (!isEscaped)) { 109 isInQuote = !isInQuote; 110 } 112 isEscaped = false; 113 break; 114 case '\\': 115 if (isInString || isInQuote) { 116 isEscaped = true; 117 } 119 break; 120 case '(': 121 if (!isInString) { 122 parenthLvl++; 123 } 125 isEscaped = false; 126 break; 127 case ')': 128 if (!isInString) 129 { 130 parenthLvl--; 131 132 134 if ((endLvl != -1) && (parenthLvl == endLvl)) 135 { 136 endLvl = -1; 137 return i; 138 } 139 } 140 isEscaped = false; 141 break; 142 default: 143 isEscaped = false; 144 break; 145 } 146 147 if (endLvl == -1) { 148 output.write(line.charAt(i)); 149 } else { 151 } 153 return i; 154 } 155 156 int lineNumber = 0; 157 158 public void parse(ParseInput input, Writer output) 159 { 160 String line; 161 162 lineNumber = 0; 163 try { 164 line = input.readLine(); 165 lineNumber++; 167 while (line != null) 168 { 169 int lineLength = line.length(); 170 171 for (int i = 0; i < lineLength ; i++) 172 { 173 if ((!isInString) && 174 ((line.charAt(i) == '/') && (i < lineLength - 1))) 175 { 176 if (line.charAt(i + 1) == '/') 178 { 179 writeToOutput(line.substring(i), output); 181 break; 182 } 183 184 else if (line.charAt(i + 1) == '*') 186 { 187 writeToOutput(input.skipTo(i, "*/"), output); 189 break; 190 } 191 else 192 i = treat(line, i, output); 193 } 194 else 195 i = treat(line, i, output); 196 } 197 if (!input.isModified()) 198 output.write('\n'); 199 line = input.readLine(); 200 lineNumber++; 201 } 203 } 204 catch (Exception e) 205 { 206 e.printStackTrace(); 207 } 208 } 209 210 protected String [] getTokens(String str) 211 throws Exception 212 { 213 if (str == null) 214 return null; 215 216 if (str.length() == 0) 217 return new String [] {}; 218 219 Vector result = new Vector (); 220 221 int begin = 0; 222 int end = 0; 223 224 int strLength = str.length(); 225 226 while (begin < strLength) 227 { 228 while ((begin < strLength) 229 && ((str.charAt(begin) == ' ') || (str.charAt(begin) == '\t'))) 230 begin++; 231 232 end = begin; 233 while ((end < strLength) 234 && ((str.charAt(end) != ' ') && (str.charAt(end) != '\t'))) 235 end++; 236 result.add(str.substring(begin, end)); 237 begin = end + 1; 238 } 239 return (String []) result.toArray(new String [] {}); 240 } 241 242 public void readConfigFile(String filename) 243 throws Exception 244 { 245 try { 246 BufferedReader config = 247 new BufferedReader ( 248 new FileReader (filename)); 249 250 String line = config.readLine(); 251 while (line != null) 252 { 253 line = line.trim(); 254 if ((line.length() > 0) && (line.charAt(0) != '#')) { 255 String [] tokens = getTokens(line); 256 tokenList.add(new Config(tokens[0],!tokens[1].equals("0"))); 257 } 258 line = config.readLine(); 259 } 260 config.close(); 261 } 262 catch (Exception e) 263 { 264 e.printStackTrace(); 265 throw new Exception ("problem with config file '" 266 + filename 267 +"' : " + e.getMessage()); 268 } 269 } 270 271 272 public String modifName(String javaName) 273 { 274 int i = javaName.length() - 1; 275 while ((i >= 0) && (javaName.charAt(i) != '.')) 276 i--; 277 if (i == 0) 278 return javaName + Preparse.EXTENSION; 279 return javaName.substring(0, i) + Preparse.EXTENSION; 280 } 281 282 285 public static void main(String [] args) 286 throws Exception 287 { 288 Preparse parser = new Preparse(); 289 290 if (args.length == 0) 291 { 292 System.err.println("Error: a configuration file must be specified"); 293 return; 294 } 295 296 parser.readConfigFile(args[0]); 297 298 String msg = new String (); 299 for (int i = 1; i < args.length; i++) 300 msg += args[i] + " "; 301 302 File destDir = new File (args[1]); 303 if (!destDir.exists()) { 304 destDir.mkdirs(); 305 } 306 307 for (int i=2; i<args.length; i++) 308 { 309 try { 310 File inputFile = new File (args[i]); 312 File outputFile = new File (destDir,args[i]); 313 if (inputFile.lastModified()>outputFile.lastModified()) { 314 ParseInput input = 315 new ParseInput( 316 new InputStreamReader ( 317 new FileInputStream (inputFile), 318 "UTF-8")); 319 System.out.println("Preparse: "+inputFile+" -> "+outputFile); 320 File outputDir = outputFile.getParentFile(); 321 if (!outputDir.exists()) { 322 outputDir.mkdirs(); 323 } 324 Writer output = 325 new BufferedWriter ( 326 new OutputStreamWriter ( 327 new FileOutputStream (outputFile), 328 "UTF-8")); 329 330 parser.parse(input, output); 331 input.close(); 332 output.close(); 333 } 334 } 335 catch (Exception e) { 336 System.err.println("Error: " + e.getMessage()); 337 } 338 } 339 } 340 341 static class Config { 342 public String token; 343 public boolean hasArguments; 344 public Config(String token, boolean hasArguments) { 345 this.token = token; 346 this.hasArguments = hasArguments; 347 } 348 } 349 } 350 | Popular Tags |