package org.apache.maven;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 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.io.FileWriter;
import java.io.File;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;

/**
 * A little XML document formatter. Used to format a POM in XML format
 * after it has been transformed.
 *
 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 * @author <a href="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
 * @version $Id: XmlPomFormatter.java,v 1.1 2003/01/24 03:44:33 jvanzyl Exp $
 */
public class XmlPomFormatter
{
    /** Input xml */
    private File input;
    
    /** Output xml */
    private File output;
    
    /**
     * Set input file.
     * @param input the {@link File} to read the unformatted project object
     *      model from
     */
    public void setInput(File input)
    {
        this.input = input;
    }        
    
    /**
     * Get input file.
     * @return the file the unformatted project object model will read from
     */
    public File getInput()
    {
        return input;
    }

    /**
     * Set output file.
     * @param output the {@link File} to write the formatted project object
     *      object model to
     */
    public void setOutput(File output)
    {
        this.output = output;
    }        
    
    /**
     * Get output file.
     * @return the file the formatted project object model will be written to
     */
    public File getOutput()
    {
        return output;
    }


    /**
     * Format the project descriptor.
     * @throws Exception when any error occurs 
     */
    public void execute() throws Exception
    {
        Document document = createDocument();
        OutputFormat format = new OutputFormat();
        format.setIndentSize(2);
        format.setNewlines(true);
        format.setTrimText(true);

        FileWriter out = new FileWriter(output);
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        out.close();
    }

    /**
     * Description of the Method
     * @return a {@link Document} from parsing the {@link #input input file}
     * @throws Exception when any error occurs 
     */
    protected Document createDocument() throws Exception
    {
        SAXReader reader = new SAXReader();
        return reader.read(input);
    }

    /** Left over testing method - to be removed
     * @param args command line arguments
     * @throws Exception when any error occurs 
     */
    public static void main(String[] args) throws Exception
    {
        System.out.println(args[0]);
        System.out.println(args[1]);
        
        XmlPomFormatter formatter = new XmlPomFormatter();
        formatter.setInput(new File(args[0]));
        formatter.setOutput(new File(args[1]));
        formatter.execute();
    }
}
