package org.dita.dost.util;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.AbstractFileSet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.URLResource;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

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

public class JobSourceSet extends AbstractFileSet implements ResourceCollection {

    private String format;
    private Collection<Resource> res;
    private boolean isFilesystemOnly = true;

    public JobSourceSet() {
        super();
    }

    private Collection<Resource> getResults() {
        if (res == null) {
            if (format == null) {
                throw new IllegalStateException();
            }
            final Job job = getProject().getReference(ANT_REFERENCE_JOB);
            if (job == null) {
                throw new IllegalStateException();
            }
            res = new ArrayList<>();
            for (final Job.FileInfo f : job.getFileInfo(new Job.FileInfo.Filter() {
                @Override
                public boolean accept(final Job.FileInfo f) {
                    return f.format != null && f.format.equals(format);
                }
            })) {
                log("Scanning for " + f.file.getPath(), Project.MSG_VERBOSE);
                final File tempFile = new File(job.tempDir, f.file.getPath());
                if (tempFile.exists()) {
                    log("Found temporary directory file " + tempFile, Project.MSG_VERBOSE);
                    res.add(new FileResource(tempFile));
                } else if (f.src.getScheme().equals("file")) {
                    final File srcFile = new File(f.src);
                    if (srcFile.exists()) {
                        log("Found source directory file " + srcFile, Project.MSG_VERBOSE);
                        res.add(new FileResource(srcFile));
                    } else {
                        log("File " + f.src + " not found", Project.MSG_ERR);
                    }
                } else {
                    log("Found source URI " + f.src.toString(), Project.MSG_VERBOSE);
                    try {
                        res.add(new URLResource(f.src.toURL()));
                    } catch (final MalformedURLException e) {
                        throw new IllegalArgumentException(e);
                    }
                    isFilesystemOnly = false;
                }
            }
        }
        return res;
    }

    @Override
    public Iterator<Resource> iterator() {
        return getResults().iterator();
    }

    @Override
    public int size() {
        return getResults().size();
    }

    @Override
    public boolean isFilesystemOnly() {
        getResults();
        return isFilesystemOnly;
    }

    public void setFormat(final String format) {
        this.format = format;
    }

}

@@ -22,16 +22,62 @@
 .util;%0A%0A
+import static org.dita.dost.util.URLUtils.*;%0A%0A
 import o
@@ -105,16 +105,16 @@
 roject;%0A
-
 import o
@@ -426,24 +426,45 @@
 LException;%0A
+import java.net.URL;%0A
 import java.
@@ -1841,24 +1841,46 @@
 esource(
-tempFile
+job.tempDir, f.file.toString()
 ));%0A    
@@ -2181,23 +2181,60 @@
 esource(
-srcFile
+toFile(job.getInputDir()), f.file.toString()
 ));%0A    
@@ -2523,23 +2523,37 @@
     
-res.add(
+final JobResource r = 
 new 
-URL
+Job
 Reso
@@ -2561,22 +2561,87 @@
 rce(
-f.src.toURL())
+job.getInputDir().toURL(), f.uri.toString());%0A                        res.add(r
 );%0A 
@@ -3247,24 +3247,24 @@
 g format) %7B%0A
-
         this
@@ -3288,10 +3288,733 @@
 %0A    %7D%0A%0A
+    private static class JobResource extends URLResource %7B%0A        private String relPath;%0A        public JobResource(final URL baseURL, final String relPath) %7B%0A            super();%0A            setBaseURL(baseURL);%0A            setRelativePath(relPath);%0A            this.relPath = relPath;%0A        %7D%0A        /**%0A         * Get the name of this URLResource with original relative path.%0A         *%0A         * URLResource will return full URL file part that also contains ancestor directories.%0A         **/%0A        @Override%0A        public synchronized String getName() %7B%0A            if (isReference()) %7B%0A                return ((Resource) getCheckedRef()).getName();%0A            %7D%0A            return relPath;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Fix relative path in image copy