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 DirFilter implements FileFilter
42 {
43 public static final DirFilter INSTANCE = new DirFilter();
44
45 @Override
46 public boolean accept(File path)
47 {
48 return path.isDirectory();
49 }
50 }
51
52 public static class RelativeRegexFilter implements FileFilter
53 {
54 private final File baseDir;
55 private final Pattern pattern;
56
57 public RelativeRegexFilter(File baseDir, Pattern pattern)
58 {
59 this.baseDir = baseDir;
60 this.pattern = pattern;
61 }
62
63 @Override
64 public boolean accept(File path)
65 {
66
67 String relativePath = FS.toRelativePath(baseDir,path);
68
69
70 return (pattern.matcher(relativePath).matches());
71 }
72 }
73
74 public static class FilenameRegexFilter implements FileFilter
75 {
76 private final Pattern pattern;
77
78 public FilenameRegexFilter(String regex)
79 {
80 pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
81 }
82
83 @Override
84 public boolean accept(File path)
85 {
86 return path.isFile() && pattern.matcher(path.getName()).matches();
87 }
88 }
89
90 public static class FileNamesFilter implements FileFilter
91 {
92 private final String filenames[];
93
94 public FileNamesFilter(String... names)
95 {
96 this.filenames = names;
97 }
98
99 @Override
100 public boolean accept(File path)
101 {
102 if (!path.isFile())
103 {
104 return false;
105 }
106 for (String name : filenames)
107 {
108 if (name.equalsIgnoreCase(path.getName()))
109 {
110 return true;
111 }
112 }
113 return false;
114 }
115 }
116
117 public static class IniFilter extends FilenameRegexFilter
118 {
119 public IniFilter()
120 {
121 super("^.*\\.ini$");
122 }
123 }
124
125 public static class XmlFilter extends FilenameRegexFilter
126 {
127 public XmlFilter()
128 {
129 super("^.*\\.xml$");
130 }
131 }
132
133 public static boolean canReadDirectory(File path)
134 {
135 return (path.exists() && path.isDirectory() && path.canRead());
136 }
137
138 public static boolean canReadFile(File path)
139 {
140 return (path.exists() && path.isFile() && path.canRead());
141 }
142
143 public static void close(Closeable c)
144 {
145 if (c == null)
146 {
147 return;
148 }
149
150 try
151 {
152 c.close();
153 }
154 catch (IOException ignore)
155 {
156
157 }
158 }
159
160 public static void ensureDirectoryExists(File dir) throws IOException
161 {
162 if (dir.exists())
163 {
164 return;
165 }
166 if (!dir.mkdirs())
167 {
168 throw new IOException("Unable to create directory: " + dir.getAbsolutePath());
169 }
170 }
171
172 public static void ensureDirectoryWritable(File dir) throws IOException
173 {
174 if (!dir.exists())
175 {
176 throw new IOException("Directory does not exist: " + dir.getAbsolutePath());
177 }
178 if (!dir.canWrite())
179 {
180 throw new IOException("Unable to write to directory: " + dir.getAbsolutePath());
181 }
182 }
183
184 public static boolean isFile(File file)
185 {
186 if (file == null)
187 {
188 return false;
189 }
190 return file.exists() && file.isFile();
191 }
192
193 public static boolean isXml(String filename)
194 {
195 return filename.toLowerCase(Locale.ENGLISH).endsWith(".xml");
196 }
197
198 public static String toRelativePath(File baseDir, File path)
199 {
200 return baseDir.toURI().relativize(path.toURI()).toASCIIString();
201 }
202
203 public static String separators(String path)
204 {
205 StringBuilder ret = new StringBuilder();
206 for (char c : path.toCharArray())
207 {
208 if ((c == '/') || (c == '\\'))
209 {
210 ret.append(File.separatorChar);
211 }
212 else
213 {
214 ret.append(c);
215 }
216 }
217 return ret.toString();
218 }
219 }