package org.apache.maven.tasklist;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 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 acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" and
 *    "Apache Maven" 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",
 *    "Apache Maven", nor may "Apache" appear in their name, without
 *    prior written permission of the Apache Software Foundation.
 *
 * 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/>.
 *
 * ====================================================================
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * This class represents a list of {@link TaskListEntry} objects.  The
 * task list is used to represent the tasks that have been parsed from
 * JavaDoc task tags in a source tree.
 *
 * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
 * @version $Id: TaskList.java,v 1.2 2002/06/06 16:54:28 dion Exp $
 */
class TaskList
{
    /** The encoding used when creating the XML representation. */
    static final String ENCODING = "ISO-8859-1";

    /**
     * Output encoding.
     */
    private String outputEncoding = "ISO-8859-1";

    /** The list of TaskListEntries accumulated. */
    private List taskListEntries = new ArrayList();

    /**
     * Default constructor.
     */
    public TaskList()
    {
    }

    /**
     * Gets the list of entries representing the entire task list.  Each
     * entry represents all of the tasks from a given file.
     *
     * @return A List of TaskListEntries.
     */
    public List getTaskListEntries()
    {
        return taskListEntries;
    }

    /**
     * Set Output encoding.
     * @param outputEncoding the Charset Encoding that the object is using
     */
    public void setOutputEncoding(String outputEncoding)
    {
        this.outputEncoding = outputEncoding;
    }        

    /**
     * Get Output encoding.
     * @return the Charset Encoding that the object is using
     */
    public String getOutputEncoding()
    {
        return outputEncoding;
    }

    /**
     * Add a TaskListEntry to the list of entries.
     *
     * @param taskListEntry The TaskListEntry to be added.
     */
    public void addTaskListEntry(TaskListEntry taskListEntry)
    {
        taskListEntries.add(taskListEntry);
    }

    /**
     * Creates a String representation of the TaskList.
     *
     * @return The String representation of the TaskList.
     */
    public String toString()
    {
        return taskListEntries.toString();
    }

    /**
     * Creates an XML String representation of the TaskList.  The TaskList
     * XML representation is a valid <tt>xdoc</tt>.  The first section of
     * the xdoc contains a top-level <tt>&lt;taskList&gt;</tt> element.
     * Each {@link TaskListEntry} then renders itself (please see JavaDoc
     * for details).
     *
     * @return The XML String representation of the TaskList.
     */
    public String toXML()
    {
        StringBuffer sb = new StringBuffer();

        sb.append("<?xml version=\"1.0\" encoding=\"");
        sb.append(outputEncoding);
        sb.append("\" ?>\n");
        sb.append("<document>\n");
        sb.append("<properties>\n");
        sb.append("  <title>Task List</title>\n");
        sb.append("</properties>\n");
        sb.append("<body>\n");
        sb.append("  <section name=\"Task List\">\n");
        sb.append("    <taskList>\n");

        // Let each entry create its own XML representation.
        for (Iterator i = taskListEntries.iterator(); i.hasNext();)
        {
            TaskListEntry taskListEntry = (TaskListEntry) i.next();
            sb.append(taskListEntry.toXML());
        }

        sb.append("    </taskList>\n");
        sb.append("  </section>\n");
        sb.append("</body>\n");
        sb.append("</document>\n");

        return sb.toString();
    }
}
