1 4 package com.zerog.ia.customcode.util.fileutils; 5 6 import java.io.*; 7 8 import com.zerog.ia.api.pub.*; 9 10 17 public class Rename extends CustomCodeAction 18 { 19 private static final String INSTALL_MESSAGE = "Renaming files"; 20 private static final String UNINSTALL_MESSAGE = ""; 21 private static final String ERR_MSG = 22 "Rename: no target or new name specified."; 23 private static final String TARGET_VAR_NAME = "$Rename_Target$"; 24 private static final String NEWNAME_VAR_NAME = "$Rename_NewName$"; 25 private static final String SUCCESS = "SUCCESS"; 26 private static final String ERROR = "ERROR"; 27 private boolean isLoaded = false; 28 41 public void install( InstallerProxy ip ) throws InstallException 42 { 43 51 ip.setVariable("RENAME_SUCCESS", Rename.SUCCESS); 52 53 54 System.out.println("Rename: RENAME_SUCCESS=" + Rename.SUCCESS); 55 56 58 String target = ip.substitute( TARGET_VAR_NAME ); 59 String newName = ip.substitute( NEWNAME_VAR_NAME ); 60 61 62 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 } 83 } 84 } 85 86 94 public void uninstall( UninstallerProxy up ) throws InstallException 95 { 96 } 97 98 104 public String getInstallStatusMessage() 105 { 106 return INSTALL_MESSAGE; 107 } 108 109 115 public String getUninstallStatusMessage() 116 { 117 return UNINSTALL_MESSAGE; 118 } 119 120 public static void rename( String target, String newName ) 121 throws IOException 122 { 123 rename( new File( target ), newName ); 124 } 125 126 130 public static void rename( File target, String 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 140 private void error( String target, String 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
|