KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CopyDirectory takes a specified file and copies it
12  * to a new location.
13  *
14  * @see com.acme.dialogs.CustomCodeAction
15  *
16  * @version 3.0.0
17  */

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

38     public void install( InstallerProxy ip ) throws InstallException
39     {
40
41         if(isLoaded == true)
42             return;
43         isLoaded = true;
44
45         /* Get input from InstallAnywhere Variables. The literal contents of
46             the Variables are retieved into the Strings. */

47         String JavaDoc fileToDelete = ip.substitute( FILE_VAR_NAME );
48
49         /* substitute() will return an empty string for any InstallAnywhere
50             Variable that hasn't been assigned yet.
51         
52         /* If there is both a source and a destination, copy the files. */

53         if ( fileToDelete.equals("") )
54         {
55             error( fileToDelete );
56         }
57         else
58         {
59             try {
60                 deleteFile( fileToDelete );
61             } catch ( IOException ioe ) {
62                 throw new NonfatalInstallException( ioe.getMessage() );
63             }
64         }
65     }
66     
67     /**
68      * This is the method that is called at uninstall-time. For
69      * an example of how to effect the uninstallation of something
70      * like this, please see com.acme.fileutils.CopyFile.
71      *
72      * @see com.acme.fileutils.CopyFile
73      * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
74      */

75     public void uninstall( UninstallerProxy up ) throws InstallException
76     {
77     }
78     
79     /**
80      * This method will be called to display a status message during the
81      * installation.
82      *
83      * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
84      */

85     public String JavaDoc getInstallStatusMessage()
86     {
87         return INSTALL_MESSAGE;
88     }
89     
90     /**
91      * This method will be called to display a status message during the
92      * uninstall.
93      *
94      * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
95      */

96     public String JavaDoc getUninstallStatusMessage()
97     {
98         return UNINSTALL_MESSAGE;
99     }
100     
101     /**
102      * Delete the file represented by fileToDelete.
103      */

104     public static void deleteFile( String JavaDoc fileToDelete ) throws IOException
105     {
106         deleteFile( new File( fileToDelete ) );
107     }
108     
109     /**
110      * Delete the file represented by fileToDelete.
111      */

112     public static void deleteFile( File fileToDelete ) throws IOException
113     {
114         if ( fileToDelete.isFile() )
115         {
116             if ( ! fileToDelete.delete() )
117             {
118                 throw new IOException( "Couldn't delete file: "
119                     + fileToDelete.getAbsolutePath() );
120             }
121         }
122         else
123         {
124             throw new IOException(
125                 "Couldn't delete file because file is a directory: " +
126                     fileToDelete.getAbsolutePath() );
127         }
128     }
129     
130     /**
131      * Print something to indicate that the parameters were not acceptable.
132      */

133     private void error( String JavaDoc path )
134     {
135         System.err.println( ERR_MSG );
136         System.err.println( "Path: " + path );
137     }
138 }
139
Popular Tags