View Javadoc

    1   /***************************************************************************************
    2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
    3    * http://aspectwerkz.codehaus.org                                                    *
    4    * ---------------------------------------------------------------------------------- *
    5    * The software in this package is published under the terms of the LGPL license      *
    6    * a copy of which has been included with this distribution in the license.txt file.  *
    7    **************************************************************************************/
    8   package org.codehaus.aspectwerkz.compiler;
    9
    10  import org.apache.tools.ant.BuildLogger;
    11  import org.apache.tools.ant.DefaultLogger;
    12  import org.apache.tools.ant.Project;
    13  import org.apache.tools.ant.taskdefs.Copy;
    14  import org.apache.tools.ant.taskdefs.Delete;
    15  import org.apache.tools.ant.types.FileSet;
    16  import org.apache.tools.ant.types.selectors.FilenameSelector;
    17
    18  import java.io.File;
    19
    20  /***
    21   * Utility class providing file manipulation facilities. <p/>This implementation uses Ant task programmaticaly.
    22   *
    23   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
    24   */
    25  public class Utility {
    26      /***
    27       * Ant project
    28       */
    29      private Project project;
    30
    31      /***
    32       * Ant logger
    33       */
    34      private BuildLogger logger;
    35
    36      private boolean verbose = false;
    37
    38      /***
    39       * Constructs a new project and attach simple logger
    40       */
    41      public Utility() {
    42          project = new Project();
    43          logger = new DefaultLogger();
    44          logger.setMessageOutputLevel(0);
    45          logger.setOutputPrintStream(System.out);
    46          logger.setErrorPrintStream(System.err);
    47          project.addBuildListener(logger);
    48      }
    49
    50      /***
    51       * Set verbosity
    52       */
    53      public void setVerbose(boolean verbose) {
    54          if (verbose) {
    55              logger.setMessageOutputLevel(2);
    56              verbose = true;
    57          } else {
    58              logger.setMessageOutputLevel(-1);
    59              verbose = false;
    60          }
    61      }
    62
    63      /***
    64       * Delete recursively a directory and the directory itself
    65       */
    66      public void deleteDir(File dir) {
    67          Delete task = new Delete();
    68          task.setProject(project);
    69          task.setTaskName("delete");
    70          FilenameSelector fns = new FilenameSelector();
    71          fns.setName("**/*");
    72          FileSet fs = new FileSet();
    73          fs.setDir(dir);
    74          fs.addFilename(fns);
    75          task.addFileset(fs);
    76          task.setIncludeEmptyDirs(true);
    77          task.perform();
    78          dir.delete();
    79      }
    80
    81      /***
    82       * Copy a file or directory recursively
    83       */
    84      public void backupFile(File source, File dest) {
    85          Copy task = new Copy();
    86          task.setProject(project);
    87          task.setTaskName("backup");
    88          task.setVerbose(verbose);
    89
    90          //@todo haltOnError
    91          //copyTask.setFailOnError(haltOnError);
    92          if (source.isDirectory()) {
    93              FilenameSelector fns = new FilenameSelector();
    94              fns.setName("**/*");
    95              FileSet fs = new FileSet();
    96              fs.setDir(source);
    97              fs.addFilename(fns);
    98              task.addFileset(fs);
    99              task.setTodir(dest);
    100             task.setIncludeEmptyDirs(true);
    101         } else {
    102             task.setFile(source);
    103             task.setTofile(dest);
    104         }
    105         task.setOverwrite(true);
    106         task.setPreserveLastModified(true);
    107         task.execute();
    108     }
    109
    110     public void log(String msg) {
    111         project.log(msg);
    112     }
    113 }