1 14 package org.compiere.tools; 15 16 import java.io.*; 17 18 import org.compiere.util.*; 19 20 26 public class FileUtil 27 { 28 34 public FileUtil(String file, String filter, String action) 35 { 36 this (new File(file), filter, action); 37 } 39 45 public FileUtil (File file, String filter, String action) 46 { 47 if (action == null || action.length() == 0) 48 System.err.println("FileUtil: No Action"); 49 else if (!validAction(action)) 50 System.err.println("FileUtil: Action not valid: " + action + ACTIONS); 51 else if (file == null) 52 System.err.println("FileUtil: No Input file"); 53 else if (!file.exists()) 54 System.err.println("FileUtil: Input file does not exist: " + file); 55 else 56 { 57 System.out.println("FileUtil (" + file + ", Filter=" + filter + ", Action=" + action + ")"); 58 m_filterString = filter; 59 processFile (file); 60 System.out.println("FileUtil Process count = " + m_count); 61 } 62 } 64 private String m_filterString = null; 65 private FileUtilFilter m_filter = new FileUtilFilter(); 66 private int m_count = 0; 67 private int m_actionIndex = -1; 68 69 public static final String [] ACTIONS = new String [] 70 {"List", "Replace", "Latex"}; 71 72 73 78 private boolean validAction (String action) 79 { 80 for (int i = 0; i < ACTIONS.length; i++) 81 { 82 if (ACTIONS[i].equals (action)) 83 { 84 m_actionIndex = i; 85 return true; 86 } 87 } 88 return false; 89 } 91 95 private void processFile (File file) 96 { 97 if (file == null) 98 return; 99 else if (!file.exists()) 100 return; 101 else if (file.isDirectory()) 102 { 103 File[] dirFiles = file.listFiles(m_filter); 104 for (int i = 0; i < dirFiles.length; i++) 105 processFile(dirFiles[i]); 106 } 107 else 108 { 109 System.out.println(" ProcessFile=" + file.getAbsolutePath()); 110 m_count++; 111 processFileAction(file); 112 } 113 } 115 119 void processFileAction(File file) 120 { 121 try 122 { 123 if (m_actionIndex == 0) ; 125 else if (m_actionIndex == 1) replaceString (file, "C_BPartner_Contact_ID", "AD_User_ID"); 127 else if (m_actionIndex == 2) latex (file); 129 } 130 catch (Exception ex) 131 { 132 } 133 } 135 136 137 144 private void replaceString (File file, String from, String to) throws IOException 145 { 146 String fileName = file.getAbsolutePath(); 147 BufferedReader in = new BufferedReader(new FileReader(file)); 148 File tmpFile = new File(fileName + ".tmp"); 150 BufferedWriter out = new BufferedWriter (new FileWriter(tmpFile, false)); 151 boolean found = false; 152 153 String line = null; 154 int lineNo = 0; 155 while ((line = in.readLine()) != null) 156 { 157 lineNo++; 158 if (line.indexOf(from) != -1) 159 { 160 found = true; 161 System.out.println(" " + lineNo + ": " + line); 162 line = Util.replace(line, from, to); 163 } 164 out.write(line); 165 out.newLine(); 166 } in.close(); 169 out.close(); 170 if (found) 172 { 173 File oldFile = new File (fileName + ".old"); 174 if (file.renameTo(oldFile)) 175 { 176 if (tmpFile.renameTo (new File (fileName))) 177 { 178 if (oldFile.delete ()) 179 System.out.println (" - File updated: " + fileName); 180 else 181 System.err.println (" - Old File not deleted - " + fileName); 182 } 183 else 184 System.err.println (" - New File not renamed - " + fileName); 185 } 186 else 187 System.err.println(" - Old File not renamed - " + fileName); 188 } 189 else 190 { 191 if (!tmpFile.delete()) 192 System.err.println(" - Temp file not deleted - " + tmpFile.getAbsolutePath()); 193 } 194 } 196 205 private void latex (File file) throws IOException 206 { 207 String fileName = file.getAbsolutePath(); 208 BufferedReader in = new BufferedReader(new FileReader(file)); 209 File outFile = new File(fileName + ".txt"); 211 BufferedWriter out = new BufferedWriter (new FileWriter(outFile, false)); 212 213 String line = null; 214 int lineNo = 0; 215 216 while ((line = in.readLine()) != null) 217 { 218 lineNo++; 219 boolean ignore = false; 220 char[] inLine = line.toCharArray(); 222 StringBuffer sb = new StringBuffer (); 223 for (int i = 0; i < inLine.length; i++) 224 { 225 char c = inLine[i]; 226 if (c == '\\') 227 ignore = true; 228 else if (c == '{') 229 ignore = false; 230 else if (c == '}') 231 ; 232 else if (!ignore) 233 sb.append(c); 234 } 235 out.write(sb.toString()); 237 out.newLine(); 238 } in.close(); 241 out.close(); 242 System.out.println("File " + fileName + " - lines=" + lineNo); 243 } 245 246 247 248 249 253 class FileUtilFilter implements FilenameFilter 254 { 255 262 public boolean accept (File dir, String name) 263 { 264 File file = new File (dir, name); 266 if (file.isDirectory()) 267 return true; 268 if (m_filterString == null || m_filterString.length() == 0) 269 return true; 270 if (name == null) 271 return false; 272 if (name.indexOf("~") != -1 || name.equals("FileUtil.java")) 274 return false; 275 return name.indexOf(m_filterString) != -1; 277 } 279 } 281 282 283 287 public static void main (String [] args) 288 { 289 294 String directory = "C:\\Compiere\\documentation\\latex"; 295 String filter = ".tex"; 296 String action = "Latex"; 297 298 if (args.length == 1) 299 directory = args[0]; 300 if (args.length == 2) 301 filter = args[1]; 302 if (filter == null) 303 filter = ""; 304 new FileUtil(directory, filter, action); 305 } 307 } | Popular Tags |