package org.codehaus.mojo.animal_sniffer.ant;

/*
 * The MIT License
 *
 * Copyright (c) 2008 Kohsuke Kawaguchi and codehaus.org.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.codehaus.mojo.animal_sniffer.SignatureBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;

/**
 * @author Kohsuke Kawaguchi
 */
public class BuildSignaturesTask
    extends Task
{

    private File destfile;

    private Vector paths = new Vector();

    private Vector signatures = new Vector();

    public void setDestfile( File dest )
    {
        this.destfile = dest;
    }

    public void addPath( Path path )
    {
        paths.add( path );
    }

    public Signature createSignature()
    {
        Signature signature = new Signature();
        signatures.add( signature );
        return signature;
    }

    protected void validate()
    {
        if ( destfile == null )
        {
            throw new BuildException( "destfile not set" );
        }
        if ( paths.size() < 1 )
        {
            throw new BuildException( "path not set" );
        }
        Iterator i = signatures.iterator();
        while ( i.hasNext() )
        {
            Signature signature = (Signature) i.next();
            if ( signature.getSrc() == null )
            {
                throw new BuildException( "signature src not set" );
            }
            if ( !signature.getSrc().isFile() )
            {
                throw new BuildException( "signature " + signature.getSrc() + " does not exist" );
            }
        }
    }

    public void execute()
        throws BuildException
    {
        validate();
        try
        {
            Vector inStreams = new Vector();
            Iterator i = signatures.iterator();
            while ( i.hasNext() )
            {
                Signature signature = (Signature) i.next();
                log( "Importing signatures from " + signature.getSrc() );
                inStreams.add( new FileInputStream( signature.getSrc() ) );
            }

            SignatureBuilder builder =
                new SignatureBuilder( (InputStream[]) inStreams.toArray( new InputStream[inStreams.size()] ),
                                      new FileOutputStream( destfile ), new AntLogger( this ) );
            i = paths.iterator();
            while ( i.hasNext() )
            {
                Path path = (Path) i.next();
                final String[] files = path.list();
                for ( int j = 0; j < files.length; j++ )
                {
                    log( "Capturing signatures from " + files[j], Project.MSG_INFO );
                    process( builder, new File( files[j] ) );
                }
            }
            builder.close();
        }
        catch ( IOException e )
        {
            throw new BuildException( e );
        }
    }

    private void process( SignatureBuilder builder, File f )
        throws IOException
    {
        if ( f.exists() )
        {
            builder.process( f );
        }
    }
}

@@ -1818,24 +1818,126 @@
  Vector();%0A%0A
+    private Vector includeClasses = new Vector();%0A%0A    private Vector excludeClasses = new Vector();%0A%0A
     public v
@@ -1965,16 +1965,16 @@
  dest )%0A
-
     %7B%0A  
@@ -2243,24 +2243,342 @@
 ure;%0A    %7D%0A%0A
+    public Ignore createIncludeClasses()%0A    %7B%0A        final Ignore result = new Ignore();%0A        includeClasses.add( result );%0A        return result;%0A    %7D%0A%0A    public Ignore createExcludeClasses()%0A    %7B%0A        final Ignore result = new Ignore();%0A        excludeClasses.add( result );%0A        return result;%0A    %7D%0A%0A
     protecte
@@ -3281,32 +3281,607 @@
     %7D%0A        %7D%0A
+        i = includeClasses.iterator();%0A        while ( i.hasNext() )%0A        %7B%0A            Ignore tmp = (Ignore) i.next();%0A            if ( tmp.getClassName() == null )%0A            %7B%0A                throw new BuildException( %22includeClasses className not set%22 );%0A            %7D%0A        %7D%0A        i = excludeClasses.iterator();%0A        while ( i.hasNext() )%0A        %7B%0A            Ignore tmp = (Ignore) i.next();%0A            if ( tmp.getClassName() == null )%0A            %7B%0A                throw new BuildException( %22excludeClasses className not set%22 );%0A            %7D%0A        %7D%0A%0A
     %7D%0A%0A    publi
@@ -4490,16 +4490,16 @@
 e()%5D ),%0A
-
         
@@ -4583,24 +4583,446 @@
 ( this ) );%0A
+            i = includeClasses.iterator();%0A            while ( i.hasNext() )%0A            %7B%0A                Ignore tmp = (Ignore) i.next();%0A                builder.addInclude( tmp.getClassName() );%0A            %7D%0A            i = excludeClasses.iterator();%0A            while ( i.hasNext() )%0A            %7B%0A                Ignore tmp = (Ignore) i.next();%0A                builder.addExclude( tmp.getClassName() );%0A            %7D%0A
             

add include/exclude support to the ant tasks