1 57 58 import java.io.*; 59 import org.apache.oro.text.regex.*; 60 61 74 public final class oroToApache { 75 public static final String PACKAGE_PATTERN = "com\\.oroinc\\.(io|text|util)"; 76 public static final String PACKAGE_SUBSTITUTION = "org.apache.oro.$1"; 77 public static final String OLD_FILE_EXTENSION = "_old"; 78 79 public static final class RenameException extends IOException { 80 public RenameException() { } 81 82 public RenameException(String message) { 83 super(message); 84 } 85 } 86 87 public static final class Converter { 88 Pattern _sourcePattern; 89 Perl5Matcher _matcher; 90 Perl5Substitution _substitution; 91 92 public static final int readFully(Reader reader, char[] buffer) 93 throws IOException 94 { 95 int offset, length, charsRead; 96 97 offset = 0; 98 length = buffer.length; 99 100 while(offset < buffer.length) { 101 charsRead = reader.read(buffer, offset, length); 102 if(charsRead == -1) 103 break; 104 offset+=charsRead; 105 length-=charsRead; 106 } 107 108 return offset; 109 } 110 111 public Converter(String patternString) throws MalformedPatternException { 112 Perl5Compiler compiler; 113 114 _matcher = new Perl5Matcher(); 115 compiler = new Perl5Compiler(); 116 _sourcePattern = compiler.compile(patternString); 117 _substitution = new Perl5Substitution(PACKAGE_SUBSTITUTION); 118 } 119 120 public void convertFile(String filename, String oldExtension) 121 throws FileNotFoundException, RenameException, SecurityException , 122 IOException 123 { 124 char[] inputBuffer; 125 int inputLength; 126 File srcFile, outputFile; 127 FileReader input; 128 FileWriter output; 129 String outputData; 130 131 srcFile = new File(filename); 132 input = new FileReader(srcFile); 133 outputFile = 134 File.createTempFile(srcFile.getName(), null, 135 srcFile.getAbsoluteFile().getParentFile()); 136 output = new FileWriter(outputFile); 137 138 inputBuffer = new char[(int)srcFile.length()]; 139 140 inputLength = readFully(input, inputBuffer); 141 input.close(); 142 143 outputData = 147 Util.substitute(_matcher, _sourcePattern, _substitution, 148 new String (inputBuffer), Util.SUBSTITUTE_ALL); 149 output.write(outputData); 150 output.close(); 151 152 if(!srcFile.renameTo(new File(srcFile.getAbsolutePath() + 153 OLD_FILE_EXTENSION))) 154 throw new RenameException("Could not rename " + srcFile.getPath() + 155 "."); 156 157 if(!outputFile.renameTo(srcFile)) 158 throw new RenameException("Could not rename temporary output file. " + 159 "Original file is in " + 160 srcFile.getAbsolutePath() + 161 OLD_FILE_EXTENSION); 162 } 163 } 164 165 public static final void main(String [] args) { 166 int file; 167 Converter converter; 168 169 if(args.length < 1) { 170 System.err.println("usage: oroToApache [file ...]"); 171 return; 172 } 173 174 try { 175 converter = new Converter(PACKAGE_PATTERN); 176 } catch(MalformedPatternException mpe) { 177 mpe.printStackTrace(); 179 return; 180 } 181 182 for(file = 0; file < args.length; file++) { 183 try { 184 System.out.println("Converting " + args[file]); 185 converter.convertFile(args[file], OLD_FILE_EXTENSION); 186 } catch(FileNotFoundException fnfe) { 187 System.err.println("Error: Could not open file. Skipping " + 188 args[file]); 189 } catch(RenameException re) { 190 System.err.println("Error: " + re.getMessage()); 191 } catch(SecurityException se) { 192 System.err.println("Error: Could not rename a file while processing" + 193 args[file] + ". Insufficient permission. " + 194 "File may not have been converted."); 195 } catch(IOException ioe) { 196 ioe.printStackTrace(); 197 System.err.println("Error: I/O exception while converting " + 198 args[file] + ". File not converted."); 199 } 200 } 201 } 202 } 203 | Popular Tags |