1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *      http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * © CrossWire Bible Society, 2005 - 2016
18   *
19   */
20  package org.crosswire.common.util;
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * .
28   * 
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author Joe Walker
31   */
32  public final class FileUtil {
33      /**
34       * Prevent instantiation
35       */
36      private FileUtil() {
37      }
38  
39      /**
40       * Deletes a file or a directory and all of its contents
41       * 
42       * @param file
43       *            or directory to delete
44       * @return the list of files that could not be deleted
45       */
46      public static List<File> delete(File file) {
47          List<File> failures = new ArrayList<File>();
48          if (file.isDirectory()) {
49              deleteContents(file, failures);
50          }
51          if (!file.delete()) {
52              failures.add(file);
53          }
54          return failures;
55      }
56  
57      /**
58       * Recursive delete files.
59       * 
60       * @param dirPath
61       *            directory of files to delete
62       * @param failures
63       *            the list of files that could not be deleted
64       */
65      private static void deleteContents(File dirPath, List<File> failures) {
66          String[] ls = dirPath.list();
67  
68          for (int idx = 0; idx < ls.length; idx++) {
69              File file = new File(dirPath, ls[idx]);
70              if (file.isDirectory()) {
71                  deleteContents(file, failures);
72              }
73              if (!file.delete()) {
74                  failures.add(file);
75              }
76          }
77      }
78  
79      /**
80       * Extension for java files
81       */
82      public static final String EXTENSION_JAVA = ".java";
83  
84      /**
85       * Extension for properties files
86       */
87      public static final String EXTENSION_PROPERTIES = ".properties";
88  
89      /**
90       * Extension for plug-in files
91       */
92      public static final String EXTENSION_PLUGIN = ".plugin";
93  
94      /**
95       * Extension for XSLT files
96       */
97      public static final String EXTENSION_XSLT = ".xsl";
98  
99      /**
100      * Extension for XML files
101      */
102     public static final String EXTENSION_XML = ".xml";
103 
104     /**
105      * Modes for opening random access files
106      */
107     public static final String MODE_READ = "r";
108 
109     /**
110      * Modes for opening random access files
111      */
112     public static final String MODE_WRITE = "rw";
113 }
114