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.Node;

/**
 *
 *
 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
 *
 * @version $Id$
 */
public class SnapshotVersionTransformer
    extends AbstractPomTransformer
{
    // -------------------------------------------------------------------------
    // Accessors
    // -------------------------------------------------------------------------

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

    public String selectNodeXPath()
    {
        return "currentVersion";
    }

    public String getNodeContent( Node node )
        throws Exception
    {
        String currentVersion = node.selectSingleNode( selectNodeXPath() ).getText();

        int i = currentVersion.indexOf( "-SNAPSHOT" );
        if ( i > 0 )
        {
            currentVersion = currentVersion.substring( 0, i );
        }

        return currentVersion;
    }

    public void transformNode( Node node )
        throws Exception
    {
        Node currentVersion = node.selectSingleNode( selectNodeXPath() );
        currentVersion.setText( getNodeContent( node ) );
    }

    public Node getTransformedNode( Node node )
        throws Exception
    {
        Node transformedNode = (Node) node.clone();
        Node currentVersion = transformedNode.selectSingleNode( selectNodeXPath() );
        currentVersion.setText( getNodeContent( transformedNode ) );

        return transformedNode;
    }
}
