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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: FileUtil.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * .
30   * 
31   * @see gnu.lgpl.License for license details.<br>
32   *      The copyright to this program is held by it's authors.
33   * @author Joe Walker [joe at eireneh dot com]
34   */
35  public final class FileUtil {
36      /**
37       * Prevent instantiation
38       */
39      private FileUtil() {
40      }
41  
42      /**
43       * Deletes a file or a directory and all of its contents
44       * 
45       * @param file
46       *            or directory to delete
47       * @return the list of files that could not be deleted
48       */
49      public static List<File> delete(File file) {
50          List<File> failures = new ArrayList<File>();
51          if (file.isDirectory()) {
52              deleteContents(file, failures);
53          }
54          if (!file.delete()) {
55              failures.add(file);
56          }
57          return failures;
58      }
59  
60      /**
61       * Recursive delete files.
62       * 
63       * @param dirPath
64       *            directory of files to delete
65       * @param failures
66       *            the list of files that could not be deleted
67       */
68      private static void deleteContents(File dirPath, List<File> failures) {
69          String[] ls = dirPath.list();
70  
71          for (int idx = 0; idx < ls.length; idx++) {
72              File file = new File(dirPath, ls[idx]);
73              if (file.isDirectory()) {
74                  deleteContents(file, failures);
75              }
76              if (!file.delete()) {
77                  failures.add(file);
78              }
79          }
80      }
81  
82      /**
83       * Extension for java files
84       */
85      public static final String EXTENSION_JAVA = ".java";
86  
87      /**
88       * Extension for properties files
89       */
90      public static final String EXTENSION_PROPERTIES = ".properties";
91  
92      /**
93       * Extension for plug-in files
94       */
95      public static final String EXTENSION_PLUGIN = ".plugin";
96  
97      /**
98       * Extension for XSLT files
99       */
100     public static final String EXTENSION_XSLT = ".xsl";
101 
102     /**
103      * Extension for XML files
104      */
105     public static final String EXTENSION_XML = ".xml";
106 
107     /**
108      * Modes for opening random access files
109      */
110     public static final String MODE_READ = "r";
111 
112     /**
113      * Modes for opening random access files
114      */
115     public static final String MODE_WRITE = "rw";
116 }
117