KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > tools > Strip


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.tools;
15
16 import java.io.*;
17
18 /**
19  * Strip Windows (cr/lf) Text files to Unix (cr) Text files
20  *
21  * @author Jorg Janke
22  * @version $Id: Strip.java,v 1.1 2003/07/21 05:10:29 jjanke Exp $
23  */

24 public class Strip
25 {
26     /** print more info if set to true */
27     private static final boolean VERBOSE = false;
28
29     /**
30      * Constructor
31      */

32     public Strip()
33     {
34     } // Strip
35

36     /*************************************************************************/
37
38     /**
39      * Strip a directory (and its subdirectories recursively)
40      * @param directory directory
41      * @param nameMustContain file name must include characters - e.g. .sh
42      * (do not include wildcards like *). If null, all files are stripped
43      */

44     public void stripDirectory (String JavaDoc directory, String JavaDoc nameMustContain)
45     {
46         if (directory == null)
47             throw new NullPointerException JavaDoc("Strip: directory cannot be null");
48         File dir = new File (directory);
49         if (!dir.exists() || !dir.isDirectory())
50             throw new IllegalArgumentException JavaDoc ("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 JavaDoc 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     } // stripDirectory
66

67
68     /**
69      * Strip infile to outfile
70      * @param infile input file
71      * @param outfile (can be null)
72      * @return true if copied
73      */

74     public boolean strip (String JavaDoc infile, String JavaDoc outfile)
75     {
76         if (infile == null)
77             throw new NullPointerException JavaDoc("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         //
83
return strip (in, out);
84     } // strip
85

86     /**
87      * Strip infile to outfile
88      * @param infile input file
89      * @param outfile if the output file is null, the infile is renamed to ".bak"
90      * @return true if copied
91      */

92     public boolean strip (File infile, File outfile)
93     {
94         if (infile == null)
95             throw new NullPointerException JavaDoc ("Strip: infile cannot ne null");
96         // infile
97
if (!infile.exists() || !infile.isFile())
98             throw new IllegalArgumentException JavaDoc ("Strip: infile does not exist or is not a file: " + infile);
99         System.out.println("Stripping file: " + infile);
100
101         // outfile
102
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         //
119
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 JavaDoc ("Strip output file cannot be created or written: " + outfile);
132
133         // copy it
134
if (!copy (infile, outfile))
135             return false;
136
137         // rename outfile
138
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     } // strip
151

152     /**
153      * Copy the file and strip
154      * @param infile input file
155      * @param outfile output file
156      * @returns true if success
157      */

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         //
171
FileOutputStream fos = null;
172         try
173         {
174             fos = new FileOutputStream(outfile, false); // no append
175
}
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) // lf
192
{
193                     fos.write(c);
194                     noOut++;
195                 }
196                 if (c == 13) // cr
197
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     } // stripIt
210

211     /*************************************************************************/
212
213     /**
214      * Strip file in args
215      * @param args infile outfile
216      */

217     public static void main(String JavaDoc[] args)
218     {
219         if (args.length == 0)
220         {
221             System.err.println("Syntax: Strip infile outfile");
222             System.exit(-1);
223         }
224         String JavaDoc p2 = null;
225         if (args.length > 1)
226             p2 = args[1];
227         //
228
new Strip().strip(args[0], p2);
229     } // main
230

231 } // Strip
232
Popular Tags