package org.apache.maven.changes;

/* ====================================================================
 *   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.*;
import java.text.*;
import java.util.*;

import org.apache.commons.io.FileUtils;
import org.dom4j.*;
import org.dom4j.io.*;

/**
 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 *
 * @version $Id$
 */
public class ReleaseVersion
{
    public static final void releaseVersion( File changesFile, String currentVersion, String version, String encoding)
        throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
    {
        DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        releaseVersion( changesFile, currentVersion, version, encoding, dateFormat.format( new Date() ) );
    }

    static final Document transformVersion( File changesFile, String currentVersion, String version, String date )
        throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
    {
        SAXReader r = new SAXReader();
        
        Document doc = r.read( changesFile );
        
        Element root = doc.getRootElement();
        
        Element releases = root.element( "body" );
        
        for ( Iterator i = releases.elementIterator( "release" ); i.hasNext(); )
        {
            Element e = ( Element ) i.next();
            
            Attribute v = e.attribute( "version" );
            
            if ( v != null )
            {
                String text = v.getText();
                Attribute d = e.attribute( "date" );
                boolean updateRelease = false;
                if ( text.equals( currentVersion ) || text.equals( version ) )
                {
                    updateRelease = true;
                }
                if ( updateRelease && d != null )
                {
                    DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
                    try
                    {
                        dateFormat.parse( d.getText() );
                        // date is set correctly
                        updateRelease = false;
                    }
                    catch ( ParseException ex )
                    {
                        // intentional
                    }
                }

                if ( updateRelease )
                {
                    v.setText( version );

                    if ( d == null )
                    {
                        e.addAttribute( "date", date );
                    }
                    else
                    {
                        d.setText( date );
                    }
                    return doc;
                }
            }
        }

        // indicates no transform necessary
        return null;
    }

    public static final void releaseVersion( File changesFile, String currentVersion, String version, String encoding, String date )
        throws DocumentException, FileNotFoundException, UnsupportedEncodingException, IOException
    {
        Document doc = transformVersion( changesFile, currentVersion, version, date );
        if ( doc == null )
        {
            // no transform necessary
            return;
        }

        // Backup the original first.
        FileUtils.copyFile( changesFile, new File( changesFile + ".backup" ) );

        OutputStream os = new FileOutputStream( changesFile );

        OutputFormat format = new OutputFormat();
        format.setIndentSize( 2 );
        format.setNewlines( true );
        format.setTrimText( true );
        format.setEncoding( encoding );

        XMLWriter writer = new XMLWriter( format );
        writer.setOutputStream( os );
        writer.write( doc );
        writer.flush();
        writer.close();
    }   
}

