/*
 * This file is part of the DITA Open Toolkit project hosted on
 * Sourceforge.net. See the accompanying license.txt file for
 * applicable licenses.
 */

/*
 * (c) Copyright IBM Corp. 2005 All Rights Reserved.
 */
package org.dita.dost.util;

import static org.dita.dost.util.Constants.*;

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;
import org.dita.dost.log.DITAOTLogger;
import org.dita.dost.log.DITAOTAntLogger;

/**
 * Class description goes here.
 *
 * @author Wu, Zhi Qiang
 */
public final class DITAOTCopy extends Task {
    private String includes = null;
    private String relativePaths = null;
    private String destDir = null;  // the destination directory
    private DITAOTLogger logger;

    /**
     * Default Constructor.
     * 
     */
    public DITAOTCopy(){
    }

    /**
     * Set the copy files.
     * @param incld The includes to set.
     */
    public void setIncludes(final String incld) {
        this.includes = incld;
    }

    /**
     * Set the destination directory.
     * @param destdir the destination directory.
     */
    public void setTodir(final String destdir) {
        this.destDir = destdir;
    }

    /**
     * Set the relative path from output directory.
     * @param relPaths the relative path .
     */
    public void setRelativePaths(final String relPaths) {
        this.relativePaths = relPaths;
    }

    /**
     * @see org.apache.tools.ant.Task#execute()
     */
    @Override
    public void execute() throws BuildException {
        logger = new DITAOTAntLogger(getProject());
        final FileUtils fileUitls = FileUtils.newFileUtils();
        StringTokenizer tokenizer;
        StringTokenizer pathTokenizer;
        if (includes == null) {
            return;
        }
        tokenizer = new StringTokenizer(includes, COMMA);
        if (relativePaths == null) {
            try {
                while (tokenizer.hasMoreTokens()) {
                    final File srcFile = new File(tokenizer.nextToken());
                    if (srcFile.exists()) {
                        final File destFile = new File(destDir, srcFile.getName());
                        fileUitls.copyFile(srcFile, destFile);
                    }
                }
            } catch (final IOException e) {
                logger.logException(e);
            }
        }else{
            pathTokenizer = new StringTokenizer(relativePaths, COMMA);
            StringBuffer realDest=null;
            try {
                while (tokenizer.hasMoreTokens()) {
                    realDest=new StringBuffer();
                    //destDir is the ouput dir
                    //pathTokenizer is the relative path with the filename
                    if(destDir!=null && destDir.trim().length()>0){
                        realDest.append(destDir).append(File.separator).append(pathTokenizer.nextToken());
                    }
                    final File srcFile = new File(tokenizer.nextToken());
                    if (srcFile.exists()) {
                        final File destFile = new File(realDest.toString());
                        fileUitls.copyFile(srcFile, destFile);
                    }
                }
            } catch (final IOException e) {
                logger.logException(e);
            }
        }
    }

}
@@ -2688,39 +2688,103 @@
     
-    realDest=new StringBuffer()
+%09final File srcFile = new File(tokenizer.nextToken());%0A                    File destFile = null
 ;%0A  
@@ -2927,18 +2927,129 @@
     
+pathTokenizer = new StringTokenizer(relativePaths, COMMA);%0A%09%09%09%09%09while (pathTokenizer.hasMoreTokens()) %7B%0A%09%09%09%09%09%09
 if
+ 
 (destDir
 !=nu
@@ -3048,10 +3048,12 @@
 tDir
+ 
 !=
+ 
 null
@@ -3083,37 +3083,59 @@
 th()
-%3E
+ %3E 
 0)
+ 
 %7B%0A
-                        
+%09%09%09%09%09%09%09realDest=new StringBuffer();%0A%09%09%09%09%09%09%09
 real
@@ -3177,16 +3177,26 @@
 parator)
+%0A%09%09%09%09%09%09%09%09%09
 .append(
@@ -3227,103 +3227,185 @@
 ));%0A
-                    %7D%0A                    final File srcFile = new File(tokenizer.nextToken());
+%09%09%09%09%09%09%09File temp = new File(realDest.toString());%0A%09%09%09%09%09%09%09if (temp.getName().equalsIgnoreCase(srcFile.getName()))%7B%0A%09%09%09%09%09%09%09%09destFile = temp;%0A%09%09%09%09%09%09%09%09break;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D
 %0A   
@@ -3433,38 +3433,51 @@
 srcFile.exists()
+&&destFile!=null
 ) %7B
-%0A  
                 
@@ -3486,60 +3486,8 @@
     
-final File destFile = new File(realDest.toString());
 %0A   

Add a missing change in Git -- wrong image output dir if using generate.copy.outer