1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 32 33 import java.io.*; 34 import java.util.Vector ; 35 import org.apache.oro.text.regex.*; 36 import java.util.Enumeration ; 37 import java.util.Properties ; 38 import java.util.StringTokenizer ; 39 import org.apache.derbyTesting.functionTests.util.TestUtil; 40 41 public class Sed 42 { 43 private static final String SQL_EXCEPTION_FILTERED_SUBSTITUTION = 44 "java.sql.SQLException:"; 45 46 public Sed() 47 { 48 } 49 50 public static void main(String [] args) throws Exception { 51 if (args == null || args.length != 2) { 52 System.err.println("Usage: Sed sourcefile targetfile"); 53 System.exit(1); 54 } 55 File src = new File(args[0]); 56 File tgt = new File(args[1]); 57 new Sed().exec(src,tgt,null, false, false,false); 58 } 59 60 public void exec 62 (File srcFile, File dstFile, InputStream isSed, boolean isJCC, boolean isI18N, boolean isJDBC4) 63 throws IOException 64 { 65 String hostName = TestUtil.getHostName(); 66 67 Vector deleteLines = new Vector (); 69 deleteLines.addElement("^ij version.*$"); 70 deleteLines.addElement("^\\*\\*\\*\\* Test Run Started .* \\*\\*\\*\\*$"); 71 deleteLines.addElement("^\\*\\*\\*\\* Test Run Completed .* \\*\\*\\*\\*$"); 72 deleteLines.addElement("^ELAPSED TIME = [0-9]* milliseconds$"); 73 deleteLines.addElement("^\\^\\?$"); 74 deleteLines.addElement("^S.*ij> $"); 76 deleteLines.addElement("^ *$"); 77 deleteLines.addElement("^Server StackTrace:$"); 78 deleteLines.addElement("^\\[ *$"); 79 deleteLines.addElement("^\\] *$"); 80 deleteLines.addElement("^\\[$"); 81 deleteLines.addElement("^\\]$"); 82 deleteLines.addElement("^<not available>\\]$"); 83 deleteLines.addElement("^(.*at .*)\\(.*:[0-9].*\\)$"); 84 deleteLines.addElement("^(.*at .*)\\(*.java\\)$"); 85 deleteLines.addElement("^(.*at .*)\\(Compiled Code\\)$"); 86 deleteLines.addElement("^(.*at .*)\\(Interpreted Code\\)$"); 87 deleteLines.addElement("^(.*at .*)\\(Unknown Source\\)$"); 88 deleteLines.addElement("^(.*at .*)\\(Native Method\\)$"); 89 deleteLines.addElement("^\\tat $"); deleteLines.addElement("optimizer estimated cost"); 91 deleteLines.addElement("optimizer estimated row count"); 92 deleteLines.addElement("Using executables built for native_threads"); 93 deleteLines.addElement("Estimate of memory used"); 94 deleteLines.addElement("Size of merge runs"); 95 deleteLines.addElement("Number of merge runs"); 96 deleteLines.addElement("Sort type"); 97 deleteLines.addElement("Optimization started at .*$"); 98 deleteLines.addElement("WARNING 02000: No row was found for FETCH, UPDATE or DELETE"); 99 deleteLines.addElement("Stack trace:"); 101 deleteLines.addElement("^.*java/.*\\<init\\>\\(.*\\)V"); 102 deleteLines.addElement("^.*org/apache/derby/.*\\(.*\\).*$"); 103 deleteLines.addElement("^.*java/.*\\(.*\\).*$"); 105 deleteLines.addElement("^\\[.*db2jcc.jar\\] [0-9].[1-9] - .*$"); 106 deleteLines.addElement("^\\[.*db2jcc_license_c.jar\\] [1-9].[0-9] - .*$"); 107 deleteLines.addElement("^XSDB.*$"); 108 109 deleteLines.addElement("^\\.*$"); 111 deleteLines.addElement("^Time: [0-9].*$"); 112 deleteLines.addElement("^OK \\(.*$"); 113 114 Vector searchStrings = new Vector (); 116 searchStrings.addElement("^Transaction:\\(.*\\) *\\|"); 117 searchStrings.addElement("^Read [0-9]* of [0-9]* bytes$"); 118 searchStrings.addElement("Directory .*connect.wombat.seg0"); 119 StringBuffer constraintNameFilter = new StringBuffer (); 121 constraintNameFilter.append("SQL[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"); 122 searchStrings.addElement(constraintNameFilter.toString()); 123 StringBuffer uuidFilter = new StringBuffer (); 125 uuidFilter.append("[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]-"); 126 uuidFilter.append("[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-"); 127 uuidFilter.append("[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-"); 128 uuidFilter.append("[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-"); 129 uuidFilter.append("[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"); 130 searchStrings.addElement(uuidFilter.toString()); 131 StringBuffer timestampFilter = new StringBuffer (); 133 timestampFilter.append( "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] " ); 134 timestampFilter.append( "[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]* *" ); 135 searchStrings.addElement( timestampFilter.toString() ); 136 timestampFilter = new StringBuffer (); 138 timestampFilter.append( "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] " ); 139 timestampFilter.append( "[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]* *" ); 140 searchStrings.addElement( timestampFilter.toString() ); 141 timestampFilter = new StringBuffer (); 143 timestampFilter.append( "[0-9]-[0-9][0-9]-[0-9][0-9] " ); 144 timestampFilter.append( "[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]* *" ); 145 searchStrings.addElement( timestampFilter.toString() ); 146 searchStrings.addElement("^ Waiting XID : {.*}"); 148 searchStrings.addElement("^ Granted XID : .*$"); 149 searchStrings.addElement("^The selected victim is XID : .*"); 150 searchStrings.addElement("(beta - )\\(([0-9]*)\\)"); 152 searchStrings.addElement("Level2CostEstimateImpl: .*"); 153 searchStrings.addElement("^Transaction ([0-9])* : \\(([0-9]*)\\,([0-9a-f]*)\\,([0-9a-f]*)\\)"); 155 searchStrings.addElement("^Modifying access paths using optimizer .[0-9]*"); 157 searchStrings.addElement("CDWS[0-9]*"); 158 searchStrings.addElement("IXWS[0-9]*"); 159 searchStrings.addElement("FAILED STACK MAP"); 161 if (isJCC) 162 { 163 searchStrings.addElement("[ ]*\\|"); 164 searchStrings.addElement("^--*"); 165 } 166 167 searchStrings.addElement("Directory.*.wombat.already.exists"); 169 searchStrings.addElement("Directory.*.extinout/crwombatlog/log.*.exists"); 170 171 searchStrings.addElement(" DB2ConnectionCorrelator: [0-9A-Z.]*"); 174 searchStrings.addElement("org.xml.sax.SAX.*$"); 176 searchStrings.addElement(hostName); 178 179 if ( isJDBC4 ) 180 { 181 searchStrings.addElement("java.sql.SQLDataException:"); 184 searchStrings.addElement("java.sql.SQLDataSetSyncException:"); 185 searchStrings.addElement("java.sql.SQLException:"); 186 searchStrings.addElement("java.sql.SQLFeatureNotSupportedException:"); 187 searchStrings.addElement("java.sql.SQLIntegrityConstraintViolationException:"); 188 searchStrings.addElement("java.sql.SQLInvalidAuthorizationSpecException:"); 189 searchStrings.addElement("java.sql.SQLNonTransientConnectionException:"); 190 searchStrings.addElement("java.sql.SQLNonTransientException:"); 191 searchStrings.addElement("java.sql.SQLRuntimeException:"); 192 searchStrings.addElement("java.sql.SQLSyntaxErrorException:"); 193 searchStrings.addElement("java.sql.SQLTimeoutException:"); 194 searchStrings.addElement("java.sql.SQLTransactionRollbackException:"); 195 searchStrings.addElement("java.sql.SQLTransientConnectionException:"); 196 searchStrings.addElement("java.sql.SQLTransientException:"); 197 198 searchStrings.addElement("No suitable driver found for [0-9A-Za-z:]*"); 200 searchStrings.addElement("No suitable driver;[0-9A-Za-z:=]*"); 201 searchStrings.addElement("SQL Exception: No suitable driver"); 202 203 searchStrings.addElement("\\[\\.fffffffff\\]"); 205 } 206 207 Vector subStrings = new Vector (); 208 subStrings.addElement("Transaction:(XXX)|"); 209 subStrings.addElement("Read ... bytes"); 210 subStrings.addElement("Directory DBLOCATION/seg0"); 211 subStrings.addElement("xxxxGENERATED-IDxxxx"); 212 subStrings.addElement("xxxxFILTERED-UUIDxxxx"); 213 subStrings.addElement("xxxxxxFILTERED-TIMESTAMPxxxxx"); 214 subStrings.addElement("xxxxxxFILTERED-TIMESTAMPxxxxx"); 215 subStrings.addElement("xxxxxxFILTERED-TIMESTAMPxxxxx"); 216 subStrings.addElement(" Waiting XID : {WWW,QQQ}"); 218 subStrings.addElement(" Granted XID : {GGG.QQQ}..."); 219 subStrings.addElement("The selected victim is XID : VVV"); 220 subStrings.addElement("$1(xxXXxxFILTERED-BUILD-NUMBERxxXXxx)"); 222 subStrings.addElement("Level2CostEstimateImpl: xxXXxxFILTERED-INFORMATIONxxXXxx"); 223 subStrings.addElement("Transaction $1 : ($2,FILTERED,FILTERED)"); 225 subStrings.addElement("Modifying access paths using optimizer FILTERED_NUMBER"); 227 subStrings.addElement("CDWSno"); 228 subStrings.addElement("IXWSno"); 229 subStrings.addElement(""); 231 if (isJCC) 234 { 235 subStrings.addElement(" |"); 236 subStrings.addElement("-----"); 237 } 238 subStrings.addElement("Directory DBLOCATION/wombat already exists"); 239 subStrings.addElement("Directory 'extinout<sp>crwombatlog<sp>log' exists"); 240 subStrings.addElement(""); 242 subStrings.addElement("xxxFILTERED-SAX-EXCEPTIONxxx'."); 244 subStrings.addElement("xxxFILTERED_HOSTNAMExxx"); 246 247 if ( isJDBC4 ) 248 { 249 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 250 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 251 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 252 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 253 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 254 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 255 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 256 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 257 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 258 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 259 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 260 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 261 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 262 subStrings.addElement(SQL_EXCEPTION_FILTERED_SUBSTITUTION); 263 264 subStrings.addElement("No suitable driver"); 265 subStrings.addElement("No suitable driver"); 266 subStrings.addElement("java.sql.SQLException: No suitable driver"); 267 268 subStrings.addElement(".fffffffff"); 269 } 270 271 doWork(srcFile, dstFile, null, deleteLines, searchStrings, subStrings, isSed, isI18N); 272 273 } 275 public void execJCC(InputStream is, File dstFile) 277 throws IOException 278 { 279 Vector deleteLines = new Vector (); 281 282 Vector searchStrings = new Vector (); 284 searchStrings.addElement("[ ]*\\|"); 285 searchStrings.addElement("^--*"); 286 287 Vector subStrings = new Vector (); 288 subStrings.addElement(" |"); 291 subStrings.addElement("-----"); 292 293 doWork(null, dstFile, is, deleteLines, searchStrings, subStrings, null); 294 295 } 296 297 private void doWork(File srcFile, File dstFile, InputStream is, Vector deleteLines, 298 Vector searchStrings, Vector subStrings, InputStream isSed) 299 throws IOException 300 { 301 doWork(srcFile, dstFile, is, deleteLines, searchStrings, subStrings, isSed, false); 302 } 303 304 305 private void doWork(File srcFile, File dstFile, InputStream is, Vector deleteLines, 306 Vector searchStrings, Vector subStrings, InputStream isSed, boolean isI18N) 307 throws IOException 308 { 309 310 boolean lineDeleted = false; 311 PatternMatcher matcher; 312 Perl5Compiler pcompiler; 313 PatternMatcherInput input; 314 BufferedReader inFile; 315 PrintWriter outFile; 316 String result = ""; 317 String regex; 318 Vector delPatternVector = new Vector (); 319 Vector subPatternVector = new Vector (); 320 321 if ( isSed != null ) 324 { 325 Properties sedp = new Properties (); 326 327 sedp.load(isSed); 328 for (Enumeration e = sedp.propertyNames(); e.hasMoreElements(); ) 329 { 330 String key = (String )e.nextElement(); 331 if (key.equals("substitute")) 332 { 333 String value = sedp.getProperty(key); 334 StringTokenizer st = new StringTokenizer (value,","); 336 String patternName = ""; 337 String patName = ""; 338 String subName = ""; 339 while (st.hasMoreTokens()) 340 { 341 patternName = st.nextToken(); 342 StringTokenizer st2 = new StringTokenizer (patternName,";"); 344 patName = st2.nextToken(); 345 subName = st2.nextToken(); 346 if (!patName.equals("") && !subName.equals("")) 347 { 348 searchStrings.addElement(patName); 349 subStrings.addElement(subName); 350 } 351 } 353 } 354 else if (key.equals("delete")) 355 { 356 String value = sedp.getProperty(key); 357 StringTokenizer st = new StringTokenizer (value,","); 359 String patternName = ""; 360 while (st.hasMoreTokens()) 361 { 362 patternName = st.nextToken(); 363 deleteLines.addElement(patternName); 364 } 365 } 366 } 367 } 368 370 pcompiler = new Perl5Compiler(); 372 matcher = new Perl5Matcher(); 373 374 if (is == null && isI18N) { 376 InputStream fs = new FileInputStream(srcFile); 378 inFile = new BufferedReader(new InputStreamReader(fs, "UTF-8")); 379 } else if (is == null) { 380 inFile = new BufferedReader(new FileReader(srcFile)); 382 } else { 383 inFile = new BufferedReader(new InputStreamReader(is, "UTF-8")); 384 } 385 outFile = new PrintWriter 386 ( new BufferedWriter(new FileWriter(dstFile), 10000), true ); 387 388 for (int i = 0; i < deleteLines.size(); i++) 390 { 391 try 392 { 393 regex = (String )deleteLines.elementAt(i); 394 Pattern pattern = pcompiler.compile(regex); 396 if (pattern == null) 397 System.out.println("pattern is null"); 398 delPatternVector.addElement(pattern); 399 } 400 catch(MalformedPatternException e) 401 { 402 System.out.println("Bad pattern."); 403 System.out.println(e.getMessage()); 404 } 405 } 406 407 for (int i = 0; i < searchStrings.size(); i++) 409 { 410 try 411 { 412 regex = (String )searchStrings.elementAt(i); 413 Pattern pattern = pcompiler.compile(regex); 415 if (pattern == null) 416 System.out.println("pattern is null"); 417 subPatternVector.addElement(pattern); 418 } 419 catch(MalformedPatternException e) 420 { 421 System.out.println("Bad pattern."); 422 System.out.println(e.getMessage()); 423 } 424 } 425 426 String str; 427 int j; 428 int lineCount = 0; 429 while ( (str = inFile.readLine()) != null ) 431 { 432 lineCount++; 433 434 lineDeleted = false; 437 438 if (str.length() == 1) 440 { 441 if (str.charAt(0) == (char) 0) 442 { 443 lineDeleted = true; 446 } 447 } 448 449 if (isI18N) 454 { 455 boolean hasNonAscii = false; 456 for (int si = 0; si < str.length(); si++) 458 { 459 char c = str.charAt(si); 460 if (c < (char) 0x20 || c >= (char) 0x7f) 461 { 462 hasNonAscii = true; 463 break; 464 } 465 } 466 467 if (hasNonAscii) 468 { 469 StringBuffer sb = new StringBuffer (); 470 for (int si = 0; si < str.length(); si++) 471 { 472 char c = str.charAt(si); 473 if (c < (char) 0x20 || c >= (char) 0x7f) 474 { 475 sb.append(' '); 476 sb.append("EnC:>"); 478 sb.append((int) str.charAt(si)); 479 sb.append("< "); 480 } 481 else 482 sb.append(c); 483 } 484 str = sb.toString(); 485 } 486 } 487 488 if ( lineDeleted == false ) 490 { 491 for (j = 0; j < delPatternVector.size(); j++) 492 { 493 if ( matcher.contains( str, (Pattern)delPatternVector.elementAt(j) ) ) 494 { 495 String tmpp = ((Pattern)delPatternVector.elementAt(j)).getPattern(); 497 499 lineDeleted = true; 501 break; 502 } 503 } 504 } 505 506 if (lineDeleted == false) 508 { 509 Substitution substitution; 510 StringSubstitution strsub = new StringSubstitution(""); 511 Perl5Substitution perlsub = new Perl5Substitution(""); 512 boolean subDone = false; 513 for (j = 0; j < subPatternVector.size(); j++) 514 { 515 input = new PatternMatcherInput(str); 516 Pattern patt = (Pattern)subPatternVector.elementAt(j); 517 String pstr = patt.getPattern(); 518 String sub = (String )subStrings.elementAt(j); 520 if (sub.indexOf("$") > 0) 521 { 522 perlsub.setSubstitution(sub); 523 substitution = (Substitution)perlsub; 524 } else { 525 strsub.setSubstitution(sub); 526 substitution = (Substitution)strsub; 527 } 528 if ( matcher.contains( input, patt ) ) 530 { 531 MatchResult mr = matcher.getMatch(); 532 result = Util.substitute(matcher, patt, substitution, str, 535 Util.SUBSTITUTE_ALL); 536 str = result; 539 subDone = true; 540 } 541 } 542 if (subDone) 543 { 544 outFile.println(result); 546 } 547 else 548 { 549 outFile.println(str); 551 outFile.flush(); 552 } 553 } } inFile.close(); 556 outFile.flush(); 557 outFile.close(); 558 }} 560 | Popular Tags |