001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.compiler;
005:
006: import org.apache.tools.ant.BuildLogger;
007: import org.apache.tools.ant.DefaultLogger;
008: import org.apache.tools.ant.Project;
009: import org.apache.tools.ant.taskdefs.Copy;
010: import org.apache.tools.ant.taskdefs.Delete;
011: import org.apache.tools.ant.types.FileSet;
012: import org.apache.tools.ant.types.selectors.FilenameSelector;
013:
014: import java.io.File;
015:
016: /**
017: * Utility class providing file manipulation facilities. <p/>This implementation uses Ant task programmaticaly.
018: *
019: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
020: */
021: public class Utility {
022: /**
023: * Ant project
024: */
025: private Project project;
026:
027: /**
028: * Ant logger
029: */
030: private BuildLogger logger;
031:
032: private boolean verbose = false;
033:
034: /**
035: * Constructs a new project and attach simple logger
036: */
037: public Utility() {
038: project = new Project();
039: logger = new DefaultLogger();
040: logger.setMessageOutputLevel(0);
041: logger.setOutputPrintStream(System.out);
042: logger.setErrorPrintStream(System.err);
043: project.addBuildListener(logger);
044: }
045:
046: /**
047: * Set verbosity
048: */
049: public void setVerbose(boolean verbose) {
050: if (verbose) {
051: logger.setMessageOutputLevel(2);
052: verbose = true;
053: } else {
054: logger.setMessageOutputLevel(-1);
055: verbose = false;
056: }
057: }
058:
059: /**
060: * Delete recursively a directory and the directory itself
061: */
062: public void deleteDir(File dir) {
063: Delete task = new Delete();
064: task.setProject(project);
065: task.setTaskName("delete");
066: FilenameSelector fns = new FilenameSelector();
067: fns.setName("**/*");
068: FileSet fs = new FileSet();
069: fs.setDir(dir);
070: fs.addFilename(fns);
071: task.addFileset(fs);
072: task.setIncludeEmptyDirs(true);
073: task.perform();
074: dir.delete();
075: }
076:
077: /**
078: * Copy a file or directory recursively
079: */
080: public void backupFile(File source, File dest) {
081: Copy task = new Copy();
082: task.setProject(project);
083: task.setTaskName("backup");
084: task.setVerbose(verbose);
085:
086: //@todo haltOnError
087: //copyTask.setFailOnError(haltOnError);
088: if (source.isDirectory()) {
089: FilenameSelector fns = new FilenameSelector();
090: fns.setName("**/*");
091: FileSet fs = new FileSet();
092: fs.setDir(source);
093: fs.addFilename(fns);
094: task.addFileset(fs);
095: task.setTodir(dest);
096: task.setIncludeEmptyDirs(true);
097: } else {
098: task.setFile(source);
099: task.setTofile(dest);
100: }
101: task.setOverwrite(true);
102: task.setPreserveLastModified(true);
103: task.execute();
104: }
105:
106: public void log(String msg) {
107: project.log(msg);
108: }
109: }
|