KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > zerog > ia > customcode > util > fileutils > Rename


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.zerog.ia.customcode.util.fileutils;
5
6 import java.io.*;
7
8 import com.zerog.ia.api.pub.*;
9
10 /**
11  * Rename renames a file or directory.
12  *
13  * @see com.acme.dialogs.CustomCodeAction
14  *
15  * @version 3.0.0
16  */

17 public class Rename extends CustomCodeAction
18 {
19     private static final String JavaDoc INSTALL_MESSAGE = "Renaming files";
20     private static final String JavaDoc UNINSTALL_MESSAGE = "";
21     private static final String JavaDoc ERR_MSG =
22         "Rename: no target or new name specified.";
23     private static final String JavaDoc TARGET_VAR_NAME = "$Rename_Target$";
24     private static final String JavaDoc NEWNAME_VAR_NAME = "$Rename_NewName$";
25     private static final String JavaDoc SUCCESS = "SUCCESS";
26     private static final String JavaDoc ERROR = "ERROR";
27     private boolean isLoaded = false;
28     /**
29      * This is the method that is called at install-time. The InstallerProxy
30      * instance provides methods to access information in the installer,
31      * set status, and control flow.<p>
32      *
33      * For the purposes of the this action (Rename), this method
34      * <ol>
35      * <li>gets its parameters from InstallAnywhere Variables,</li>
36      * <li>checks the parameters' validity,</li>
37      * <li>and renames the file.</li></ol>
38      *
39      * @see com.zerog.ia.api.pub.CustomCodeAction#install
40      */

41     public void install( InstallerProxy ip ) throws InstallException
42     {
43         /**
44          * InstallAnywhere variable:RENAME_SUCCESS
45          *
46          * possible return values are:
47      * SUCCESS
48          * ERROR
49          *
50          */

51         ip.setVariable("RENAME_SUCCESS", Rename.SUCCESS);
52     
53     
54         System.out.println("Rename: RENAME_SUCCESS=" + Rename.SUCCESS);
55
56         /* Get input from InstallAnywhere Variables. The literal contents of
57             the Variables are retieved into the Strings. */

58         String JavaDoc target = ip.substitute( TARGET_VAR_NAME );
59         String JavaDoc newName = ip.substitute( NEWNAME_VAR_NAME );
60         
61         
62         /* substitute() will return an empty string for any InstallAnywhere
63             Variable that hasn't been assigned yet.
64         
65         /* If there is both a source and a destination, copy the files. */

66         
67         if ( target.equals("")
68             || newName.equals("") )
69         {
70             error( target, newName );
71         }
72         else
73         {
74             System.out.println("target = " + target + ", newName = " + newName);
75             try {
76                 rename( target, newName );
77             } catch ( IOException ioe ) {
78                 System.out.println("Rename: Exception = "+ ioe.getMessage());
79                 ip.setVariable("RENAME_SUCCESS", Rename.ERROR);
80                 System.out.println("Rename: RENAME_SUCCESS=" + Rename.ERROR);
81                 //throw new NonfatalInstallException( ioe.getMessage() );
82
}
83         }
84     }
85     
86     /**
87      * This is the method that is called at uninstall-time. For
88      * an example of how to effect the uninstallation of something
89      * like this, please see com.acme.fileutils.CopyFile.
90      *
91      * @see com.acme.fileutils.CopyFile
92      * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
93      */

94     public void uninstall( UninstallerProxy up ) throws InstallException
95     {
96     }
97     
98     /**
99      * This method will be called to display a status message during the
100      * installation.
101      *
102      * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
103      */

104     public String JavaDoc getInstallStatusMessage()
105     {
106         return INSTALL_MESSAGE;
107     }
108     
109     /**
110      * This method will be called to display a status message during the
111      * uninstall.
112      *
113      * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
114      */

115     public String JavaDoc getUninstallStatusMessage()
116     {
117         return UNINSTALL_MESSAGE;
118     }
119     
120     public static void rename( String JavaDoc target, String JavaDoc newName )
121         throws IOException
122     {
123         rename( new File( target ), newName );
124     }
125     
126     /**
127      * Rename the file represented by source to the file
128      * represented by destination.
129      */

130     public static void rename( File target, String JavaDoc newName )
131         throws IOException
132     {
133         if ( ! target.renameTo( new File( target.getParent(), newName ) ) )
134             throw new IOException( "Couldn't rename file." );
135     }
136     
137     /**
138      * Print something to indicate that the parameters were not acceptable.
139      */

140     private void error( String JavaDoc target, String JavaDoc newName )
141     {
142         System.err.println( ERR_MSG );
143         System.err.println( "Target: " + target );
144         System.err.println( "New Name: " + newName );
145     }
146 }
147
Popular Tags