KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > utility > ReNameUtil


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /*
66  * ReNameUtil.java
67  *
68  * Copyright 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.utility;
71
72 import com.jcorporate.expresso.core.db.DBConnection;
73 import com.jcorporate.expresso.core.db.DBConnectionPool;
74 import com.jcorporate.expresso.core.db.DBException;
75 import com.jcorporate.expresso.core.misc.ConfigManager;
76 import com.jcorporate.expresso.core.misc.ConfigurationException;
77 import com.jcorporate.expresso.core.misc.FileUtil;
78 import com.jcorporate.expresso.core.misc.StringUtil;
79
80 import java.io.BufferedReader JavaDoc;
81 import java.io.File JavaDoc;
82 import java.io.FileReader JavaDoc;
83 import java.io.FileWriter JavaDoc;
84 import java.io.IOException JavaDoc;
85 import java.io.InputStreamReader JavaDoc;
86 import java.io.PrintWriter JavaDoc;
87 import java.util.Enumeration JavaDoc;
88 import java.util.Hashtable JavaDoc;
89 import java.util.StringTokenizer JavaDoc;
90 import java.util.Vector JavaDoc;
91
92
93 /**
94  * Utility for renaming classes & dependant objects. Takes a "mapping"
95  * file that gives the "from/to" pairs, and a directory to start looking for
96  * files in - then all files in that directory are processed
97  *
98  * @author Michael Nash
99  */

100 public class ReNameUtil {
101     private static String JavaDoc thisClass = ("com.jcorporate.expresso.core.utilty." +
102             "ReNameUtil.");
103
104     /**
105      * Suffix to be appended to copy of the file as it's processed
106      */

107     /* deleted when done */
108     private static String JavaDoc suffix = ".tmp";
109     private static Hashtable JavaDoc translateTable = new Hashtable JavaDoc(3);
110     private static int totalEdits = 0;
111
112     /**
113      * Constructor
114      */

115     public ReNameUtil() {
116         super();
117     } /* ReNameUtil() */
118
119     /**
120      * @param fileName
121      */

122     public static void processDirs(String JavaDoc fileName)
123             throws IOException JavaDoc {
124         System.out.println("Processing directory " + fileName);
125
126         if (!fileName.endsWith("/")) {
127             fileName = fileName + "/";
128         }
129
130         Vector JavaDoc contents = getDir(fileName);
131         String JavaDoc oneItem = null;
132         File JavaDoc oneFile = null;
133
134         for (Enumeration JavaDoc e = contents.elements(); e.hasMoreElements();) {
135             oneItem = (String JavaDoc) e.nextElement();
136             oneFile = new File JavaDoc(fileName + oneItem);
137
138             /* If it's a directory.... */
139             if (oneFile.isDirectory()) {
140                 System.out.println("Processing subdirectory " + fileName +
141                         oneItem);
142
143                 /* Try processing it */
144                 processDirs(fileName + oneItem);
145
146                 if (getDir(fileName + oneItem).size() == 0) {
147                     oneFile = new File JavaDoc(fileName + oneItem);
148                     System.out.println("Empty directory " + oneItem);
149                 } /* if the dir is empty */
150
151             } else { /* if the entry is a directory */
152                 processFile(fileName + oneItem);
153             }
154         } /* for each entry in the directory */
155
156     } /* processDirs(String) */
157
158
159     /**
160      * Process a single file
161      *
162      * @param sourceFile Source file pathname
163      * @throws IOException If the copy fails due to an I/O error
164      */

165     public static void processFile(String JavaDoc sourceFile)
166             throws IOException JavaDoc {
167         String JavaDoc myName = (thisClass + "processFile(String)");
168
169         if (sourceFile.equals("")) {
170             throw new IOException JavaDoc(myName + ":File name empty - cannot" +
171                     " process.");
172         }
173         if (sourceFile.endsWith(suffix)) {
174             return;
175         }
176
177         String JavaDoc destFile = sourceFile + suffix;
178
179         if (sourceFile.equals(destFile)) {
180             throw new IOException JavaDoc(myName + ":Cannot copy file '" + sourceFile +
181                     "' to itself");
182         }
183
184         File JavaDoc dest = new File JavaDoc(destFile);
185
186         if (dest.exists()) {
187             if (!dest.delete()) {
188                 throw new IOException JavaDoc(myName + ":Unable to delete existing " +
189                         "destination file '" + destFile +
190                         "'. Logged in as " +
191                         System.getProperty("user.name"));
192             }
193         }
194
195         File JavaDoc source = new File JavaDoc(sourceFile);
196
197         if (!source.exists()) {
198             throw new IOException JavaDoc(myName + ":Source file '" + sourceFile +
199                     "' does not exist. Cannot process. Logged in as " +
200                     System.getProperty("user.name"));
201         }
202
203         FileWriter JavaDoc fout = new FileWriter JavaDoc(destFile);
204         PrintWriter JavaDoc bout = new PrintWriter JavaDoc(fout);
205         FileReader JavaDoc fin = new FileReader JavaDoc(sourceFile);
206         BufferedReader JavaDoc bin = new BufferedReader JavaDoc(fin);
207         String JavaDoc oneFrom = null;
208         String JavaDoc oneTo = null;
209         String JavaDoc oneLine = bin.readLine();
210         int replacements = 0;
211
212         while (oneLine != null) {
213             String JavaDoc newLine = oneLine;
214
215             for (Enumeration JavaDoc e = translateTable.keys(); e.hasMoreElements();) {
216                 oneFrom = (String JavaDoc) e.nextElement();
217
218                 if (oneLine.indexOf(oneFrom) >= 0) {
219                     oneTo = (String JavaDoc) translateTable.get(oneFrom);
220                     newLine = StringUtil.replace(newLine, oneFrom, oneTo);
221                     replacements++;
222                     totalEdits++;
223                 }
224             }
225
226             bout.println(newLine);
227             oneLine = bin.readLine();
228         }
229
230         bout.flush();
231         bin.close();
232         fin.close();
233         System.err.println("Made " + replacements + " in file " + sourceFile);
234
235         if (!dest.exists()) {
236             throw new IOException JavaDoc(myName +
237                     ":File processing failed: destination file" +
238                     " '" + destFile +
239                     "' does not exist after processing.");
240         }
241
242         /* Now remove the original and rename the copy back to the original */
243         FileUtil.moveFile(destFile, sourceFile);
244     } /* processFile(String) */
245
246
247     /**
248      * Return a vector of the file/dir names in any give directory
249      *
250      * @param dirName
251      * @throws IOException If the given name is not a directory or if
252      * an I/O error occurs when trying to read the file list
253      */

254     public static Vector JavaDoc getDir(String JavaDoc dirName)
255             throws IOException JavaDoc {
256         String JavaDoc myName = (thisClass + "getDir(String)");
257         File JavaDoc dirFile = new File JavaDoc(dirName);
258
259         if (!dirFile.isDirectory()) {
260             throw new IOException JavaDoc(myName + ":'" + dirName +
261                     "' is not a directory.");
262         }
263
264         String JavaDoc[] dir = dirFile.list();
265
266         if (dir == null) {
267             throw new IOException JavaDoc(myName + ":Null array reading directory " +
268                     " of " + dirName);
269         }
270
271         Vector JavaDoc fileList = new Vector JavaDoc(1);
272         String JavaDoc oneFileName = null;
273
274         for (int i = 0; i < dir.length; i++) {
275             oneFileName = dir[i].trim();
276             fileList.addElement(oneFileName);
277         }
278
279         return fileList;
280     } /* getDir(String) */
281
282
283     /**
284      * Get the file extension (after the ".")
285      *
286      * @param fileName Original full file name
287      * @return String Extension name
288      * @throws IOException If unable to allocate the file to get the extension
289      */

290     public static String JavaDoc getExtension(String JavaDoc fileName)
291             throws IOException JavaDoc {
292         String JavaDoc tempName = new File JavaDoc(fileName).getName();
293         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(tempName, ".");
294         stk.nextToken();
295
296         if (stk.hasMoreTokens()) {
297             return stk.nextToken();
298         } else {
299             return ("");
300         }
301     } /* getExtension(String) */
302
303
304     /**
305      * Get the path of a file name
306      *
307      * @param fileName Original pathname
308      * @return String Path portion of the pathname
309      */

310     public static String JavaDoc getPath(String JavaDoc fileName) {
311         StringBuffer JavaDoc path = new StringBuffer JavaDoc("/");
312         String JavaDoc nextToken;
313         StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(fileName, "/");
314
315         while (stk.hasMoreTokens()) {
316             nextToken = stk.nextToken();
317
318             if (stk.hasMoreTokens()) {
319                 path.append(nextToken);
320                 path.append("/");
321             }
322         }
323
324         return path.toString();
325     } /* getPath(String) */
326
327     /**
328      * @param transFileName
329      */

330     private static void readTransFile(String JavaDoc transFileName)
331             throws IOException JavaDoc {
332         FileReader JavaDoc fin = new FileReader JavaDoc(transFileName);
333         BufferedReader JavaDoc bin = new BufferedReader JavaDoc(fin);
334         String JavaDoc oneFrom = null;
335         String JavaDoc oneTo = null;
336         int currLine = 0;
337         String JavaDoc oneLine = bin.readLine();
338
339         while (oneLine != null) {
340             currLine++;
341
342             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneLine, "|");
343
344             if (!stk.hasMoreTokens()) {
345                 throw new IllegalArgumentException JavaDoc("Translation file must have " +
346                         "from|to pairs on each line - from 'from' found on line " +
347                         currLine);
348             }
349
350             oneFrom = stk.nextToken();
351
352             if (!stk.hasMoreTokens()) {
353                 throw new IllegalArgumentException JavaDoc("Translation file must have " +
354                         "from|to pairs on each line - no 'to' found on line " +
355                         currLine);
356             }
357
358             oneTo = stk.nextToken();
359             translateTable.put(oneFrom, oneTo);
360             oneLine = bin.readLine();
361         }
362
363         System.out.println("Read " + currLine + " translation pairs");
364     } /* readTransFile(String) */
365
366
367     /**
368      * @param configDir
369      * @param db
370      */

371     public static void processDB(String JavaDoc configDir, String JavaDoc db)
372             throws IOException JavaDoc, DBException,
373             ConfigurationException {
374
375         //initialize the db pool
376
ConfigManager.dbInitialize();
377
378         DBConnectionPool dbp = DBConnectionPool.getInstance(db);
379         DBConnection myConnection = dbp.getConnection("ReNameUtil");
380         String JavaDoc oneControllerClass = null;
381         String JavaDoc oneGroup = null;
382         String JavaDoc newControllerClass = null;
383         myConnection.execute("SELECT GroupName, ControllerClass FROM CONTROLLERSECURITY");
384
385         Vector JavaDoc updates = new Vector JavaDoc();
386
387         while (myConnection.next()) {
388             oneGroup = StringUtil.notNull(myConnection.getString(1));
389             oneControllerClass = StringUtil.notNull(myConnection.getString(2));
390
391             String JavaDoc oneFrom = null;
392             String JavaDoc oneTo = null;
393
394             for (Enumeration JavaDoc e = translateTable.keys(); e.hasMoreElements();) {
395                 oneFrom = (String JavaDoc) e.nextElement();
396
397                 if (oneControllerClass.indexOf(oneFrom) >= 0) {
398                     oneTo = (String JavaDoc) translateTable.get(oneFrom);
399                     newControllerClass = StringUtil.replace(oneControllerClass,
400                             oneFrom, oneTo);
401                     totalEdits++;
402                     updates.addElement(oneGroup + "|" + oneControllerClass +
403                             "|" + newControllerClass);
404                 }
405             }
406         } /* while not end of records */
407
408
409         String JavaDoc oneUpdate = null;
410
411         for (Enumeration JavaDoc ue = updates.elements(); ue.hasMoreElements();) {
412             oneUpdate = (String JavaDoc) ue.nextElement();
413
414             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneUpdate, "|");
415             oneGroup = stk.nextToken();
416             oneControllerClass = stk.nextToken();
417             newControllerClass = stk.nextToken();
418             myConnection.executeUpdate("UPDATE CONTROLLERSECURITY SET ControllerClass = '" +
419                     newControllerClass + "' where ControllerClass = '" +
420                     oneControllerClass + "' AND GroupName = '" + oneGroup +
421                     "'");
422         }
423
424         /* Now process DBOBJSECURITY table */
425         updates = new Vector JavaDoc();
426
427         String JavaDoc oneDBObjectName = null;
428         String JavaDoc oneMethodCode = null;
429         String JavaDoc newDBObjectName = null;
430         myConnection.execute("SELECT GroupName, DBObjectName, MethodCode FROM DBOBJSECURITY");
431
432         while (myConnection.next()) {
433             oneGroup = StringUtil.notNull(myConnection.getString(1));
434             oneDBObjectName = StringUtil.notNull(myConnection.getString(2));
435             oneMethodCode = StringUtil.notNull(myConnection.getString(3));
436
437             String JavaDoc oneFrom = null;
438             String JavaDoc oneTo = null;
439
440             for (Enumeration JavaDoc e = translateTable.keys(); e.hasMoreElements();) {
441                 oneFrom = (String JavaDoc) e.nextElement();
442
443                 if (oneDBObjectName.indexOf(oneFrom) >= 0) {
444                     oneTo = (String JavaDoc) translateTable.get(oneFrom);
445                     newDBObjectName = StringUtil.replace(oneDBObjectName,
446                             oneFrom, oneTo);
447                     totalEdits++;
448                     updates.addElement(oneGroup + "|" + oneDBObjectName + "|" +
449                             oneMethodCode + "|" + newDBObjectName);
450                 }
451             }
452         } /* while not end of records */
453
454         for (Enumeration JavaDoc ue2 = updates.elements(); ue2.hasMoreElements();) {
455             oneUpdate = (String JavaDoc) ue2.nextElement();
456
457             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneUpdate, "|");
458             oneGroup = stk.nextToken();
459             oneDBObjectName = stk.nextToken();
460             oneMethodCode = stk.nextToken();
461             newDBObjectName = stk.nextToken();
462             myConnection.executeUpdate("UPDATE DBOBJSECURITY SET DBObjectName = '" +
463                     newDBObjectName + "' where DBObjectName = '" +
464                     oneDBObjectName + "' AND GroupName = '" + oneGroup +
465                     "' AND MethodCode = '" + oneMethodCode + "'");
466         }
467
468         /* Now do DBOBJLIMIT */
469         updates = new Vector JavaDoc();
470         oneDBObjectName = null;
471         myConnection.execute("SELECT DBObjectName FROM DBOBJLIMIT");
472
473         while (myConnection.next()) {
474             oneDBObjectName = StringUtil.notNull(myConnection.getString(1));
475
476             String JavaDoc oneFrom = null;
477             String JavaDoc oneTo = null;
478
479             for (Enumeration JavaDoc e = translateTable.keys(); e.hasMoreElements();) {
480                 oneFrom = (String JavaDoc) e.nextElement();
481
482                 if (oneDBObjectName.indexOf(oneFrom) >= 0) {
483                     oneTo = (String JavaDoc) translateTable.get(oneFrom);
484                     newDBObjectName = StringUtil.replace(oneDBObjectName,
485                             oneFrom, oneTo);
486                     totalEdits++;
487                     updates.addElement(oneDBObjectName + "|" +
488                             newDBObjectName);
489                 }
490             }
491         } /* while not end of records */
492
493         for (Enumeration JavaDoc ue3 = updates.elements(); ue3.hasMoreElements();) {
494             oneUpdate = (String JavaDoc) ue3.nextElement();
495
496             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneUpdate, "|");
497             oneDBObjectName = stk.nextToken();
498             newDBObjectName = stk.nextToken();
499             myConnection.executeUpdate("UPDATE DBOBJLIMIT SET DBObjectName = '" +
500                     newDBObjectName + "' where DBObjectName = '" +
501                     oneDBObjectName + "'");
502         }
503
504         /* Now do SETUP */
505         updates = new Vector JavaDoc();
506
507         String JavaDoc oneSchemaClass = null;
508         String JavaDoc newSchemaClass = null;
509         myConnection.execute("SELECT SchemaClass FROM SETUP");
510
511         while (myConnection.next()) {
512             oneSchemaClass = StringUtil.notNull(myConnection.getString(1));
513
514             String JavaDoc oneFrom = null;
515             String JavaDoc oneTo = null;
516
517             for (Enumeration JavaDoc e = translateTable.keys(); e.hasMoreElements();) {
518                 oneFrom = (String JavaDoc) e.nextElement();
519
520                 if (oneSchemaClass.indexOf(oneFrom) >= 0) {
521                     oneTo = (String JavaDoc) translateTable.get(oneFrom);
522                     newSchemaClass = StringUtil.replace(oneSchemaClass,
523                             oneFrom, oneTo);
524                     totalEdits++;
525                     updates.addElement(oneSchemaClass + "|" + newSchemaClass);
526                 }
527             }
528         } /* while not end of records */
529
530         for (Enumeration JavaDoc ue3 = updates.elements(); ue3.hasMoreElements();) {
531             oneUpdate = (String JavaDoc) ue3.nextElement();
532
533             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(oneUpdate, "|");
534             oneSchemaClass = stk.nextToken();
535             newSchemaClass = stk.nextToken();
536             myConnection.executeUpdate("UPDATE SETUP SET SchemaClass = '" +
537                     newSchemaClass +
538                     "' where SchemaClass = '" +
539                     oneSchemaClass + "'");
540         }
541     } /* processDB(String, String) */
542
543
544     /**
545      * @param args
546      */

547     public static void main(String JavaDoc[] args) {
548         System.out.println("ReNameUtil");
549
550         BufferedReader JavaDoc ds = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
551
552         try {
553             System.out.print("Enter name of translation file:");
554
555             String JavaDoc transFile = ds.readLine();
556             System.out.print("Enter config dir:");
557
558             String JavaDoc configDir = ds.readLine();
559             System.out.print("Update (d)b or (f)iles?");
560
561             String JavaDoc whichUpdate = ds.readLine();
562
563             if (whichUpdate.equalsIgnoreCase("f")) {
564                 System.out.print("Enter directory to process:");
565
566                 String JavaDoc dirName = ds.readLine();
567                 ConfigManager.load(configDir);
568                 readTransFile(transFile);
569                 processDirs(dirName);
570             } else {
571                 System.out.print("Enter context/db name:");
572
573                 String JavaDoc db = ds.readLine();
574                 ConfigManager.load(configDir);
575                 readTransFile(transFile);
576                 processDB(configDir, db);
577             }
578
579             System.out.println("\nAll edits complete. Made " + totalEdits +
580                     " changes in total");
581         } catch (Exception JavaDoc e) {
582             e.printStackTrace();
583         }
584     } /* main(String[]) */
585
586 } /* ReNameUtil */
587
Popular Tags