/*
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   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.
 */
package net.brutex.xservices.types.ant;

import java.io.File;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlType;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.TarFileSet;
import org.apache.tools.ant.types.ZipFileSet;

/**
 * Set of files from various sources.
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
@XmlType(name = "FileSetType", namespace = "http://ws.xservices.brutex.net",
propOrder = {"type", "source", "filter", "excludes", "casesensitive"})
public class FileSetResource implements ResourceSetInterface {

	public static final String XML_NAME = "fileset";
    /**
     * Type of FileSet
     */
    @XmlElement(name = "FileSetType", required = true, nillable = false, defaultValue = "FILES")
    public FileSetType type = FileSetType.FILES;
    /**
     * File set source.
     * 
     * Depends on the file set type. This is either an archive file or a
     * directory.
     */
    @XmlElement(name = "source", required = true, nillable = false)
    public String source = "";
    /**
     * Pattern of files to include.
     * 
     */
    @XmlElement(name = PatternSetType.XML_NAME, required = true, nillable = true)
    public PatternSetType filter;

    /**
     * Pattern of files to exclude.
     */
    @XmlElement(name = "excludes", required = false, nillable = true, defaultValue = "")
    public String excludes = "";
    /**
     * Case sensitivity for include/ exclude patterns.
     */
    @XmlElement(name = "casesensitive", required = true, nillable = false, defaultValue = "true")
    public boolean casesensitive = true;

    /**
     * Get Ant FileSet for this file set.
     *
     * @param p     Ant project
     * @return      Ant FileSet for this file set.
     */
    public FileSet getAntResource(Project p) {
        FileSet set = null;
        switch (type) {
            case ZIP:
                ZipFileSet zset = new ZipFileSet();
                zset.setSrc(new File(source));
                zset.setEncoding(System.getProperty("file.encoding"));
                set = zset;
                break;
            case TAR:
                TarFileSet tset = new TarFileSet();
                tset.setSrc(new File(source));
                set = tset;
                break;
            case GZTAR:
                Resource res = new FileResource(FileResource.Type.GZIP, source).getAntResource(p);
                TarFileSet gzset = new TarFileSet();
                gzset.setSrcResource(res);
                set = gzset;
                break;
            default: //FILES
                set = new FileSet();
                set.setDir(new File(source));
        }
        set.setProject(p);
        //set.setIncludes(includes);
        set.setExcludes(excludes);
        set.setCaseSensitive(casesensitive);

        return set;
    }

    /**
     * File set types.
     */
    @XmlEnum()
    @XmlType(name="resourcetype")
    public enum FileSetType {

        /**
         * Set of files (this is based on a directory, so provide a path only
         * as file set source).
         */
        FILES,
        /**
         * Set of files inside a ZIP archive.
         */
        ZIP,
        /**
         * Set of files inside a TAR archive (without compression).
         */
        TAR,
        /**
         * Set of files inside a gzip compressed TAR archive.
         */
        GZTAR
    }
}
