View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.io.File;
17  import java.io.FilenameFilter;
18  import java.util.Arrays;
19  import java.util.Comparator;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  public class FileFilterUtil {
24  
25    public static void sortFileArrayByName(File[] fileArray) {
26      Arrays.sort(fileArray, new Comparator<File>() {
27        public int compare(File o1, File o2) {
28          String o1Name = o1.getName();
29          String o2Name = o2.getName();
30          return (o1Name.compareTo(o2Name));
31        }
32      });
33    }
34  
35    public static void reverseSortFileArrayByName(File[] fileArray) {
36      Arrays.sort(fileArray, new Comparator<File>() {
37        public int compare(File o1, File o2) {
38          String o1Name = o1.getName();
39          String o2Name = o2.getName();
40          return (o2Name.compareTo(o1Name));
41        }
42      });
43    }
44  
45    public static String afterLastSlash(String sregex) {
46      int i = sregex.lastIndexOf('/');
47      if (i == -1) {
48        return sregex;
49      } else {
50        return sregex.substring(i + 1);
51      }
52    }
53  
54    static public boolean isEmptyDirectory(File dir) {
55      if (!dir.isDirectory()) {
56        throw new IllegalArgumentException("[" + dir + "] must be a directory");
57      }
58      String[] filesInDir = dir.list();
59      if (filesInDir == null || filesInDir.length == 0) {
60        return true;
61      } else {
62        return false;
63      }
64    }
65  
66    /**
67     * Return the set of files matching the stemRegex as found in 'directory'. A
68     * stemRegex does not contain any slash characters or any folder separators.
69     * 
70     * @param file
71     * @param stemRegex
72     * @return
73     */
74    public static File[] filesInFolderMatchingStemRegex(File file,
75        final String stemRegex) {
76  
77      if (file == null) {
78        return new File[0];
79      }
80      if (!file.exists() || !file.isDirectory()) {
81        return new File[0];
82      }
83      return file.listFiles(new FilenameFilter() {
84        public boolean accept(File dir, String name) {
85          return name.matches(stemRegex);
86        }
87      });
88    }
89  
90    static public int findHighestCounter(File[] matchingFileArray, final String stemRegex) {
91      int max = Integer.MIN_VALUE;
92      for (File aFile : matchingFileArray) {
93        int aCounter = FileFilterUtil.extractCounter(aFile, stemRegex);
94        if (max < aCounter)
95          max = aCounter;
96      }
97      return max;
98    }
99  
100   static public int extractCounter(File file, final String stemRegex) {
101     Pattern p = Pattern.compile(stemRegex);
102     String lastFileName = file.getName();
103 
104     Matcher m = p.matcher(lastFileName);
105     if (!m.matches()) {
106       throw new IllegalStateException("The regex [" + stemRegex
107           + "] should match [" + lastFileName + "]");
108     }
109     String counterAsStr = m.group(1);
110     return new Integer(counterAsStr).intValue();
111   }
112 
113   public static String slashify(String in) {
114     return in.replace('\\', '/');
115   }
116 
117   public static void removeEmptyParentDirectories(File file,
118       int recursivityCount) {
119     // we should never go more than 3 levels higher
120     if (recursivityCount >= 3) {
121       return;
122     }
123     File parent = file.getParentFile();
124     if (parent.isDirectory() && FileFilterUtil.isEmptyDirectory(parent)) {
125       parent.delete();
126       removeEmptyParentDirectories(parent, recursivityCount + 1);
127     }
128   }
129 }