package org.apache.maven.release;

/* ====================================================================
 *   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 org.dom4j.Element;
import org.dom4j.Node;

/**
 *
 *
 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 *
 * @version $Id$
 */
public class VersionTransformer
    extends AbstractPomTransformer
{
    private String version;
    private String tag;

    public VersionTransformer( String version, String tag )
    {
        this.version = version;
        this.tag = tag;
    }

    // -------------------------------------------------------------------------
    // Accessors
    // -------------------------------------------------------------------------

    public String selectNodesXPathExpression()
    {
        return "/project";
    }

    public void transformNode( Node node )
        throws Exception
    {
        Element project = ( Element ) node;

        Node currentVersion = node.selectSingleNode( "currentVersion" );
        if ( currentVersion != null )
        {
            currentVersion.setText( version );
        }
        else
        {
            project.addElement( "currentVersion" ).addText( version );
        }

        Element version = ( Element ) node.selectSingleNode( "versions/version[tag='" + tag + "']" );
        if ( version != null )
        {
            // tag exists - overwrite
            Node id = version.selectSingleNode( "id" );
            if ( id != null )
            {
                id.setText( this.version );
            }
            else
            {
                version.addElement( "id" ).addText( this.version );
            }
            Node name = version.selectSingleNode( "name" );
            if ( name != null )
            {
                name.setText( this.version );
            }
            else
            {
                version.addElement( "name" ).addText( this.version );
            }
        }
        else
        {
            // tag doesn't exist - add
            Element versions = ( Element ) project.selectSingleNode( "versions" );
            if ( versions == null )
            {
                versions = project.addElement( "versions" );
            }
            Element v = versions.addElement( "version" );
            v.addElement( "id" ).addText( this.version );
            v.addElement( "name" ).addText( this.version );
            v.addElement( "tag" ).addText( tag );
        }
    }

    public Node getTransformedNode( Node node )
        throws Exception
    {
        throw new UnsupportedOperationException( "getTransformedNode not implemented" );
    }

    public boolean transformRequired()
    {
        Node node = ( Node ) getSelectedNodes().get( 0 );
        if ( node == null )
        {
            return true;
        }

        Node currentVersion = node.selectSingleNode( "currentVersion" );
        if ( currentVersion == null || !currentVersion.getText().equals( version ))
        {
            return true;
        }

        
        Node v = node.selectSingleNode( "versions/version[tag='" + tag + "' and id='" + version + "' and name='" + version + "']" );
        return ( v == null );
    }
}

