package org.apache.maven.plugin.resources;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
import org.codehaus.plexus.util.FileUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 * @version $Id$
 * @goal resources
 * @description copy application resources
 * @parameter name="outputDirectory"
 * type="String"
 * required="true"
 * validator=""
 * expression="#project.build.outputDirectory"
 * description=""
 * @parameter name="resources"
 * type="List"
 * required="true"
 * validator=""
 * expression="#project.build.resources"
 * description=""
 */
public class ResourcesMojo
    extends AbstractPlugin
{
    public void execute( PluginExecutionRequest request, PluginExecutionResponse response )
        throws Exception
    {
        // ----------------------------------------------------------------------
        //
        // ----------------------------------------------------------------------

        String outputDirectory = (String) request.getParameter( "outputDirectory" );

        List resources = (List) request.getParameter( "resources" );

        // ----------------------------------------------------------------------
        //
        // ----------------------------------------------------------------------

        for ( Iterator i = getJarResources( resources ).iterator(); i.hasNext(); )
        {
            ResourceEntry resourceEntry = (ResourceEntry) i.next();

            File destinationFile = new File( outputDirectory, resourceEntry.getDestination() );

            if ( !destinationFile.getParentFile().exists() )
            {
                destinationFile.getParentFile().mkdirs();
            }

            fileCopy( resourceEntry.getSource(), destinationFile.getPath() );
        }
    }

    private List getJarResources( List resources )
        throws Exception
    {
        List resourceEntries = new ArrayList();

        for ( Iterator i = resources.iterator(); i.hasNext(); )
        {
            Resource resource = (Resource) i.next();

            String targetPath = resource.getTargetPath();

            File resourceDirectory = new File( resource.getDirectory() );

            if ( !resourceDirectory.exists() )
            {
                continue;
            }

            // If we only have a directory then we want to include
            // everything we can find within that path.

            String includes = resource.getIncludes();

            if ( includes == null || includes.length() == 0 )
            {
                includes = "**/**";
            }

            String excludes = resource.getExcludes();

            if ( excludes != null && excludes.length() > 0 )
            {
                excludes += ",";
            }
            excludes += listToString( resource.getDefaultExcludes() );

            List files = FileUtils.getFileNames( resourceDirectory, includes, excludes, false );

            for ( Iterator j = files.iterator(); j.hasNext(); )
            {
                String name = (String) j.next();

                String entryName = name;

                if ( targetPath != null )
                {
                    entryName = targetPath + "/" + name;
                }

                ResourceEntry je = new ResourceEntry( new File( resource.getDirectory(), name ).getPath(), entryName );

                resourceEntries.add( je );
            }
        }

        return resourceEntries;
    }

    private String listToString( List list )
    {
        StringBuffer sb = new StringBuffer();

        for ( int i = 0; i < list.size(); i++ )
        {
            sb.append( list.get( i ) );

            if ( i != list.size() - 1 )
            {
                sb.append( "," );
            }
        }

        return sb.toString();
    }

    public static byte[] fileRead( String fileName )
        throws IOException
    {
        FileInputStream in = new FileInputStream( fileName );
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int count;
        byte[] b = new byte[512];
        while ( ( count = in.read( b ) ) > 0 )  // blocking read
        {
            buffer.write( b, 0, count );
        }

        in.close();

        byte[] content = buffer.toByteArray();

        buffer.close();

        return content;
    }

    public static void fileWrite( String fileName, byte[] data )
        throws Exception
    {
        FileOutputStream out = new FileOutputStream( fileName );
        out.write( data );
        out.close();
    }

    public static void fileCopy( String inFileName, String outFileName )
        throws Exception
    {
        byte[] content = fileRead( inFileName );
        fileWrite( outFileName, content );
    }

    class ResourceEntry
    {
        private String source;

        private String destination;

        public ResourceEntry( String source, String entry )
        {
            this.source = source;

            this.destination = entry;
        }

        public String getSource()
        {
            return source;
        }

        public String getDestination()
        {
            return destination;
        }
    }
}

@@ -956,71 +956,17 @@
 tion
-Request;%0Aimport org.apache.maven.plugin.PluginExecutionResponse
+Exception
 ;%0Aim
@@ -1812,634 +1812,167 @@
    p
-ublic void execute( PluginExecutionRequest request, PluginExecutionResponse response )%0A        throws Exception%0A    %7B%0A        // ----------------------------------------------------------------------%0A        //%0A        // ----------------------------------------------------------------------%0A%0A        String outputDirectory = (String) request.getParameter( %22outputDirectory%22 );%0A%0A        List resources = (List) request.getParameter( %22resources%22 );%0A
+rivate String outputDirectory;%0A%0A    private List resources;%0A%0A    public void execute()%0A        throws PluginExecutionException
 %0A    
+%7B%0A
     
-// ----------------------------------------------------------------------%0A        //%0A        // ----------------------------------------------------------------------%0A%0A
+    try%0A        %7B%0A    
     
@@ -2050,34 +2050,42 @@
 xt(); )%0A        
-%7B%0A
+    %7B%0A    
             Reso
@@ -2129,32 +2129,36 @@
 try) i.next();%0A%0A
+    
             File
@@ -2246,24 +2246,28 @@
             
+    
 if ( !destin
@@ -2307,34 +2307,42 @@
 ) )%0A            
+   
+ 
 %7B%0A
+    
                 
@@ -2387,35 +2387,43 @@
 ();%0A            
+ 
-%7D%0A%0A
+   %7D%0A%0A    
             file
@@ -2480,24 +2480,206 @@
 etPath() );%0A
+            %7D%0A        %7D%0A        catch ( Exception e )%0A        %7B%0A            // TODO: handle exception%0A            throw new PluginExecutionException( %22Error copying resources%22, e );%0A
         %7D%0A  

convert resources mojo to new execute().