View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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, final String stemRegex) {
75  
76          if (file == null) {
77              return new File[0];
78          }
79          if (!file.exists() || !file.isDirectory()) {
80              return new File[0];
81          }
82          return file.listFiles(new FilenameFilter() {
83              public boolean accept(File dir, String name) {
84                  return name.matches(stemRegex);
85              }
86          });
87      }
88  
89      static public int findHighestCounter(File[] matchingFileArray, final String stemRegex) {
90          int max = Integer.MIN_VALUE;
91          for (File aFile : matchingFileArray) {
92              int aCounter = FileFilterUtil.extractCounter(aFile, stemRegex);
93              if (max < aCounter)
94                  max = aCounter;
95          }
96          return max;
97      }
98  
99      static public int extractCounter(File file, final String stemRegex) {
100         Pattern p = Pattern.compile(stemRegex);
101         String lastFileName = file.getName();
102 
103         Matcher m = p.matcher(lastFileName);
104         if (!m.matches()) {
105             throw new IllegalStateException("The regex [" + stemRegex + "] should match [" + lastFileName + "]");
106         }
107         String counterAsStr = m.group(1);
108         return Integer.parseInt(counterAsStr);
109     }
110 
111     public static String slashify(String in) {
112         return in.replace('\\', '/');
113     }
114 
115     public static void removeEmptyParentDirectories(File file, int recursivityCount) {
116         // we should never go more than 3 levels higher
117         if (recursivityCount >= 3) {
118             return;
119         }
120         File parent = file.getParentFile();
121         if (parent.isDirectory() && FileFilterUtil.isEmptyDirectory(parent)) {
122             parent.delete();
123             removeEmptyParentDirectories(parent, recursivityCount + 1);
124         }
125     }
126 }