001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.util;
034:
035: import fr.loria.ecoo.so6.xml.xydiff.XyDiff;
036:
037: import jlibdiff.Diff;
038:
039: import org.libresource.so6.core.Workspace;
040:
041: import java.io.File;
042: import java.io.FileInputStream;
043: import java.io.FileOutputStream;
044: import java.io.FileReader;
045: import java.io.FileWriter;
046: import java.io.FilenameFilter;
047: import java.io.IOException;
048: import java.io.LineNumberReader;
049: import java.io.OutputStreamWriter;
050: import java.io.PrintStream;
051:
052: import java.util.Arrays;
053: import java.util.Collection;
054: import java.util.Iterator;
055: import java.util.Vector;
056: import java.util.zip.ZipOutputStream;
057:
058: public class FileUtils {
059: /** Base tmp file directory */
060: public final static String BASE_TMP_DIR = "so6.tmp";
061: private static int number = 1;
062:
063: /**
064: * Use for test
065: */
066: public static void createBinFile(String path, String name,
067: String msg) throws Exception {
068: editBinFile(path + File.separator + name, msg);
069: }
070:
071: /**
072: * Use for test
073: */
074: public static void editBinFile(String path, String msg)
075: throws Exception {
076: if (FileUtils.fileExists(path)) {
077: FileUtils.remove(path);
078: }
079:
080: FileOutputStream ostream = new FileOutputStream(path);
081:
082: for (int i = 127; i < 255; i++)
083: ostream.write(i);
084:
085: ostream.write(msg.getBytes());
086: ostream.flush();
087: ostream.close();
088: }
089:
090: /**
091: * Use for test
092: */
093: public static void createTxtFile(String path, String name,
094: String msg) throws Exception {
095: editTxtFile(path + File.separator + name, msg);
096: }
097:
098: public static boolean isLocked(File f) {
099: return !f.renameTo(f);
100: }
101:
102: public static boolean isLocked(String f) {
103: return isLocked(new File(f));
104: }
105:
106: /**
107: * Use for test
108: */
109: public static void createXmlFile(String root, String path)
110: throws Exception {
111: editTxtFile(path, "<?xml version='1.0'?>\n<" + root + "/>");
112: }
113:
114: /**
115: * Use for test
116: */
117: public static void createXmlFile(String root, String path,
118: String encoding) throws Exception {
119: String xml = "<?xml version='1.0' encoding='" + encoding
120: + "'?>\n<" + root + "/>";
121:
122: if (FileUtils.fileExists(path)) {
123: FileUtils.remove(path);
124: }
125:
126: FileOutputStream fos = new FileOutputStream(path);
127: OutputStreamWriter ow = new OutputStreamWriter(fos, encoding);
128: ow.write(xml);
129: ow.close();
130: fos.close();
131: }
132:
133: /**
134: * Use for test
135: */
136: public static void editTxtFile(String path, String msg)
137: throws Exception {
138: if (FileUtils.fileExists(path)) {
139: FileUtils.remove(path);
140: }
141:
142: FileOutputStream ostream = new FileOutputStream(path);
143: PrintStream p = new PrintStream(ostream);
144: p.print(msg);
145: p.flush();
146: p.close();
147: }
148:
149: public static void editTxtFile3(String path, String msg)
150: throws Exception {
151: if (FileUtils.fileExists(path)) {
152: FileUtils.remove(path);
153: }
154:
155: FileOutputStream ostream = new FileOutputStream(path);
156: PrintStream p = new PrintStream(ostream);
157: p.print(msg.getBytes("ISO8859_1"));
158: p.flush();
159: p.close();
160: }
161:
162: public static void editTxtFile2(String path, String msg)
163: throws Exception {
164: FileWriter ostream = new FileWriter(path, true);
165: ostream.write(msg);
166: ostream.flush();
167: ostream.close();
168: }
169:
170: /**
171: * Use create a directory and throw an exception if the creation failed or
172: * if the directory already exist
173: */
174: public static void createDir(String path) throws IOException {
175: File d = new File(path);
176:
177: if (!(d.mkdirs())) {
178: throw new IOException("cannot create dir:" + path);
179: }
180: }
181:
182: /**
183: * Create a directory if needed and throw an exception if the creation
184: * failed
185: */
186: public static void createDirIfNotExist(String path)
187: throws IOException {
188: File f = new File(path);
189:
190: if (f.exists() && (f.isDirectory())) {
191: return;
192: }
193:
194: if (f.exists() && (!f.isDirectory())) {
195: throw new RuntimeException("Cannot create :" + path
196: + " already exists as a file");
197: }
198:
199: createDir(path);
200: }
201:
202: /**
203: * Create a tmp directory in the BASE_TMP_DIR
204: */
205: public static File createTmpDir() throws Exception {
206: File f = new File(getBaseTmpPath() + File.separator + "SO6_"
207: + System.currentTimeMillis());
208:
209: while (f.exists()) {
210: f = new File(f.getPath() + "_" + number++);
211: }
212:
213: if (!f.mkdirs()) {
214: throw new Exception("cannot create tmpdir:" + f.getPath());
215: }
216:
217: return f;
218: }
219:
220: /**
221: * Remove a path recursivley
222: *
223: * @param path
224: * @throws Exception
225: */
226: public static void remove(String path, FilenameFilter ff,
227: boolean deleteCurrentDir) throws Exception {
228: File f = new File(path);
229:
230: if (f.exists()) {
231: delrec(f, ff, deleteCurrentDir);
232: }
233: }
234:
235: public static void remove(String path) throws Exception {
236: File f = new File(path);
237:
238: if (f.exists()) {
239: delrec(f, null, true);
240: }
241: }
242:
243: private static void delrec(File f, FilenameFilter ff,
244: boolean deleteCurrentDir) throws java.io.IOException {
245: if (f.isDirectory()) {
246: File[] fs = (ff == null) ? f.listFiles() : f.listFiles(ff);
247:
248: for (int i = 0; i < fs.length; i++) {
249: delrec(fs[i], ff, true);
250: }
251: }
252:
253: if (deleteCurrentDir) {
254: if (!(f.delete())) {
255: throw new java.io.IOException("cannot delete "
256: + f.getPath());
257: }
258: }
259: }
260:
261: /**
262: * Compare texte files
263: *
264: * @param file1
265: * @param file2
266: * @return @throws
267: * Exception
268: */
269: public static boolean compareTxtFile(String file1, String file2)
270: throws Exception {
271: Diff d = new Diff();
272: d.diffFile(file1, file2);
273:
274: //return d.areEquals();
275: return (d.getHunkCount() == 0);
276: }
277:
278: /**
279: * Compare binary files
280: *
281: * @param file1
282: * @param file2
283: * @return
284: */
285: public static boolean compareBinFile(String file1, String file2) {
286: return FileUtils.compareBinFile(new File(file1),
287: new File(file2));
288: }
289:
290: /**
291: * Compare binary files
292: *
293: * @param file1
294: * @param file2
295: * @return
296: */
297: public static boolean compareBinFile(File file1, File file2) {
298: if (file1.length() != file2.length()) {
299: return false;
300: }
301:
302: FileInputStream f1 = null;
303: FileInputStream f2 = null;
304: byte[] buffer1 = new byte[102400];
305: byte[] buffer2 = new byte[102400];
306:
307: for (int i = 0; i < buffer1.length; i++) {
308: buffer1[i] = 0;
309: buffer2[i] = 0;
310: }
311:
312: try {
313: f1 = new FileInputStream(file1);
314: f2 = new FileInputStream(file2);
315:
316: int length = f1.read(buffer1);
317: f2.read(buffer2);
318:
319: while (length != -1) {
320: if (!Arrays.equals(buffer1, buffer2)) {
321: f1.close();
322: f2.close();
323:
324: return false;
325: } else {
326: length = f1.read(buffer1);
327: f2.read(buffer2);
328: }
329: }
330:
331: f1.close();
332: f2.close();
333: } catch (Exception e) {
334: e.printStackTrace();
335:
336: return false;
337: } finally {
338: file1 = null;
339: file2 = null;
340: }
341:
342: return true;
343: }
344:
345: /**
346: * Compare the content of directory recursively with a binary file
347: * comparaison
348: *
349: * @param dir1
350: * @param dir2
351: * @return @throws
352: * Exception
353: */
354: public static boolean compareDir(String dir1, String dir2)
355: throws Exception {
356: File d1 = new File(dir1);
357: File d2 = new File(dir2);
358: String[] ldir1 = d1.list();
359: String[] ldir2 = d2.list();
360:
361: if ((ldir1 == null) || (ldir2 == null)) {
362: throw new Exception("cannot get list for dir1 or dir2 "
363: + dir1 + "," + dir2);
364: }
365:
366: Arrays.sort(ldir1);
367: Arrays.sort(ldir2);
368:
369: if (Arrays.equals(ldir1, ldir2)) {
370: for (int i = 0; i < ldir1.length; i++) {
371: File f1 = new File(d1.getAbsolutePath() + "/"
372: + ldir1[i]);
373: File f2 = new File(d2.getAbsolutePath() + "/"
374: + ldir2[i]);
375:
376: if (f1.isDirectory() && f2.isDirectory()) {
377: if (!ldir1[i].equals(Workspace.SO6PREFIX)) {
378: if (!compareDir(f1.getAbsolutePath(), f2
379: .getAbsolutePath())) {
380: return false;
381: }
382: }
383: } else if (f1.isFile() && f2.isFile()) {
384: if (!compareBinFile(f1, f2)) {
385: return false;
386: }
387: } else if ((f1.isDirectory() && f2.isFile())
388: || (f1.isFile() && f2.isDirectory())) {
389: return false;
390: } else {
391: return false;
392: }
393: }
394:
395: return true;
396: } else {
397: return false;
398: }
399: }
400:
401: /**
402: * List file in one String with a ";" as a file separator.
403: *
404: * @param dir
405: * @return @throws
406: * Exception
407: */
408: public static String list(String dir) throws Exception {
409: File d = new File(dir);
410: String[] ldir = d.list();
411:
412: if (ldir == null) {
413: throw new Exception("cannot get list for " + dir);
414: }
415:
416: String buff = new String();
417:
418: for (int i = 0; i < ldir.length; i++) {
419: buff = buff + ldir[i] + ";";
420: }
421:
422: return buff;
423: }
424:
425: /**
426: * Check if path exist
427: *
428: * @param file
429: * @return @throws
430: * Exception
431: */
432: public static boolean fileExists(String file) throws Exception {
433: return (new File(file)).exists();
434: }
435:
436: /**
437: * Check if the specified path is a directory
438: *
439: * @param dir
440: * @return @throws
441: * Exception
442: */
443: public static boolean isDirectory(String dir) throws Exception {
444: return (new File(dir)).isDirectory();
445: }
446:
447: /**
448: * Check if the specified path is a file
449: *
450: * @param file
451: * @return @throws
452: * Exception
453: */
454: public static boolean isFile(String file) throws Exception {
455: return (new File(file)).isFile();
456: }
457:
458: /**
459: * Walk a directoy recursively and add the file path in the specified
460: * vector.
461: *
462: * @param dir
463: * base path for the walk
464: * @param result
465: * vector of the file path
466: */
467: public static void walk(String dir, Vector result) {
468: File f = new File(dir);
469:
470: if (f.isFile() || f.isDirectory()) {
471: result.add(f.getPath());
472: }
473:
474: if (f.isDirectory()) {
475: String[] entries = f.list();
476:
477: for (int i = 0; i < entries.length; i++) {
478: walk(dir + File.separator + entries[i], result);
479: }
480: }
481: }
482:
483: /**
484: * Copy the content of the input file path to the output file path.
485: *
486: * @param input
487: * @param output
488: * @throws Exception
489: */
490: public static void copy(String input, String output)
491: throws Exception {
492: //System.out.println("copy: " + input +" -> "+ output);
493: FileInputStream fis = new FileInputStream(input);
494: FileOutputStream fos = new FileOutputStream(output);
495: byte[] buffer = new byte[1024];
496: int length;
497:
498: while ((length = fis.read(buffer)) != -1) {
499: fos.write(buffer, 0, length);
500: }
501:
502: fos.close();
503: fis.close();
504: }
505:
506: /**
507: * Copy the content of the input file to the output file .
508: *
509: * @param input
510: * @param output
511: * @throws Exception
512: */
513: public static void copy(File src, File dst) throws Exception {
514: if (src.isDirectory()) {
515: createDirIfNotExist(dst.getPath());
516:
517: File[] files = src.listFiles();
518:
519: for (int i = 0; i < files.length; i++) {
520: copy(files[i], new File(dst, files[i].getName()));
521: }
522: } else {
523: copy(src.getPath(), dst.getPath());
524: }
525: }
526:
527: /**
528: * Write the content of the file in the ZipOutputStream.
529: *
530: * @param fileName
531: * @param out
532: * @throws Exception
533: */
534: public static void writeFileInZip(String fileName,
535: ZipOutputStream out) throws Exception {
536: FileInputStream fis = new FileInputStream(fileName);
537: byte[] data = new byte[1024];
538: int byteCount;
539:
540: while ((byteCount = fis.read(data, 0, 1024)) > -1) {
541: out.write(data, 0, byteCount);
542: }
543:
544: out.flush();
545: }
546:
547: /**
548: * Convert path in order to replace the dependant file separator to the
549: * generic one "/"
550: *
551: * @param path
552: * @return
553: */
554: public static String convertPath(String path) {
555: return path.replaceAll("\\", "/");
556: }
557:
558: /**
559: * Check the first Mega byte of the file in order to determine if it's a
560: * text file or not...
561: *
562: * @param filePath
563: * @return @throws
564: * Exception
565: */
566: public static boolean isTextFile(String filePath) throws Exception {
567: final int bufferSize = 1024;
568: File f = new File(filePath);
569:
570: if (f.exists() && f.isFile()) {
571: FileInputStream fis = new FileInputStream(f);
572: byte[] data = new byte[bufferSize];
573: int byteCount = fis.read(data, 0, bufferSize);
574: fis.close();
575:
576: int non_text = 0;
577:
578: for (int i = 0; i < byteCount; i++) {
579: byte a = data[i];
580:
581: if ((a < 8) || ((a > 13) && (a < 32)) || (a > 126)) {
582: non_text++;
583: }
584: }
585:
586: // Check pourcent
587: // System.out.println(non_text + " / " + byteCount + " -> " +
588: // ((non_text * 100) / byteCount));
589: return (((non_text * 100) / byteCount) <= 20);
590: }
591:
592: throw new Exception("Error: it's not a file");
593: }
594:
595: /**
596: * Check if it's an XML file or not.
597: *
598: * @param filePath
599: * @return @throws
600: * Exception
601: */
602: public static boolean isXmlFile(String filePath) throws Exception {
603: File f = new File(filePath);
604:
605: if (!f.exists()) {
606: throw new Exception("Error the file " + f.getPath()
607: + " does not exist");
608: }
609:
610: if (f.length() == 0) {
611: return false;
612: }
613:
614: // check inside
615: LineNumberReader input = null;
616:
617: try {
618: input = new LineNumberReader(new FileReader(f));
619:
620: String line = input.readLine();
621:
622: if (line != null) {
623: // (Change this in order to make a regexp instead... )
624: return (line.startsWith("<?xml "))
625: || (line.startsWith("<office:document ")); // <?xml
626:
627: // version="1.0"?>
628: }
629: } finally {
630: if (input != null) {
631: input.close();
632: }
633: }
634:
635: return false;
636: }
637:
638: /**
639: * Return the base tmp path
640: *
641: * @return
642: */
643: public static String getBaseTmpPath() {
644: return System.getProperty("java.io.tmpdir") + File.separator
645: + BASE_TMP_DIR + "_" + System.getProperty("user.name");
646: }
647:
648: /**
649: * Clean all the files and directory stored in the base tmp path
650: *
651: * @throws Exception
652: */
653: public static void cleanSo6TmpFiles() throws Exception {
654: remove(getBaseTmpPath());
655: }
656:
657: public static boolean compareXmlFile(String file1, String file2)
658: throws Exception {
659: XyDiff d = new XyDiff(file1, file2);
660: Collection cmds = d.diff().getXMLCommand();
661:
662: for (Iterator i = cmds.iterator(); i.hasNext();) {
663: System.out.println(i.next());
664: }
665:
666: return cmds.size() == 0;
667: }
668: }
|