1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.Closeable;
22 import java.io.File;
23 import java.io.FileFilter;
24 import java.io.IOException;
25 import java.util.Locale;
26 import java.util.regex.Pattern;
27
28 public class FS
29 {
30 public static class AllFilter implements FileFilter
31 {
32 public static final AllFilter INSTANCE = new AllFilter();
33
34 @Override
35 public boolean accept(File pathname)
36 {
37 return true;
38 }
39 }
40
41 public static class FilenameRegexFilter implements FileFilter
42 {
43 private final Pattern pattern;
44
45 public FilenameRegexFilter(String regex)
46 {
47 pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
48 }
49
50 @Override
51 public boolean accept(File path)
52 {
53 return path.isFile() && pattern.matcher(path.getName()).matches();
54 }
55 }
56
57 public static class FileNamesFilter implements FileFilter
58 {
59 private final String filenames[];
60
61 public FileNamesFilter(String... names)
62 {
63 this.filenames = names;
64 }
65
66 @Override
67 public boolean accept(File path)
68 {
69 if (!path.isFile())
70 {
71 return false;
72 }
73 for (String name : filenames)
74 {
75 if (name.equalsIgnoreCase(path.getName()))
76 {
77 return true;
78 }
79 }
80 return false;
81 }
82 }
83
84 public static class IniFilter extends FilenameRegexFilter
85 {
86 public IniFilter()
87 {
88 super("^.*\\.ini$");
89 }
90 }
91
92 public static class XmlFilter extends FilenameRegexFilter
93 {
94 public XmlFilter()
95 {
96 super("^.*\\.xml$");
97 }
98 }
99
100 public static boolean canReadDirectory(File path)
101 {
102 return (path.exists() && path.isDirectory() && path.canRead());
103 }
104
105 public static boolean canReadFile(File path)
106 {
107 return (path.exists() && path.isFile() && path.canRead());
108 }
109
110 public static void close(Closeable c)
111 {
112 if (c == null)
113 {
114 return;
115 }
116
117 try
118 {
119 c.close();
120 }
121 catch (IOException ignore)
122 {
123
124 }
125 }
126
127 public static void ensureDirectoryExists(File dir) throws IOException
128 {
129 if (dir.exists())
130 {
131 return;
132 }
133 if (!dir.mkdirs())
134 {
135 throw new IOException("Unable to create directory: " + dir.getAbsolutePath());
136 }
137 }
138
139 public static boolean isFile(File file)
140 {
141 if (file == null)
142 {
143 return false;
144 }
145 return file.exists() && file.isFile();
146 }
147
148 public static boolean isXml(String filename)
149 {
150 return filename.toLowerCase(Locale.ENGLISH).endsWith(".xml");
151 }
152
153 public static String separators(String path)
154 {
155 StringBuilder ret = new StringBuilder();
156 for (char c : path.toCharArray())
157 {
158 if ((c == '/') || (c == '\\'))
159 {
160 ret.append(File.separatorChar);
161 }
162 else
163 {
164 ret.append(c);
165 }
166 }
167 return ret.toString();
168 }
169 }