1 14 package org.compiere.tools; 15 16 import java.io.*; 17 18 24 public class Strip 25 { 26 27 private static final boolean VERBOSE = false; 28 29 32 public Strip() 33 { 34 } 36 37 38 44 public void stripDirectory (String directory, String nameMustContain) 45 { 46 if (directory == null) 47 throw new NullPointerException ("Strip: directory cannot be null"); 48 File dir = new File (directory); 49 if (!dir.exists() || !dir.isDirectory()) 50 throw new IllegalArgumentException ("Strip: directory does not exist or is not a directory: " + dir); 51 52 File[] list = dir.listFiles(); 53 if (list == null) 54 return; 55 if (VERBOSE) 56 System.out.println("Stripping directory: " + dir); 57 for (int i = 0; i < list.length; i++) 58 { 59 String name = list[i].getAbsolutePath(); 60 if (list[i].isDirectory()) 61 stripDirectory (name, nameMustContain); 62 else if (nameMustContain == null || name.indexOf(nameMustContain) != -1) 63 strip (list[i], null); 64 } 65 } 67 68 74 public boolean strip (String infile, String outfile) 75 { 76 if (infile == null) 77 throw new NullPointerException ("Strip: infile cannot be null"); 78 File in = new File (infile); 79 File out = null; 80 if (outfile != null) 81 out = new File (outfile); 82 return strip (in, out); 84 } 86 92 public boolean strip (File infile, File outfile) 93 { 94 if (infile == null) 95 throw new NullPointerException ("Strip: infile cannot ne null"); 96 if (!infile.exists() || !infile.isFile()) 98 throw new IllegalArgumentException ("Strip: infile does not exist or is not a file: " + infile); 99 System.out.println("Stripping file: " + infile); 100 101 if (infile.equals(outfile)) 103 outfile = null; 104 boolean tempfile = false; 105 if (outfile == null) 106 { 107 try 108 { 109 outfile = File.createTempFile("strip", ".txt"); 110 } 111 catch (IOException ioe) 112 { 113 System.err.println(ioe); 114 return false; 115 } 116 tempfile = true; 117 } 118 try 120 { 121 if (VERBOSE) 122 System.out.println("Creating: " + outfile); 123 outfile.createNewFile(); 124 } 125 catch (IOException ioe) 126 { 127 System.err.println(ioe); 128 return false; 129 } 130 if (!outfile.exists() || !outfile.canWrite()) 131 throw new IllegalArgumentException ("Strip output file cannot be created or written: " + outfile); 132 133 if (!copy (infile, outfile)) 135 return false; 136 137 if (tempfile) 139 { 140 if (VERBOSE) 141 System.out.print("Renaming original: " + infile); 142 if (!infile.renameTo(new File(infile.getAbsolutePath() + ".bak"))) 143 System.err.println("Could not rename original file: " + infile); 144 if (VERBOSE) 145 System.out.println(" - Renaming: " + outfile + " to: " + infile); 146 if (!outfile.renameTo(infile)) 147 System.err.println("Could not rename " + outfile + " to: " + infile); 148 } 149 return true; 150 } 152 158 private boolean copy (File infile, File outfile) 159 { 160 FileInputStream fis = null; 161 try 162 { 163 fis = new FileInputStream(infile); 164 } 165 catch (FileNotFoundException fnfe) 166 { 167 System.err.println(fnfe); 168 return false; 169 } 170 FileOutputStream fos = null; 172 try 173 { 174 fos = new FileOutputStream(outfile, false); } 176 catch (FileNotFoundException fnfe) 177 { 178 System.err.println(fnfe); 179 return false; 180 } 181 182 int noIn = 0; 183 int noOut = 0; 184 int noLines = 1; 185 try 186 { 187 int c; 188 while ((c = fis.read()) != -1) 189 { 190 noIn++; 191 if (c != 10) { 193 fos.write(c); 194 noOut++; 195 } 196 if (c == 13) noLines++; 198 } 199 fis.close(); 200 fos.close(); 201 } 202 catch (IOException ioe) 203 { 204 System.err.println(ioe); 205 return false; 206 } 207 System.out.println(" read: " + noIn + ", written: " + noOut + " - lines: " + noLines); 208 return true; 209 } 211 212 213 217 public static void main(String [] args) 218 { 219 if (args.length == 0) 220 { 221 System.err.println("Syntax: Strip infile outfile"); 222 System.exit(-1); 223 } 224 String p2 = null; 225 if (args.length > 1) 226 p2 = args[1]; 227 new Strip().strip(args[0], p2); 229 } 231 } | Popular Tags |