1
13
14 package gate.util;
15 import java.io.*;
16 import java.util.*;
17
18 import gnu.regexp.*;
19
20
40 public class Files {
41
42
43 private static final boolean DEBUG = false;
44
45
46 static long resourceIndex = 0;
47
48
49 protected static String resourcePath = "/gate/resources";
50
51
52 public static String getResourcePath(){
53 return resourcePath;
54 }
55
56
59 public static String getLastPathComponent(String path){
60 if(path == null || path.length() == 0) return "";
61 int index = path.lastIndexOf('/');
64 if(index == -1) index = path.lastIndexOf('\\');
65 if(index == -1) return path;
66 else return path.substring(index + 1);
67 }
69
70 public static String getString(String fileName) throws IOException {
71 return getString(new File(fileName));
72 }
74
75 public static String getString(File textFile) throws IOException {
76 FileInputStream fis = new FileInputStream(textFile);
77 int len = (int) textFile.length();
78 byte[] textBytes = new byte[len];
79 fis.read(textBytes, 0, len);
80 fis.close();
81 return new String(textBytes);
82 }
84
85 public static byte[] getByteArray(File binaryFile) throws IOException {
86 FileInputStream fis = new FileInputStream(binaryFile);
87 int len = (int) binaryFile.length();
88 byte[] bytes = new byte[len];
89 fis.read(bytes, 0, len);
90 fis.close();
91 return bytes;
92 }
94
96 public static String getResourceAsString(String resourceName)
97 throws IOException {
98 InputStream resourceStream = getResourceAsStream(resourceName);
99 BufferedReader resourceReader =
100 new BufferedReader(new InputStreamReader(resourceStream));
101 StringBuffer resourceBuffer = new StringBuffer();
102
103 int i;
104
105 int charsRead = 0;
106 final int size = 1024;
107 char[] charArray = new char[size];
108
109 while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
110 resourceBuffer.append (charArray,0,charsRead);
111
112 while( (i = resourceReader.read()) != -1 )
113 resourceBuffer.append((char) i);
114
115 resourceReader.close();
116 return resourceBuffer.toString();
117 }
119
125 public static String getGateResourceAsString(String resourceName)
126 throws IOException {
127
128 InputStream resourceStream = getGateResourceAsStream(resourceName);
129 BufferedReader resourceReader =
130 new BufferedReader(new InputStreamReader(resourceStream));
131 StringBuffer resourceBuffer = new StringBuffer();
132
133 int i;
134
135 int charsRead = 0;
136 final int size = 1024;
137 char[] charArray = new char[size];
138
139 while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
140 resourceBuffer.append (charArray,0,charsRead);
141
142 while( (i = resourceReader.read()) != -1 )
143 resourceBuffer.append((char) i);
144
145 resourceReader.close();
146 return resourceBuffer.toString();
147 }
149
154 public static File writeTempFile(InputStream contentStream)
155 throws IOException {
156
157 File resourceFile = null;
158 FileOutputStream resourceFileOutputStream = null;
159
160 resourceFile = File.createTempFile ("gateResource", ".tmp");
162 resourceFileOutputStream = new FileOutputStream(resourceFile);
163 resourceFile.deleteOnExit ();
164
165 if (contentStream == null)
166 return resourceFile;
167
168 int bytesRead = 0;
169 final int readSize = 1024;
170 byte[] bytes = new byte[readSize];
171 while( (bytesRead = contentStream.read(bytes,0,readSize) ) != -1 )
172 resourceFileOutputStream.write(bytes,0, bytesRead);
173
174 resourceFileOutputStream.close();
175 contentStream.close ();
176 return resourceFile;
177 }
179
190 public static File writeTempFile(String aString, String anEncoding) throws
191 UnsupportedEncodingException, IOException{
192 File resourceFile = null;
193 OutputStreamWriter writer = null;
194
195 resourceFile = File.createTempFile ("gateResource", ".tmp");
197 resourceFile.deleteOnExit ();
198
199 if (aString == null) return resourceFile;
200 if (anEncoding == null){
202 writer = new OutputStreamWriter(new FileOutputStream(resourceFile));
204
205 }else {
206 writer = new OutputStreamWriter(
208 new FileOutputStream(resourceFile),anEncoding);
209 }
211 writer.write(aString);
214 writer.flush();
215 writer.close();
216 return resourceFile;
217 }
219
228 public static File writeTempFile(String aString) throws IOException{
229 return writeTempFile(aString,null);
230 }
232
233
235 public static byte[] getResourceAsByteArray(String resourceName)
236 throws IOException, IndexOutOfBoundsException, ArrayStoreException {
237
238 InputStream resourceInputStream = getResourceAsStream(resourceName);
239 BufferedInputStream resourceStream =
240 new BufferedInputStream(resourceInputStream);
241 byte b;
242 final int bufSize = 1024;
243 byte[] buf = new byte[bufSize];
244 int i = 0;
245
246 while( (b = (byte) resourceStream.read()) != -1 ) {
248 if(i == buf.length) {
249 byte[] newBuf = new byte[buf.length * 2];
250 System.arraycopy (buf,0,newBuf,0,i);
251 buf = newBuf;
252 }
253 buf[i++] = b;
254 }
255
256 resourceStream.close();
258
259 byte[] bytes = new byte[i];
261 System.arraycopy (buf,0,bytes,0,i);
263 return bytes;
264 }
266
272 public static byte[] getGateResourceAsByteArray(String resourceName)
273 throws IOException, IndexOutOfBoundsException, ArrayStoreException {
274
275 InputStream resourceInputStream = getGateResourceAsStream(resourceName);
276 BufferedInputStream resourceStream =
277 new BufferedInputStream(resourceInputStream);
278 byte b;
279 final int bufSize = 1024;
280 byte[] buf = new byte[bufSize];
281 int i = 0;
282
283 while( (b = (byte) resourceStream.read()) != -1 ) {
285 if(i == buf.length) {
286 byte[] newBuf = new byte[buf.length * 2];
287 System.arraycopy (buf,0,newBuf,0,i);
288 buf = newBuf;
289 }
290 buf[i++] = b;
291 }
292
293 resourceStream.close();
295
296 byte[] bytes = new byte[i];
298
299 System.arraycopy (buf,0,bytes,0,i);
301 return bytes;
302 }
304
305
307 public static InputStream getResourceAsStream(String resourceName)
308 throws IOException {
309
310 return Files.class.getResourceAsStream(resourceName);
311 }
314
320 public static InputStream getGateResourceAsStream(String resourceName)
321 throws IOException {
322
323 if(resourceName.startsWith("/") || resourceName.startsWith("\\") )
324 return getResourceAsStream(resourcePath + resourceName);
325 else return getResourceAsStream(resourcePath + "/" + resourceName);
326 }
328
329
332 public static Set Find(String regex, String pathFile) {
333 Set regexfinal = new HashSet();
334 String[] tab;
335 File file = null;
336 PrintStream printstr = null;
337 Object obj = new Object();
338 try {
340 file = new File(pathFile);
341 } catch(NullPointerException npe) {
342 npe.printStackTrace(Err.getPrintWriter());
343 }
345 try {
347 RE regexp = new RE("^"+regex);
348 if (file.isDirectory()){
349 tab = file.list();
350 for (int i=0;i<=tab.length-1;i++){
351 String finalPath = pathFile+"/"+tab[i];
352 REMatch m1 = regexp.getMatch(finalPath);
353 if (regexp.getMatch(finalPath) != null){
354 regexfinal.add(finalPath);
355 }
356 }
357 }
358 else {
359 if (file.isFile()){
360 if (regexp.getMatch(pathFile) != null) {
361 regexfinal.add(file.getAbsolutePath());
362 }
363 }
364 }
365 } catch(REException ree) {
366 ree.printStackTrace(Err.getPrintWriter());
367 }
369 return regexfinal;
370 }
372
376 public static boolean rmdir(File dir) {
377 if(dir == null || ! dir.isDirectory()) return false;
379
380 String[] members = dir.list();
382
383 boolean succeeded = true;
385
386 for(int i = 0; i<members.length; i++) {
388 File member = new File(dir, members[i]);
389
390 if(member.isFile()) {
391 if(! member.delete())
392 succeeded = false;
393 } else {
394 if(! Files.rmdir(member))
395 succeeded = false;
396 }
397 }
398
399 dir.delete();
401
402 return succeeded;
404 }
406
416 public static String updateXmlElement(
417 BufferedReader xml, String elementName, Map newAttrs
418 ) throws IOException {
419 String line = null;
420 String nl = Strings.getNl();
421 StringBuffer newXml = new StringBuffer();
422
423 while( ( line = xml.readLine() ) != null ) {
425 newXml.append(line);
426 newXml.append(nl);
427 }
428
429 int start = newXml.toString().indexOf("<" + elementName);
431 if(start == -1) return newXml.toString();
432 int end = newXml.toString().indexOf(">", start);
433 if(end == -1) return newXml.toString();
434
435 boolean isEmpty = false;
437 if(newXml.toString().charAt(end - 1) == '/') isEmpty = true;
438
439 StringBuffer newElement = new StringBuffer();
441 newElement.append("<");
442 newElement.append(elementName);
443
444 Iterator iter = newAttrs.entrySet().iterator();
446 while(iter.hasNext()) {
447 Map.Entry entry = (Map.Entry) iter.next();
448 String key = (String) entry.getKey();
449 String value = (String) entry.getValue();
450
451 newElement.append(" ");newElement.append(key);
452 newElement.append("=\"");
453 newElement.append(value);
454 newElement.append("\"" + nl);
455 }
456
457 if(isEmpty) newElement.append("/");
459 newElement.append(">");
460
461 newXml.replace(start, end + 1, newElement.toString());
463
464 return newXml.toString();
465 }
467
478 public static String updateXmlElement(
479 File xmlFile, String elementName, Map newAttrs
480 ) throws IOException {
481 BufferedReader fileReader = new BufferedReader(new FileReader(xmlFile));
482 String newXml = updateXmlElement(fileReader, elementName, newAttrs);
483 fileReader.close();
484
485 FileWriter fileWriter = new FileWriter(xmlFile);
486 fileWriter.write(newXml);
487 fileWriter.close();
488
489 return newXml;
490 }
492 }