ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 6225] New: - new Concatenate task. supports path (not fileset), append, filters
Date Mon, 04 Feb 2002 16:40:49 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6225>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6225

new Concatenate task. supports path (not fileset), append, filters

           Summary: new Concatenate task. supports path (not fileset),
                    append, filters
           Product: Ant
           Version: 1.5 alpha (nightly)
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Core tasks
        AssignedTo: ant-dev@jakarta.apache.org
        ReportedBy: mrezaei@starpoint.com


/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.util.*;

import java.io.*;
import java.util.*;

/**
 * A consolidated concatenate task. Concatenates one or more files
 * to a destination file. It takes a path (not a fileset, therefore
 * it's ordered). It can optionally append to an existing destination
 * file. It also supports filters like the copy task.</p>
 *
 *       <concatenate toFile="wl_server/my_ejb_sql.txt" append="true">
 *           <src>
 *               <path location="${wlconfigDir}/base.config.xml"/>
 *               <path location="${ejbTemp}/ejb.config.xml"/>
 *               <fileset dir="wl_server">
 *                   <include name="my.config.xml"/>
 *               </fileset>
 *           </src>
 *           <filterset begintoken="%" endtoken="%" id="config.filters">
 *               <filter token="WL_SERVER" value="${wl.server}"/>
 *               <filter token="PROJECT_NAME" value="repeg"/>
 *               <filter token="PROJECT_HOME" value="${project.home}"/>
 *               <filter token="LOCAL_CONFIG_DIR" value="${local.config.dir}"/>
 *               <filter token="ORACLE_SID" value="${oracle.sid}"/>
 *               <filter token="WEBLOGIC_PORT_BASE" value="${weblogic.port}"/>
 *           </filterset>
 *       </concatenate>
 *
 *
 * <p>This implementation is based on the Copy task in Ant 1.4.1</p>
 *
 * @author Mohammad Rezaei
 */
public class Concatenate extends Task {
    protected File file = null;     // the source file 
    protected File destFile = null; // the destination file 
    protected Path src;

    protected boolean filtering = false;
    protected boolean append = false;
    protected int verbosity = Project.MSG_VERBOSE;


    private Vector filterSets = new Vector();

    /**
     * Sets a single source file to copy.
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * Sets the destination file.
     */
    public void setTofile(File destFile) {
        this.destFile = destFile;
    }

    /**
     * Create a nested filterset
     */
    public FilterSet createFilterSet() {
        FilterSet filterSet = new FilterSet();
        filterSets.addElement(filterSet);
        return filterSet;
    }
    

    /**
     * Get the filtersets being applied to this operation.
     *
     * @return a vector of FilterSet objects
     */
    protected Vector getFilterSets() {
        return filterSets;
    }
    
    /**
     * Sets filtering.
     */
    public void setFiltering(boolean filtering) {
        this.filtering = filtering;
    }

    /**
     * Append to the existing file
     */
    public void setAppend(boolean append) {
        this.append = append;
    }


    /**
     * Used to force listing of all names of copied files.
     */
    public void setVerbose(boolean verbose) {
        if (verbose) {
            this.verbosity = Project.MSG_INFO;
        } else {
            this.verbosity = Project.MSG_VERBOSE;
        } 
    } 


    /**
     * Create a nested <src ...> element for multiple source path
     * support.
     *
     * @return a nested src element.
     */
    public Path createSrc() {
        if (src == null) {
            src = new Path(project);
        }
        return src.createPath();
    }


    /**
     * Performs the concatenate operation.
     */
    public void execute() throws BuildException {
        // make sure we don't have an illegal set of options
        validateAttributes();   
        // Get the list of path components in canonical form
        String[] elems = src.list();
        String toFile = destFile.getPath();
        FilterSetCollection filters = new FilterSetCollection();
        if (filtering) {
            filters.addFilterSet(project.getGlobalFilterSet());
        }
        for (Enumeration filterEnum = filterSets.elements();
filterEnum.hasMoreElements();) {
            filters.addFilterSet((FilterSet)filterEnum.nextElement());
        }
        try
        {
            if (append) log("Appending to "+toFile, verbosity);
            else log("Writing to "+toFile, verbosity);
            if (filters != null && filters.hasFilters())
            {
                BufferedWriter writer = new BufferedWriter(new
FileWriter(toFile, append));
                for( int i=0; i < elems.length; i++ )
                {
                    String fromFile = elems[i];
                    log("Concatenating " + fromFile + " with filters", verbosity);
                    concatenateWithFilter(fromFile, writer, filters);
                }
                writer.close();
            }
            else
            {
                FileOutputStream out = new FileOutputStream(toFile, append);
                for( int i=0; i < elems.length; i++ )
                {
                    String fromFile = elems[i];
                    log("Concatenating " + fromFile, verbosity);
                    concatenate(fromFile, out);
                }
                out.close();
            }
        }
        catch(IOException e)
        {
            throw new BuildException("Unexpected io exception:
"+e.getClass().getName()+": "+e.getMessage());
        }

    }

//************************************************************************
//  protected and private methods
//************************************************************************

    /**
     * Ensure we have a consistent and legal set of attributes, and set
     * any internal flags necessary based on different combinations 
     * of attributes.
     */
    protected void validateAttributes() throws BuildException {
        if (src == null) {
            throw new BuildException("Specify at least one source file.");
        }

        if (destFile == null) {
            throw new BuildException("destfile must be set.");
        }


    }

    protected void concatenateWithFilter(String fromFile, BufferedWriter writer,
FilterSetCollection filters)
        throws IOException
    {
        BufferedReader in = new BufferedReader(new FileReader(fromFile));

        int length;
        String newline = null;
        String line = in.readLine();
        while (line != null) {
            if (line.length() == 0) {
                writer.newLine();
            } else {
                newline = filters.replaceTokens(line);
                writer.write(newline);
                writer.newLine();
            }
            line = in.readLine();
        }
        in.close();
    }
    
    protected void concatenate(String fromFile, OutputStream out)
        throws IOException
    {
        FileInputStream in = new FileInputStream(fromFile);

        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = in.read(buffer, 0, buffer.length);
        } while (count != -1);

        in.close();
    }

}

--
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