User: kimptoc 
  Date: 01/05/26 01:09:52

  Added:       src/main/org/jboss/ant/taskdefs ConcatenateFiles.java
  Log:
  added test report xslt scripts and builds to jbosstest
  
  Revision  Changes    Path
  1.1                  jbosstest/src/main/org/jboss/ant/taskdefs/ConcatenateFiles.java
  
  Index: ConcatenateFiles.java
  ===================================================================
  package org.jboss.ant.taskdefs;
  
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.types.*;
  
  /**
  TEMPORARY TASK - until this is added to the core Ant build
  
  Thanks to Chris Winters
  <a 
href=http://www.mail-archive.com/[email protected]/msg04736.html>source</a>
  **/
  public class ConcatenateFiles
      extends Task
  {
      protected File file           = null;
      protected File destFile       = null;
      protected File beginFile      = null;
      protected File endFile        = null;
      protected String beginMessage = null;
      protected String endMessage   = null;
      protected List filesets       = new ArrayList();
      protected int verbosity       = Project.MSG_VERBOSE;
  
      public void setFile( File _set )           { file = _set; }
      public void setBeginfile( File _set )      { beginFile = _set; }
      public void setEndfile( File _set )        { endFile = _set; }
      public void setBeginmessage( String _set ) { beginMessage = _set; }
      public void setEndmessage( String _set )   { endMessage = _set; }
      public void addFileset( FileSet set )      { filesets.add( set ); }
      public void setVerbose( boolean _set )
      {
          if ( _set ) { verbosity = Project.MSG_INFO; }
          else        { verbosity = Project.MSG_VERBOSE; }
      }
  
  
      /**
       * Perform the actual action. First validate the parameters passed
       * to the task to ensure everything is sane, then open up the
       * output file. Next do the beginning file/message, process the
       * fileset and then do the ending file/message.
       *
       * @exception BuildException for any IO problems (can't read file,
       * can't open file, can't write to file, etc.)
       */
      public void execute()
          throws BuildException
      {
          // Ensure everything is set correctly
  
          validateAttributes();
          FileWriter out = null;
          try
          {
  
            // Open up the new file and a writer to it.
  
            destFile = new File( file.getAbsolutePath() );
            if ( destFile.exists() )
            {
              log( "File " + destFile.getAbsolutePath() + " exists; removing.",
  verbosity );
              destFile.delete();
              destFile.createNewFile();
            }
            out = new FileWriter( destFile.getAbsolutePath() );
  
            // If the beginFile is defined, contatenate it
  
            if ( beginFile != null )
            {
              concatenate( out, beginFile );
              log( "Beginning file " + beginFile.getAbsolutePath() + " ok", verbosity 
);
            }
            if ( beginMessage != null )
            {
              concatenate( out, beginMessage );
              log( "Beginning message ok", verbosity );
            }
  
            // Now do the filesets specified
  
            Iterator fsi = filesets.iterator();
            while ( fsi.hasNext() )
            {
              FileSet fs = (FileSet)fsi.next();
              DirectoryScanner ds = fs.getDirectoryScanner( project );
              String[] srcFiles = ds.getIncludedFiles();
              File baseDir = ds.getBasedir().getAbsoluteFile();
              for ( int i = 0; i < srcFiles.length; i++ )
              {
                File readFile = new File( baseDir, srcFiles[i] );
                concatenate( out, readFile );
                log( "Fileset file " + readFile.getAbsolutePath() + " ok", verbosity );
              }
            }
  
            // And if the endFile is defined, do it
  
            if ( endFile != null )
            {
              concatenate( out, endFile );
              log( "Ending file " + endFile.getAbsolutePath() + " ok", verbosity );
            }
            if ( endMessage != null )
            {
              concatenate( out, endMessage );
              log( "Ending message ok", verbosity );
            }
            log( "Created new file (" + destFile.getAbsolutePath() + ") successfully" 
);
            out.close();
          }
          catch ( IOException ioe )
          {
            throw new BuildException( "Error with the filesystem: " + ioe.getMessage() 
);
          }
          finally
          {
              try
              {
                  if ( out != null ) { out.close(); }
              }
              catch ( IOException ioe )
              {
                  throw new BuildException( "Cannot close output buffer! Error: " +
  ioe.getMessage() );
              }
          }
      }
  
      /**
       * Ensure we have the right attributes. Failure conditions are:
       *
       * <ul>
       *   <li>destination file is not specified</li>
       *   <li>both 'beginmessage' and 'beginfile' are specified</li>
       *   <li>both 'endmessage' and 'endfile' are specified</li>
       *   <li>no messages or files are specified at all</li>
       * </ul>
       *
       * @throws BuildException if one of the specifications is not met.
       */
      protected void validateAttributes()
          throws BuildException
      {
          if ( file == null )
          {
            throw new BuildException("You must specify a destfile." );
          }
          if ( beginMessage != null && beginFile != null )
          {
            throw new BuildException(  "You cannot specify both 'beginmessage' and 
'beginfile'" );
          }
          if ( endMessage != null && endFile != null )
          {
            throw new BuildException(  "You cannot specify both 'endmessage' and 
'endfile'" );
          }
          boolean hasMessage = ( beginMessage != null || endMessage != null );
          boolean hasFile    = ( beginFile != null  || endFile != null || 
filesets.size() > 0 );
          if ( ! hasMessage && ! hasFile )
          {
            throw new BuildException( "You must specify one or more messages or 
files.");
          }
      }
  
      /**
       * Concatenate two text files
       *
       * @param to file object appending to
       * @param from file whose contents we're appending
       * @throws IOException if we can't read the file or if the actual
       * write fails
       */
      private static void concatenate( FileWriter to, File from )
          throws IOException
      {
          BufferedReader in = new BufferedReader( new FileReader( from ) );
          String inBuf = new String();
          StringBuffer contents = new StringBuffer();
          while ( ( inBuf = in.readLine() ) != null )
          {
            contents.append( inBuf + "\r\n" );
          }
          in.close();
          concatenate( to, contents.toString() );
      }
  
      /**
       * Concatenate a string to a text file
       *
       * @param to file object appending to
       * @param from string we're appending
       * @throws IOException if the write fails
       */
      private static void concatenate( FileWriter to, String from )
          throws IOException
      {
          to.write( from );
      }
  }
  
  
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to