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.Document;
import org.dom4j.io.SAXReader;
import org.jaxen.XPath;
import org.jaxen.dom4j.Dom4jXPath;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * This is a simple POM manipulator that doesn't take into account any sort of
 * POM inheritance. When the interpolation and inheritance is cleaned up in the
 * Maven we will be able to serialize a POM directly with greater ease. But we
 * may stick with this Jaxen solution anyway because it's so easy. Just turn the
 * POM in memory into a Dom4j document and perform the same operation as we are
 * here.
 */
public class PomNodeSelector
{
    /**
     * POM document
     */
    private File pom;

    /**
     * Xpath expression.
     */
    private String xpathExpression;

    /**
     * Snapshot dependencies found.
     */
    private List nodes;

    /**
     * Constructor.
     */
    public PomNodeSelector()
    {
        nodes = new ArrayList();
    }

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

    public void setXpathExpression( String xpathExpression )
    {
        this.xpathExpression = xpathExpression;
    }

    public String getXpathExpression()
    {
        return xpathExpression;
    }

    /**
     * Sets the nodes attribute of the PomManipulator object
     */
    public void setNodes( List nodes )
    {
        this.nodes = nodes;
    }

    /**
     * Gets the nodes attribute of the PomManipulator object
     */
    public List getNodes()
    {
        return nodes;
    }

    public void setPom( File pom )
    {
        this.pom = pom;
    }

    public File getPom()
    {
        return pom;
    }

    /**
     * Update the snapshot version identifiers with actual timestamp versions
     * and write out the POM in its updated form.
     *
     * @throws Exception
     */
    public void execute()
        throws Exception
    {
        SAXReader reader = new SAXReader();
        Document doc = reader.read( getPom() );

        // The selecting nodes with the xpath expression will give us a list
        // of dependencies elements where the version element is equal to 'SNAPSHOT'.
        // So we can get any information we need, and alter anything we need to before writing
        // the dom4j document back out.
        XPath xpath = new Dom4jXPath( getXpathExpression() );
        setNodes( xpath.selectNodes( doc ) );
    }
}
