ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/file Touch.java Mkdir.java
Date Sun, 03 Feb 2002 07:10:11 GMT
donaldp     02/02/02 23:10:11

  Added:       proposal/myrmidon/src/java/org/apache/antlib/file Touch.java
                        Resources.properties Mkdir.java
  Removed:     proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/file
                        Touch.java Mkdir.java
  Log:
  Move the delete, touch and mkdir tasks into antlib. CLean and refactor each task to make
sure is closer to code-normalized. Also make sure all the messages are capable of being i18n'ed
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/file/Touch.java
  
  Index: Touch.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.file;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.util.ArrayList;
  import java.util.Locale;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.myrmidon.api.AbstractTask;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.tools.ant.types.DirectoryScanner;
  import org.apache.tools.ant.types.FileSet;
  import org.apache.tools.ant.types.ScannerUtil;
  
  /**
   * Touch a file and/or fileset(s) -- corresponds to the Unix touch command.
   *
   * If the file to touch doesn't exist, an empty one is created. </p>
   *
   * @ant:task name="touch"
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   * @author <a href="mailto:mj@servidium.com">Michael J. Sikorsky</a>
   * @author <a href="mailto:shaw@servidium.com">Robert Shaw</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/03 07:10:11 $
   */
  public class Touch
      extends AbstractTask
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( Touch.class );
  
      private long m_millis = -1;
      private String m_datetime;
      private ArrayList m_filesets = new ArrayList();
      private File m_file;
  
      /**
       * Date in the format MM/DD/YYYY HH:MM AM_PM.
       */
      public void setDatetime( final String datetime )
      {
          m_datetime = datetime;
      }
  
      /**
       * Sets a single source file to touch. If the file does not exist an empty
       * file will be created.
       */
      public void setFile( final File file )
      {
          m_file = file;
      }
  
      /**
       * Milliseconds since 01/01/1970 00:00 am.
       */
      public void setMillis( final long millis )
      {
          m_millis = millis;
      }
  
      /**
       * Adds a set of files (nested fileset attribute).
       */
      public void addFileset( final FileSet set )
      {
          m_filesets.add( set );
      }
  
      /**
       * Execute the touch operation.
       *
       * @exception TaskException Description of Exception
       */
      public void execute()
          throws TaskException
      {
          validate();
  
          if( m_datetime != null )
          {
              final DateFormat format =
                  DateFormat.getDateTimeInstance( DateFormat.SHORT,
                                                  DateFormat.SHORT,
                                                  Locale.US );
              try
              {
                  final long millis = format.parse( m_datetime ).getTime();
                  if( 0 > millis )
                  {
                      final String message = REZ.getString( "touch.neg-time.error", m_datetime
);
                      throw new TaskException( message );
                  }
                  setMillis( millis );
              }
              catch( final ParseException pe )
              {
                  throw new TaskException( pe.getMessage(), pe );
              }
          }
  
          touch();
      }
  
      private void validate()
          throws TaskException
      {
          if( null == m_file && 0 == m_filesets.size() )
          {
              final String message = REZ.getString( "touch.no-files.error" );
              throw new TaskException( message );
          }
  
          if( null != m_file && m_file.exists() && m_file.isDirectory() )
          {
              final String message = REZ.getString( "touch.use-fileset.error" );
              throw new TaskException( message );
          }
      }
  
      private void touch()
          throws TaskException
      {
          if( m_millis < 0 )
          {
              m_millis = System.currentTimeMillis();
          }
  
          if( null != m_file )
          {
              if( !m_file.exists() )
              {
                  if( getLogger().isInfoEnabled() )
                  {
                      final String message = REZ.getString( "touch.create.notice", m_file
);
                      getLogger().info( message );
                  }
  
                  try
                  {
                      FileOutputStream fos = new FileOutputStream( m_file );
                      fos.write( new byte[ 0 ] );
                      fos.close();
                  }
                  catch( final IOException ioe )
                  {
                      final String message = REZ.getString( "touch.no-touch.error", m_file,
ioe );
                      throw new TaskException( message, ioe );
                  }
              }
  
              touch( m_file );
          }
  
          // deal with the filesets
          final int size = m_filesets.size();
          for( int i = 0; i < size; i++ )
          {
              final FileSet fileSet = (FileSet)m_filesets.get( i );
              final DirectoryScanner scanner = ScannerUtil.getDirectoryScanner( fileSet );
              final File fromDir = fileSet.getDir();
  
              final String[] srcFiles = scanner.getIncludedFiles();
              final String[] srcDirs = scanner.getIncludedDirectories();
  
              for( int j = 0; j < srcFiles.length; j++ )
              {
                  final File file = new File( fromDir, srcFiles[ j ] );
                  touch( file );
              }
  
              for( int j = 0; j < srcDirs.length; j++ )
              {
                  final File file = new File( fromDir, srcDirs[ j ] );
                  touch( file );
              }
          }
      }
  
      private void touch( final File file )
          throws TaskException
      {
          if( !file.canWrite() )
          {
              final String message = REZ.getString( "touch.readonly-file.error", file );
              throw new TaskException( message );
          }
  
          final long time = ( m_millis < 0 ) ? System.currentTimeMillis() : m_millis;
          file.setLastModified( time );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/file/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  mkdir.missing-dir.error=dir attribute is required.
  mkdir.file-exists.error=Unable to create directory as a file already exists with that name:
"{0}".
  mkdir.nocreate.error=Failed to create directory {0} due to an unknown reason.
  mkdir.create.notice=Created dir: {0}
  
  touch.neg-time.error=Date of {0} results in negative milliseconds value relative to epoch
(January 1, 1970, 00:00:00 GMT).
  touch.no-files.error=Specify at least one source - a file or a fileset.
  touch.use-fileset.error=Use a fileset to touch directories.
  touch.readonly-file.error=Can not change modification date of read-only file {0}.
  touch.no-touch.error=Could not create file {0} due to {1}.
  touch.create.notice=Creating {0}.
  
  delete.nofiles.error=At least one of the file or dir attributes, or a fileset element, must
be set.
  delete.bad-file.error=Directory {0} cannot be removed using the file attribute.  Use dir
instead.
  delete.missing-file.error=Could not find file {0} to delete.
  delete.delete-dir.notice=Deleting directory {0}.
  delete.delete-dir.error=Unable to delete directory {0}.
  delete.delete-file.notice=Deleting {0}.
  delete.delete-file.error=Unable to delete file {0}.
  delete.delete-file.error=Deleting {0} files from {1}.
  delete.summary.notice=Deleted {0,choice,0#zero directories|1#1 directory|2<{0} directories}
from {1}.
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/file/Mkdir.java
  
  Index: Mkdir.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.file;
  
  import java.io.File;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.myrmidon.api.AbstractTask;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * Creates specified directory.
   *
   * @ant:task name="mkdir"
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author duncan@x180.com
   * @version $Revision: 1.1 $ $Date: 2002/02/03 07:10:11 $
   */
  public class Mkdir
      extends AbstractTask
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( Mkdir.class );
  
      private File m_dir;
  
      public void setDir( final File dir )
      {
          m_dir = dir;
      }
  
      public void execute()
          throws TaskException
      {
          if( null == m_dir )
          {
              final String message = REZ.getString( "mkdir.missing-dir.error" );
              throw new TaskException( message );
          }
  
          if( m_dir.isFile() )
          {
              final String message =
                  REZ.getString( "mkdir.file-exists.error", m_dir.getAbsolutePath() );
              throw new TaskException( message );
          }
  
          if( !m_dir.exists() )
          {
              final boolean result = m_dir.mkdirs();
              if( !result )
              {
                  final String message =
                      REZ.getString( "mkdir.nocreate.error", m_dir.getAbsolutePath() );
                  throw new TaskException( message );
              }
              final String message =
                  REZ.getString( "mkdir.create.notice", m_dir.getAbsolutePath() );
              getLogger().info( message );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message