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.util.List;

import org.dom4j.Attribute;
import org.dom4j.Node;
import org.dom4j.tree.DefaultElement;

/**
 * @author <a href="bwalding@jakarta.org">Ben Walding</a>
 * @version $Id: NavBean.java,v 1.7 2004/03/02 15:36:14 evenisse Exp $
 */
public class NavBean
{
    private String location;
    private Node document;

    public void setDocument(Object o)
    {
        document = (Node) o;
    }

    private static String getAttribute(DefaultElement elem, String attribute, String defaultValue)
    {
        Attribute attr = elem.attribute(attribute);
        if (attr == null)
        {
            return defaultValue;
        }

        return attr.getStringValue();
    }

    private static boolean getCollapseAttribute(DefaultElement elem)
    {
        return Boolean.valueOf(getAttribute(elem, "collapse", "false")).booleanValue();
    }
    private static String getHREFAttribute(DefaultElement elem)
    {
        return getAttribute(elem, "href", null);
    }
    
    /**
     * Determines if the given node is collapsed.  If a parent of this node is
     * collapsed, this will give spurious results.
     * @param o
     * @return boolean
     */
    public boolean isCollapsed(Object o)
    {
        //If we don't know where we are, collapse it - something outside of our knowledge
        if (location == null)
        {
            return true;
        }

        if (!(o instanceof DefaultElement))
        {
            System.out.println(o.getClass().getName());
            return false;
        }
        DefaultElement elem = (DefaultElement) o;

        boolean collapsed = getCollapseAttribute(elem);
        if (!collapsed) 
        {
            return false;
        }

        if (isSelected(o))
        {
            return false;
        }

        //System.out.println(elem.asXML());
        String xpath;
        if (location.startsWith("/")  || location.startsWith(".")) {
            xpath =    ".//item[@href='" + location + "']";

        } else {
            xpath =   ".//item[@href='/" + location + "']";

        }

        
        List l = elem.selectNodes(xpath);

        if (l.size() != 0)
        {
            return false;
        }

        return true;
    }

    /**
     * Determines if a given <code>item</code> node is the selected node
     * @param o
     * @return boolean
     */
    public boolean isSelected(Object o)
    {
        if (location == null)
        {
            return false;
        }

        DefaultElement elem = (DefaultElement) o;
        if (location.equals(getHREFAttribute(elem)))
        {
            return true;
        }

        return false;
    }

    /**
     * Sets the location.
     * @param location The location to set
     */
    public void setLocation(String location)
    {
        if (!location.startsWith("/"))
            location = "/" + location;
            
        this.location = location;
    }

    /**
     * Find the first <code>item</code> with a given href
     * @param href
     * @return DefaultElement
     */
    public DefaultElement getFirstNodeByHREF(String href)
    {
        String xpath = "//item[@href='" + href + "']";
        return (DefaultElement) document.selectSingleNode(xpath);
    }
}
