View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.xmlpage.util;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  
38  import java.io.File;
39  import java.io.FileInputStream;
40  import java.io.FileOutputStream;
41  import java.io.IOException;
42  
43  import java.nio.channels.FileChannel;
44  
45  
46  /**
47   * File Utilities for XmlPage Plugin
48   */
49  public class XmlPageFileUtils
50  {
51      /**
52       * Default constructor
53       */
54      protected XmlPageFileUtils(  )
55      {
56          // nothing to do
57      }
58  
59      /**
60      * Make a directory using the path given in parameter
61      * @param strDirectoryPath the path of new directory
62      * @return true if and only if the directory was created, along with all necessary parent directories; false otherwise
63      */
64      public static boolean makeDirectory( String strDirectoryPath )
65      {
66          File fDirectory = new File( strDirectoryPath );
67          AppLogService.debug( "Creating directory : " + strDirectoryPath );
68  
69          return fDirectory.mkdirs(  );
70      }
71  
72      /**
73       * Create a file using the path given in parameter
74       * @param strFilePath file path to create
75       * @return true if the named file does not exist and was successfully created; false if the named file already exists
76       */
77      public static boolean createFile( String strFilePath )
78      {
79          AppLogService.debug( "Creating file : " + strFilePath );
80  
81          File fFile = new File( strFilePath );
82          boolean bReturn = false;
83  
84          try
85          {
86              bReturn = fFile.createNewFile(  );
87          }
88          catch ( IOException ioe )
89          {
90              AppLogService.error( ioe.getMessage(  ), ioe );
91          }
92  
93          return bReturn;
94      }
95  
96      /**
97       * Copy file from directory strDirectory to directory strNewDirectory
98       * @param strDirectory original directory contening the file
99       * @param strFileName file name to copy
100      * @param strNewDirectory destination directory for the copied file
101      */
102     public static void copyFile( String strDirectory, String strFileName, String strNewDirectory )
103     {
104         try
105         {
106             AppLogService.debug( "Copying file \"" + strFileName + "\" from directory \"" + strDirectory +
107                 "\" to directory \"" + strNewDirectory + "\"" );
108 
109             FileInputStream fInputStream = new FileInputStream( strDirectory.concat( "/" ).concat( strFileName ) );
110             FileOutputStream fOutputStream = new FileOutputStream( strNewDirectory.concat( "/" ).concat( strFileName ) );
111             FileChannel fInputChannel = fInputStream.getChannel(  );
112             FileChannel fOutputChannel = fOutputStream.getChannel(  );
113             fInputChannel.transferTo( 0, fInputChannel.size(  ), fOutputChannel );
114 
115             fInputChannel.close(  );
116             fOutputChannel.close(  );
117 
118             fInputStream.close(  );
119             fOutputStream.close(  );
120             AppLogService.debug( "Copying file done" );
121         }
122         catch ( IOException ioe )
123         {
124             AppLogService.error( ioe.getMessage(  ), ioe );
125         }
126     }
127 
128     /**
129      * Rename a file or directory with another name
130      * @param strOriginalDirectory original directory path
131      * @param strNewDirectory new directory path
132      * @return true if and only if the renaming succeeded; false otherwise
133      */
134     public static boolean renameDirectory( String strOriginalDirectory, String strNewDirectory )
135     {
136         AppLogService.debug( "Renaming directory from \"" + strOriginalDirectory + "\" to \"" + strNewDirectory + "\"" );
137 
138         File fOriginal = new File( strOriginalDirectory );
139         File fNew = new File( strNewDirectory );
140 
141         return fOriginal.renameTo( fNew );
142     }
143 
144     /**
145      * Delete a file
146      * @param strFilePath path of deleted file
147      * @return true if and only if the file is successfully deleted; false otherwise
148      */
149     public static boolean removeFile( String strFilePath )
150     {
151         AppLogService.debug( "Removing file \"" + strFilePath + "\"" );
152 
153         return removeDirectory( new File( strFilePath ) );
154     }
155 
156     /**
157      * Delete a directory and everything in it
158      * @param strDirectoryPath path of deleted directory
159      * @return true if and only if the directory is successfully deleted; false otherwise
160      */
161     public static boolean removeDirectory( String strDirectoryPath )
162     {
163         AppLogService.debug( "Removing directory \"" + strDirectoryPath + "\"" );
164 
165         return removeDirectory( new File( strDirectoryPath ) );
166     }
167 
168     /**
169      * Delete a directory and everything in it
170      * @param fDirectoryPath File of directory
171      * @return true if and only if the file or directory is successfully deleted; false otherwise
172      */
173     public static boolean removeDirectory( File fDirectoryPath )
174     {
175         if ( fDirectoryPath.isDirectory(  ) )
176         {
177             File[] childs = fDirectoryPath.listFiles(  );
178 
179             for ( int i = 0; i < childs.length; i++ )
180             {
181                 if ( !removeDirectory( childs[i] ) )
182                 {
183                     return false;
184                 }
185             }
186         }
187 
188         return fDirectoryPath.delete(  );
189     }
190 }