private Strings loadFile(String encoding, File sourceFile) throws PreprocessorException { Strings lines = new Strings(); try { if (encoding != null && encoding.length() > 0) lines.loadFromFile(sourceFile, encoding); else lines.loadFromFile(sourceFile); } catch (java.io.UnsupportedEncodingException e) { throw new PreprocessorException("Unknown encoding \"" + encoding + "\"", sourceFile, e); } catch (java.io.IOException e) { throw new PreprocessorException("File read error", sourceFile, e); } return lines; }
// this method allow using a different filter to handle the lines. public void preprocess( IPreprocessor pp, File sourceDir, int mode, String newext, String encoding, Utility utility) throws PreprocessorException, IOException { pp.setMode(mode); String[] files = getDirectoryScanner(sourceDir).getIncludedFiles(); log("Preprocessing " + files.length + " file(s) at " + sourceDir); String filename = ""; // For exception handling try { for (int i = 0; i < files.length; i++) { filename = files[i]; String sourceFile = "" + sourceDir + File.separatorChar + filename; Strings lines = loadFile(encoding, new File(sourceFile)); pp.setFile(new File(sourceFile)); boolean modified = pp.preprocess(lines, encoding); // if preprocessing modifies the file, or // we are putting the output in a different directory // or we are changing the file extension // then we have to write a new file if (modified || !sourceDir.equals(targetDir) || (newext != null)) { try { if ((mode & IPreprocessor.MODE_VERBOSE) != 0) { System.out.println(filename + " ... modified"); } if ((mode & IPreprocessor.MODE_TEST) == 0) { String targetFile; if (newext != null) { int dot = filename.indexOf('.'); if (dot != -1) { filename = filename.substring(0, dot) + newext; } else { filename = filename + newext; } } targetFile = "" + targetDir + File.separatorChar + filename; File file = new File(targetFile + "~"); file.delete(); if (!new File(targetFile).renameTo(file) && (targetDir == null)) { throw new java.io.IOException(); } new File(targetFile).getParentFile().mkdirs(); if (encoding != null && encoding.length() > 0) { lines.saveToFile(targetFile, encoding); } else { lines.saveToFile(targetFile); } if ((mode & IPreprocessor.MODE_BACKUP) == 0) { file.delete(); // ?????? } } } catch (java.io.UnsupportedEncodingException uee) { throw new PreprocessorException("Unknown encoding \"" + encoding, new File(files[i])); } catch (java.io.IOException e) { throw new PreprocessorException("File write error", new File(files[i])); } } else { if ((mode & IPreprocessor.MODE_VERBOSE) != 0) { System.out.println(filename + " ... not modified"); } } } } catch (IOException e) { if ((mode & IPreprocessor.MODE_VERBOSE) == 0) { System.out.println(filename + " ... not modified, " + e.getMessage()); } throw e; } catch (PreprocessorException error) { if ((mode & IPreprocessor.MODE_VERBOSE) == 0) { System.out.println(filename + " ... not modified, " + error.getMessage()); } throw error; } }
package antenna.preprocessor.v3;