KickJava   Java API By Example, From Geeks To Geeks.

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


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 Smart 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-2003 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 import org.compiere.util.*;
19
20 /**
21  * File Utilities
22  *
23  * @author Jorg Janke
24  * @version $Id: FileUtil.java,v 1.1 2003/07/21 05:10:29 jjanke Exp $
25  */

26 public class FileUtil
27 {
28     /**
29      * File Utility
30      * @param file input file or directory
31      * @param filter filter
32      * @param action action
33      */

34     public FileUtil(String JavaDoc file, String JavaDoc filter, String JavaDoc action)
35     {
36         this (new File(file), filter, action);
37     } // FileUtil
38

39     /**
40      * File Utility
41      * @param file input file or directory
42      * @param filter filter
43      * @param action action
44      */

45     public FileUtil (File file, String JavaDoc filter, String JavaDoc 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     } // FileUtil
63

64     private String JavaDoc 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 JavaDoc[] ACTIONS = new String JavaDoc[]
70         {"List", "Replace", "Latex"};
71
72
73     /**
74      * Is Action Valid
75      * @param action action
76      * @return true if supported
77      */

78     private boolean validAction (String JavaDoc 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     } // validAction
90

91     /**
92      * Process File
93      * @param file file
94      */

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     } // processFile
114

115     /**
116      * File Action
117      * @param file file to be processed
118      */

119     void processFileAction(File file)
120     {
121         try
122         {
123             if (m_actionIndex == 0) // List
124
;
125             else if (m_actionIndex == 1) // Replace
126
replaceString (file, "C_BPartner_Contact_ID", "AD_User_ID");
127             else if (m_actionIndex == 2) // Latex
128
latex (file);
129         }
130         catch (Exception JavaDoc ex)
131         {
132         }
133     } // processFileAction
134

135     /*************************************************************************/
136
137     /**
138      * Replace String in File.
139      * @param file file
140      * @param from old String
141      * @param to new String
142      * @throws IOException
143      */

144     private void replaceString (File file, String JavaDoc from, String JavaDoc to) throws IOException
145     {
146         String JavaDoc fileName = file.getAbsolutePath();
147         BufferedReader in = new BufferedReader(new FileReader(file));
148         //
149
File tmpFile = new File(fileName + ".tmp");
150         BufferedWriter out = new BufferedWriter (new FileWriter(tmpFile, false));
151         boolean found = false;
152
153         String JavaDoc 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         } // while reading file
167
//
168
in.close();
169         out.close();
170         //
171
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     } // replaceString
195

196     /**
197      * Strip Latex specifics.
198      * \textsl{\colorbox{yellow}{\textbf{Important:}}} For more information on the
199         installation of the Compiere Server and the Compiere Client please refer to
200         \href{http://www.compiere.org/support/index.html}{Compiere Support} for more details and the latest
201         update.
202      * @param file file
203      * @throws IOException
204      */

205     private void latex (File file) throws IOException
206     {
207         String JavaDoc fileName = file.getAbsolutePath();
208         BufferedReader in = new BufferedReader(new FileReader(file));
209         //
210
File outFile = new File(fileName + ".txt");
211         BufferedWriter out = new BufferedWriter (new FileWriter(outFile, false));
212
213         String JavaDoc line = null;
214         int lineNo = 0;
215
216         while ((line = in.readLine()) != null)
217         {
218             lineNo++;
219             boolean ignore = false;
220             //
221
char[] inLine = line.toCharArray();
222             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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             //
236
out.write(sb.toString());
237             out.newLine();
238         } // while reading file
239
//
240
in.close();
241         out.close();
242         System.out.println("File " + fileName + " - lines=" + lineNo);
243     } // latex
244

245
246
247     /*************************************************************************/
248
249     /**
250      * File Filter.
251      * Accept directories and files matching filter
252      */

253     class FileUtilFilter implements FilenameFilter
254     {
255         /**
256          * Accept directories and files matching filter.
257          *
258          * @param dir the directory in which the file was found.
259          * @param name the name of the file.
260          * @return Accept directories and files matching filter
261          */

262         public boolean accept (File dir, String JavaDoc name)
263         {
264         // System.out.println(" Dir=" + dir + ", Name=" + name);
265
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             // ignore files with ~ and this file
273
if (name.indexOf("~") != -1 || name.equals("FileUtil.java"))
274                 return false;
275             //
276
return name.indexOf(m_filterString) != -1;
277         } // accept
278

279     } // FileUtilFilter
280

281     /*************************************************************************/
282
283     /**
284      * Start
285      * @param args fileName filter action
286      */

287     public static void main (String JavaDoc[] args)
288     {
289         /**
290         String directory = "C:\\Compiere\\compiere-all2";
291         String filter = ".java";
292         String action = "Replace";
293         **/

294         String JavaDoc directory = "C:\\Compiere\\documentation\\latex";
295         String JavaDoc filter = ".tex";
296         String JavaDoc 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     } // main
306

307 } // FileUtil
308
Popular Tags