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 org.dom4j.Attribute; import org.dom4j.Node; import org.dom4j.tree.DefaultElement; import java.util.List; /** * Navigation Bean. * * @author Ben Walding * @version $Id$ */ public class NavBean { //~ Instance fields ---------------------------------------------------------- /** The current document. */ private Node document; /** The current location. */ private String location; //~ Methods ------------------------------------------------------------------ /** * Determines if the given node is collapsed. If a parent of this node is * collapsed, this will give spurious results. * * @param o the node to test * * @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; } /** * Setter. * * @param o The new document. */ public void setDocument(Object o) { document = (Node) o; } /** * Find the first item with a given href * * @param href the href to find. * * @return DefaultElement */ public DefaultElement getFirstNodeByHREF(String href) { String xpath = "//item[@href='" + href + "']"; return (DefaultElement) document.selectSingleNode(xpath); } /** * Sets the location. * * @param newLocation The location to set */ public void setLocation(String newLocation) { if (!newLocation.startsWith("/")) { newLocation = "/" + newLocation; } this.location = newLocation; } /** * Determines if a given item node is the selected node * * @param o the item to test * * @return true if the item is currently selected. */ public boolean isSelected(Object o) { if (location == null) { return false; } DefaultElement elem = (DefaultElement) o; if (location.equals(getHREFAttribute(elem))) { return true; } return false; } /** * Return the attribute's value for an element. * * @param elem The element to read * @param attribute The attribute's name to get. * @param defaultValue A default value to return. * * @return The attribute's value for the given element. */ private static String getAttribute( DefaultElement elem, String attribute, String defaultValue) { Attribute attr = elem.attribute(attribute); if (attr == null) { return defaultValue; } return attr.getStringValue(); } /** * Test if the element is collapsed. * * @param elem The element to test * * @return true if the element has a collapse attribute with the value true. */ private static boolean getCollapseAttribute(DefaultElement elem) { return Boolean.valueOf(getAttribute( elem, "collapse", "false")).booleanValue(); } /** * Return href attribute. * * @param elem element. * * @return href value. */ private static String getHREFAttribute(DefaultElement elem) { return getAttribute( elem, "href", null); } }