1
13
14 package gate.util;
15 import java.io.*;
16 import java.util.*;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.net.URL;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22
23
24
44 public class Files {
45
46
47 private static final boolean DEBUG = false;
48
49
50 static long resourceIndex = 0;
51
52
53 protected static String resourcePath = "/gate/resources";
54
55
56 public static String getResourcePath(){
57 return resourcePath;
58 }
59
60
63 public static String getLastPathComponent(String path){
64 if(path == null || path.length() == 0) return "";
65 int index = path.lastIndexOf('/');
68 if(index == -1) index = path.lastIndexOf('\\');
69 if(index == -1) return path;
70 else return path.substring(index + 1);
71 }
73
74 public static String getString(String fileName) throws IOException {
75 return getString(new File(fileName));
76 }
78
79 public static String getString(File textFile) throws IOException {
80 FileInputStream fis = new FileInputStream(textFile);
81 int len = (int) textFile.length();
82 byte[] textBytes = new byte[len];
83 fis.read(textBytes, 0, len);
84 fis.close();
85 return new String(textBytes);
86 }
88
89 public static byte[] getByteArray(File binaryFile) throws IOException {
90 FileInputStream fis = new FileInputStream(binaryFile);
91 int len = (int) binaryFile.length();
92 byte[] bytes = new byte[len];
93 fis.read(bytes, 0, len);
94 fis.close();
95 return bytes;
96 }
98
100 public static String getResourceAsString(String resourceName)
101 throws IOException {
102 InputStream resourceStream = getResourceAsStream(resourceName);
103 BufferedReader resourceReader =
104 new BufferedReader(new InputStreamReader(resourceStream));
105 StringBuffer resourceBuffer = new StringBuffer();
106
107 int i;
108
109 int charsRead = 0;
110 final int size = 1024;
111 char[] charArray = new char[size];
112
113 while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
114 resourceBuffer.append (charArray,0,charsRead);
115
116 while( (i = resourceReader.read()) != -1 )
117 resourceBuffer.append((char) i);
118
119 resourceReader.close();
120 return resourceBuffer.toString();
121 }
123
129 public static String getGateResourceAsString(String resourceName)
130 throws IOException {
131
132 InputStream resourceStream = getGateResourceAsStream(resourceName);
133 BufferedReader resourceReader =
134 new BufferedReader(new InputStreamReader(resourceStream));
135 StringBuffer resourceBuffer = new StringBuffer();
136
137 int i;
138
139 int charsRead = 0;
140 final int size = 1024;
141 char[] charArray = new char[size];
142
143 while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
144 resourceBuffer.append (charArray,0,charsRead);
145
146 while( (i = resourceReader.read()) != -1 )
147 resourceBuffer.append((char) i);
148
149 resourceReader.close();
150 return resourceBuffer.toString();
151 }
153
158 public static File writeTempFile(InputStream contentStream)
159 throws IOException {
160
161 File resourceFile = null;
162 FileOutputStream resourceFileOutputStream = null;
163
164 resourceFile = File.createTempFile ("gateResource", ".tmp");
166 resourceFileOutputStream = new FileOutputStream(resourceFile);
167 resourceFile.deleteOnExit ();
168
169 if (contentStream == null)
170 return resourceFile;
171
172 int bytesRead = 0;
173 final int readSize = 1024;
174 byte[] bytes = new byte[readSize];
175 while( (bytesRead = contentStream.read(bytes,0,readSize) ) != -1 )
176 resourceFileOutputStream.write(bytes,0, bytesRead);
177
178 resourceFileOutputStream.close();
179 contentStream.close ();
180 return resourceFile;
181 }
183
194 public static File writeTempFile(String aString, String anEncoding) throws
195 UnsupportedEncodingException, IOException{
196 File resourceFile = null;
197 OutputStreamWriter writer = null;
198
199 resourceFile = File.createTempFile ("gateResource", ".tmp");
201 resourceFile.deleteOnExit ();
202
203 if (aString == null) return resourceFile;
204 if (anEncoding == null){
206 writer = new OutputStreamWriter(new FileOutputStream(resourceFile));
208
209 }else {
210 writer = new OutputStreamWriter(
212 new FileOutputStream(resourceFile),anEncoding);
213 }
215 writer.write(aString);
218 writer.flush();
219 writer.close();
220 return resourceFile;
221 }
223
232 public static File writeTempFile(String aString) throws IOException{
233 return writeTempFile(aString,null);
234 }
236
237
239 public static byte[] getResourceAsByteArray(String resourceName)
240 throws IOException, IndexOutOfBoundsException, ArrayStoreException {
241
242 InputStream resourceInputStream = getResourceAsStream(resourceName);
243 BufferedInputStream resourceStream =
244 new BufferedInputStream(resourceInputStream);
245 byte b;
246 final int bufSize = 1024;
247 byte[] buf = new byte[bufSize];
248 int i = 0;
249
250 while( (b = (byte) resourceStream.read()) != -1 ) {
252 if(i == buf.length) {
253 byte[] newBuf = new byte[buf.length * 2];
254 System.arraycopy (buf,0,newBuf,0,i);
255 buf = newBuf;
256 }
257 buf[i++] = b;
258 }
259
260 resourceStream.close();
262
263 byte[] bytes = new byte[i];
265 System.arraycopy (buf,0,bytes,0,i);
267 return bytes;
268 }
270
276 public static byte[] getGateResourceAsByteArray(String resourceName)
277 throws IOException, IndexOutOfBoundsException, ArrayStoreException {
278
279 InputStream resourceInputStream = getGateResourceAsStream(resourceName);
280 BufferedInputStream resourceStream =
281 new BufferedInputStream(resourceInputStream);
282 byte b;
283 final int bufSize = 1024;
284 byte[] buf = new byte[bufSize];
285 int i = 0;
286
287 while( (b = (byte) resourceStream.read()) != -1 ) {
289 if(i == buf.length) {
290 byte[] newBuf = new byte[buf.length * 2];
291 System.arraycopy (buf,0,newBuf,0,i);
292 buf = newBuf;
293 }
294 buf[i++] = b;
295 }
296
297 resourceStream.close();
299
300 byte[] bytes = new byte[i];
302
303 System.arraycopy (buf,0,bytes,0,i);
305 return bytes;
306 }
308
309
311 public static InputStream getResourceAsStream(String resourceName)
312 throws IOException {
313
314 return Files.class.getResourceAsStream(resourceName);
315 }
318
324 public static InputStream getGateResourceAsStream(String resourceName)
325 throws IOException {
326
327 if(resourceName.startsWith("/") || resourceName.startsWith("\\") )
328 return getResourceAsStream(resourcePath + resourceName);
329 else return getResourceAsStream(resourcePath + "/" + resourceName);
330 }
332
333
336 public static Set Find(String regex, String pathFile) {
337 Set regexfinal = new HashSet();
338 String[] tab;
339 File file = null;
340 PrintStream printstr = null;
341 Object obj = new Object();
342 try {
344 file = new File(pathFile);
345 } catch(NullPointerException npe) {
346 npe.printStackTrace(Err.getPrintWriter());
347 }
348
349 Pattern pattern = Pattern.compile("^"+regex);
350
351 if (file.isDirectory()){
352 tab = file.list();
353 for (int i=0;i<=tab.length-1;i++){
354 String finalPath = pathFile+"/"+tab[i];
355 Matcher matcher = pattern.matcher(finalPath);
356 if (matcher.matches()){
357 regexfinal.add(finalPath);
358 }
359 }
360 }
361 else {
362 if (file.isFile()){
363 Matcher matcher = pattern.matcher(pathFile);
364 if (matcher.matches()){
365 regexfinal.add(pathFile);
366 }
367 }
368 }
369
370 return regexfinal;
371 }
373
377 public static boolean rmdir(File dir) {
378 if(dir == null || ! dir.isDirectory()) return false;
380
381 String[] members = dir.list();
383
384 boolean succeeded = true;
386
387 for(int i = 0; i<members.length; i++) {
389 File member = new File(dir, members[i]);
390
391 if(member.isFile()) {
392 if(! member.delete())
393 succeeded = false;
394 } else {
395 if(! Files.rmdir(member))
396 succeeded = false;
397 }
398 }
399
400 dir.delete();
402
403 return succeeded;
405 }
407
417 public static String updateXmlElement(
418 BufferedReader xml, String elementName, Map newAttrs
419 ) throws IOException {
420 String line = null;
421 String nl = Strings.getNl();
422 StringBuffer newXml = new StringBuffer();
423
424 while( ( line = xml.readLine() ) != null ) {
426 newXml.append(line);
427 newXml.append(nl);
428 }
429
430 int start = newXml.toString().indexOf("<" + elementName);
432 if(start == -1) return newXml.toString();
433 int end = newXml.toString().indexOf(">", start);
434 if(end == -1) return newXml.toString();
435
436 boolean isEmpty = false;
438 if(newXml.toString().charAt(end - 1) == '/') isEmpty = true;
439
440 StringBuffer newElement = new StringBuffer();
442 newElement.append("<");
443 newElement.append(elementName);
444
445 Iterator iter = newAttrs.entrySet().iterator();
447 while(iter.hasNext()) {
448 Map.Entry entry = (Map.Entry) iter.next();
449 String key = (String) entry.getKey();
450 String value = (String) entry.getValue();
451
452 newElement.append(" ");newElement.append(key);
453 newElement.append("=\"");
454 newElement.append(value);
455 newElement.append("\"" + nl);
456 }
457
458 if(isEmpty) newElement.append("/");
460 newElement.append(">");
461
462 newXml.replace(start, end + 1, newElement.toString());
464
465 return newXml.toString();
466 }
468
479 public static String updateXmlElement(
480 File xmlFile, String elementName, Map newAttrs
481 ) throws IOException {
482 BufferedReader fileReader = new BufferedReader(new FileReader(xmlFile));
483 String newXml = updateXmlElement(fileReader, elementName, newAttrs);
484 fileReader.close();
485
486 FileWriter fileWriter = new FileWriter(xmlFile);
487 fileWriter.write(newXml);
488 fileWriter.close();
489
490 return newXml;
491 }
493
494
504 public static File fileFromURL(URL theURL) throws IllegalArgumentException {
505 try {
506 URI uri = new URI(theURL.toExternalForm());
507 return new File(uri);
508 }
509 catch(URISyntaxException use) {
510 try {
511 URI uri = new URI(theURL.getProtocol(), null, theURL.getPath(), null, null);
512 return new File(uri);
513 }
514 catch(URISyntaxException use2) {
515 throw new IllegalArgumentException("Cannot convert " + theURL + " to a file path");
516 }
517 }
518 }
519
520 }