[Arakhnę-Dev] [304] * Add dirCopy() and dirRemove() functions.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 304
Author:   galland
Date:     2011-11-05 11:30:56 +0100 (Sat, 05 Nov 2011)
Log Message:
-----------
* Add dirCopy() and dirRemove() functions.

Modified Paths:
--------------
    trunk/maventools/src/main/java/org/arakhne/maven/AbstractArakhneMojo.java

Modified: trunk/maventools/src/main/java/org/arakhne/maven/AbstractArakhneMojo.java
===================================================================
--- trunk/maventools/src/main/java/org/arakhne/maven/AbstractArakhneMojo.java	2011-11-05 10:30:31 UTC (rev 303)
+++ trunk/maventools/src/main/java/org/arakhne/maven/AbstractArakhneMojo.java	2011-11-05 10:30:56 UTC (rev 304)
@@ -45,6 +45,7 @@
 import java.util.Date;
 import java.util.EventListener;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -145,6 +146,87 @@
 	public static final String PREFERRED_CHARSET_JVM = "UTF-16"; //$NON-NLS-1$
 
 	/**
+	 * Copy a directory.
+	 * 
+	 * @param in
+	 * @param out
+	 * @throws IOException
+	 * @since 3.3
+	 */
+	public final void dirCopy(File in, File out) throws IOException {
+		assert (in != null);
+		assert (out != null);
+		debug("Copying tree "+in.toString()+" into "+out.toString());  //$NON-NLS-1$//$NON-NLS-2$
+		out.mkdirs();
+		LinkedList<File> candidates = new LinkedList<File>();
+		candidates.add(in);
+		File f;
+		File[] children;
+		while (!candidates.isEmpty()) {
+			f = candidates.removeFirst();
+			if (f.isDirectory()) {
+				f.mkdirs();
+				children = f.listFiles();
+				if (children!=null && children.length>1) {
+					// Non empty directory
+					for(File c : children) {
+						candidates.add(c);
+					}
+				}
+			}
+			else {
+				// not a directory
+				fileCopy(f, toOutput(in, f, out));
+			}
+		}		
+	}
+	
+	private File toOutput(File root, File file, File newRoot) {
+		String filename = file.getAbsolutePath();
+		String rootPath = root.getAbsolutePath();
+		return new File(filename.replaceAll("^\\Q"+rootPath+"\\E", newRoot.getAbsolutePath()));  //$NON-NLS-1$//$NON-NLS-2$
+	}
+
+	/**
+	 * Delete a directory and its content.
+	 * 
+	 * @param dir
+	 * @throws IOException
+	 * @since 3.3
+	 */
+	public final void dirRemove(File dir) throws IOException {
+		if (dir!=null) {
+			debug("Deleting tree: "+dir.toString()); //$NON-NLS-1$
+			LinkedList<File> candidates = new LinkedList<File>();
+			candidates.add(dir);
+			File f;
+			File[] children;
+			while (!candidates.isEmpty()) {
+				f = candidates.getFirst();
+				if (f.isDirectory()) {
+					children = f.listFiles();
+					if (children!=null && children.length>1) {
+						// Non empty directory
+						for(File c : children) {
+							candidates.push(c);
+						}
+					}
+					else {
+						// empty directory
+						candidates.removeFirst();
+						f.delete();
+					}
+				}
+				else {
+					// not a directory
+					candidates.removeFirst();
+					if (f.exists()) f.delete();
+				}
+			}
+		}
+	}
+
+	/**
 	 * Copy a file.
 	 * 
 	 * @param in
@@ -154,6 +236,7 @@
 	public final void fileCopy(File in, File out) throws IOException {
 		assert (in != null);
 		assert (out != null);
+		debug("Copying file: "+in.toString()+" into "+out.toString()); //$NON-NLS-1$ //$NON-NLS-2$
 		FileChannel inChannel = new FileInputStream(in).getChannel();
 		FileChannel outChannel = new FileOutputStream(out).getChannel();
 		try {


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/