package org.apache.maven;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

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$
 */
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();
    }
}
