View Javadoc

1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  import org.dom4j.Attribute;
21  import org.dom4j.Node;
22  
23  import org.dom4j.tree.DefaultElement;
24  
25  import java.util.List;
26  
27  
28  /**
29   * Navigation Bean.
30   *
31   * @author <a href="mailto:bwalding@jakarta.org">Ben Walding</a>
32   * @version $Id: NavBean.java 532339 2007-04-25 12:28:56Z ltheussl $
33   */
34  public class NavBean
35  {
36    //~ Instance fields ----------------------------------------------------------
37  
38    /** The current document. */
39    private Node document;
40  
41    /** The current location. */
42    private String location;
43  
44    //~ Methods ------------------------------------------------------------------
45  
46    /**
47     * Determines if the given node is collapsed.  If a parent of this node is
48     * collapsed, this will give spurious results.
49     *
50     * @param o the node to test
51     *
52     * @return boolean
53     */
54    public boolean isCollapsed(Object o)
55    {
56      //If we don't know where we are, collapse it - something outside of our knowledge
57      if (location == null)
58      {
59        return true;
60      }
61  
62      if (!(o instanceof DefaultElement))
63      {
64        System.out.println(o.getClass().getName());
65  
66        return false;
67      }
68  
69      DefaultElement elem = (DefaultElement) o;
70  
71      boolean        collapsed = getCollapseAttribute(elem);
72  
73      if (!collapsed)
74      {
75        return false;
76      }
77  
78      if (isSelected(o))
79      {
80        return false;
81      }
82  
83      //System.out.println(elem.asXML());
84      String xpath;
85  
86      if (location.startsWith("/") || location.startsWith("."))
87      {
88        xpath = ".//item[@href='" + location + "']";
89      }
90      else
91      {
92        xpath = ".//item[@href='/" + location + "']";
93      }
94  
95      List l = elem.selectNodes(xpath);
96  
97      if (l.size() != 0)
98      {
99        return false;
100     }
101 
102     return true;
103   }
104 
105   /**
106    * Setter.
107    *
108    * @param o The new document.
109    */
110   public void setDocument(Object o)
111   {
112     document = (Node) o;
113   }
114 
115   /**
116    * Find the first <code>item</code> with a given href
117    *
118    * @param href the href to find.
119    *
120    * @return DefaultElement
121    */
122   public DefaultElement getFirstNodeByHREF(String href)
123   {
124     String xpath = "//item[@href='" + href + "']";
125 
126     return (DefaultElement) document.selectSingleNode(xpath);
127   }
128 
129   /**
130    * Sets the location.
131    *
132    * @param newLocation The location to set
133    */
134   public void setLocation(String newLocation)
135   {
136     if (!newLocation.startsWith("/"))
137     {
138       newLocation = "/" + newLocation;
139     }
140 
141     this.location = newLocation;
142   }
143 
144   /**
145    * Determines if a given <code>item</code> node is the selected node
146    *
147    * @param o the item to test
148    *
149    * @return true if the item is currently selected.
150    */
151   public boolean isSelected(Object o)
152   {
153     if (location == null)
154     {
155       return false;
156     }
157 
158     DefaultElement elem = (DefaultElement) o;
159 
160     if (location.equals(getHREFAttribute(elem)))
161     {
162       return true;
163     }
164 
165     return false;
166   }
167 
168   /**
169    * Return the attribute's value for an element.
170    *
171    * @param elem The element to read
172    * @param attribute The attribute's name to get.
173    * @param defaultValue A default value to return.
174    *
175    * @return The attribute's value for the given element.
176    */
177   private static String getAttribute(
178     DefaultElement elem,
179     String         attribute,
180     String         defaultValue)
181   {
182     Attribute attr = elem.attribute(attribute);
183 
184     if (attr == null)
185     {
186       return defaultValue;
187     }
188 
189     return attr.getStringValue();
190   }
191 
192   /**
193    * Test if the element is collapsed.
194    *
195    * @param elem The element to test
196    *
197    * @return true if the element has a collapse attribute with the value true.
198    */
199   private static boolean getCollapseAttribute(DefaultElement elem)
200   {
201     return Boolean.valueOf(getAttribute(
202         elem,
203         "collapse",
204         "false")).booleanValue();
205   }
206 
207   /**
208    * Return href attribute.
209    *
210    * @param elem element.
211    *
212    * @return href value.
213    */
214   private static String getHREFAttribute(DefaultElement elem)
215   {
216     return getAttribute(
217       elem,
218       "href",
219       null);
220   }
221 }