📄 sed.java
字号:
String result = ""; String regex; Vector delPatternVector = new Vector(); Vector subPatternVector = new Vector(); // --------------------------------- // Try loading the sed properties if they exist (see jdbc_sed.properties as an example) if ( isSed != null ) { Properties sedp = new Properties(); sedp.load(isSed); for (Enumeration e = sedp.propertyNames(); e.hasMoreElements(); ) { String key = (String)e.nextElement(); if (key.equals("substitute")) { String value = sedp.getProperty(key); // value string contains a comma separated list of patterns StringTokenizer st = new StringTokenizer(value,","); String patternName = ""; String patName = ""; String subName = ""; while (st.hasMoreTokens()) { patternName = st.nextToken(); // pattern;substitute StringTokenizer st2 = new StringTokenizer(patternName,";"); patName = st2.nextToken(); subName = st2.nextToken(); if (!patName.equals("") && !subName.equals("")) { searchStrings.addElement(patName); subStrings.addElement(subName); } //System.out.println("pattern = " + patName + " substitute " + subName); } } else if (key.equals("delete")) { String value = sedp.getProperty(key); // value string contains a comma separated list of patterns StringTokenizer st = new StringTokenizer(value,","); String patternName = ""; while (st.hasMoreTokens()) { patternName = st.nextToken(); deleteLines.addElement(patternName); } } } } // --------------------------------- //Create Perl5Compiler and Perl5Matcher pcompiler = new Perl5Compiler(); matcher = new Perl5Matcher(); // Define the input and output files based on args if (is == null) inFile = new BufferedReader(new FileReader(srcFile)); else inFile = new BufferedReader(new InputStreamReader(is, "UTF-8")); outFile = new PrintWriter ( new BufferedWriter(new FileWriter(dstFile), 10000), true ); // Attempt to compile the patterns for deletes for (int i = 0; i < deleteLines.size(); i++) { try { regex = (String)deleteLines.elementAt(i); //System.out.println("The pattern: " + regex); Pattern pattern = pcompiler.compile(regex); if (pattern == null) System.out.println("pattern is null"); delPatternVector.addElement(pattern); } catch(MalformedPatternException e) { System.out.println("Bad pattern."); System.out.println(e.getMessage()); } } // Attempt to compile the patterns for substitutes for (int i = 0; i < searchStrings.size(); i++) { try { regex = (String)searchStrings.elementAt(i); //System.out.println("The pattern: " + regex); Pattern pattern = pcompiler.compile(regex); if (pattern == null) System.out.println("pattern is null"); subPatternVector.addElement(pattern); } catch(MalformedPatternException e) { System.out.println("Bad pattern."); System.out.println(e.getMessage()); } } String str; int j; int lineCount = 0; // Read the input file while ( (str = inFile.readLine()) != null ) { lineCount++; //System.out.println("***Line no: " + lineCount); //System.out.println("***Line is: " + str); lineDeleted = false; // First delete any nulls (Cafe 1.8 leaves nulls) if (str.length() == 1) { if (str.charAt(0) == (char) 0) { // Skip this line, don't write it //System.out.println("Skip this line..."); lineDeleted = true; } } // Now determine if & if so, replace, any non-ascii characters // We do this because non-ascii characters in .sql files will // result in different characters depending on encoding, and // encoding may be different on different os's if (isI18N) { boolean hasNonAscii = false; // check for any characters in the control range for (int si = 0; si < str.length(); si++) { char c = str.charAt(si); if (c < (char) 0x20 || c >= (char) 0x7f) { hasNonAscii = true; break; } } if (hasNonAscii) { StringBuffer sb = new StringBuffer(); for (int si = 0; si < str.length(); si++) { char c = str.charAt(si); if (c < (char) 0x20 || c >= (char) 0x7f) { sb.append(' '); // Encoded Character:> ... < sb.append("EnC:>"); sb.append((int) str.charAt(si)); sb.append("< "); } else sb.append(c); } str = sb.toString(); } } // Determine if this line should be deleted for delete pattern match if ( lineDeleted == false ) { for (j = 0; j < delPatternVector.size(); j++) { if ( matcher.contains( str, (Pattern)delPatternVector.elementAt(j) ) ) { //System.out.println("***Match found to delete line***"); String tmpp = ((Pattern)delPatternVector.elementAt(j)).getPattern(); //System.out.println("***Pattern is: " + tmpp); // In this case we are removing the line, so don't write it out lineDeleted = true; break; } } } // Determine if any substitutions are needed if (lineDeleted == false) { Substitution substitution; StringSubstitution strsub = new StringSubstitution(""); Perl5Substitution perlsub = new Perl5Substitution(""); boolean subDone = false; for (j = 0; j < subPatternVector.size(); j++) { input = new PatternMatcherInput(str); Pattern patt = (Pattern)subPatternVector.elementAt(j); String pstr = patt.getPattern(); //System.out.println("Pattern string is " + pstr); String sub = (String)subStrings.elementAt(j); if (sub.indexOf("$") > 0) { perlsub.setSubstitution(sub); substitution = (Substitution)perlsub; } else { strsub.setSubstitution(sub); substitution = (Substitution)strsub; } //System.out.println("Substitute str = " + sub); if ( matcher.contains( input, patt ) ) { MatchResult mr = matcher.getMatch(); //System.out.println("***Match found for substitute***"); // In this case we do a substitute result = Util.substitute(matcher, patt, substitution, str, Util.SUBSTITUTE_ALL); //System.out.println("New string: " + result); //outFile.println(result); str = result; subDone = true; } } if (subDone) { //System.out.println("write the subbed line"); outFile.println(result); } else { //System.out.println("Write the str: " + str); outFile.println(str); outFile.flush(); } }// end if } // end while inFile.close(); outFile.flush(); outFile.close(); }// end doWork}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -