/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.zip.*;

public class TaskLib extends Jar { 

    private static String lSep = System.getProperty("line.separator");
    private static final DocumentBuilder builder = getDocumentBuilder();

    private static DocumentBuilder getDocumentBuilder() {
        try {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder();
        }
        catch(Exception exc) {
            throw new ExceptionInInitializerError(exc);
        }
    }

    private Document doc;
    private byte[] bytes;
    
    private String name = null;
    private String version = null;
    private String home = null;
    
    private Vector tasks = new Vector();
    
    public void setName(String value) { 
        name = value;
    }
    public void setVersion(String value) { 
        version = value;
    }
    public void setHome(String value) { 
        home = value;
    }
    
    public void execute() throws BuildException {
        doc = builder.newDocument();
        
        Element tasklibElement = doc.createElement("tasklib");
        tasklibElement.setAttribute("name", name);
        tasklibElement.setAttribute("version", version);
        tasklibElement.setAttribute("home", home);

        Enumeration enum = tasks.elements();
        while (enum.hasMoreElements()) { 
            TaskLib.Task task = (TaskLib.Task)enum.nextElement();
            
            Element taskElement = doc.createElement("task");
            taskElement.setAttribute("name", task.getName());
            taskElement.setAttribute("classname", task.getClassname());
            taskElement.setAttribute("version", task.getVersion());
            tasklibElement.appendChild(taskElement);
        }

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Writer out = new PrintWriter(bos);
            out.write("<?xml version=\"1.0\"?>" + lSep + lSep);
            write(tasklibElement, out, 0);
            out.flush();
            out.close();
            bytes = bos.toByteArray();
            
        } catch(IOException exc) {
            throw new BuildException("Unable to close log file", exc);
        }
        super.execute();
    }

    /**
     *  Writes a DOM element to a file.
     */
    private static void write(Element element, Writer out, int indent) throws IOException {

        // Write indent characters
        for (int i = 0; i < indent; i++) {
            out.write("\t");
        }

        // Write element
        out.write("<");
        out.write(element.getTagName());

        // Write attributes
        NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            out.write(" ");
            out.write(attr.getName());
            out.write("=\"");
            out.write(attr.getValue());
            out.write("\"");
        }
        out.write(">");

        // Write child attributes and text
        boolean hasChildren = false;
        NodeList children = element.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (!hasChildren) {
                    out.write("\n");
                    hasChildren = true;
                }
                write((Element)child, out, indent + 1);
            }

            if (child.getNodeType() == Node.TEXT_NODE) {
                out.write(((Text)child).getData());
            }
        }

        // If we had child elements, we need to indent before we close
        // the element, otherwise we're on the same line and don't need
        // to indent
        if (hasChildren) {
            for (int i = 0; i < indent; i++) {
                out.write("\t");
            }
        }

        // Write element close
        out.write("</");
        out.write(element.getTagName());
        out.write(">\n");
    }
    
    public TaskLib.Task createTask() { 
        TaskLib.Task task = new TaskLib.Task();
        tasks.addElement(task);
        return task;
    }    

    public static class Task {
        private String taskName = null;
        private String taskClass = null;
        private String taskVersion = null;
        
        public void setName(String value) { 
            taskName = value;
        }
        public String getName() {
            return taskName;
        }
        public void setClassname(String value) { 
            taskClass = value;
        }
        public String getClassname() {
            return taskClass;
        }
        public void setVersion(String value) { 
            taskVersion = value;
        }
        public String getVersion() {
            return taskVersion;
        }
    }
    
    protected void initZipOutputStream(ZipOutputStream zOut)
        throws IOException, BuildException { 
        
        zipDir(null, zOut, "META-INF/");
        zipFile(new ByteArrayInputStream(bytes),
                zOut, 
                "META-INF/tasklib.xml", 
                System.currentTimeMillis());
        super.initZipOutputStream(zOut);
    }
}
