View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jxpath.ri.model.jdom;
17  
18  import org.apache.commons.jxpath.ri.QName;
19  import org.apache.commons.jxpath.ri.model.NodePointer;
20  import org.apache.commons.jxpath.util.TypeUtils;
21  import org.jdom.Attribute;
22  
23  /***
24   * A Pointer that points to a DOM node.
25   *
26   * @author Dmitri Plotnikov
27   * @version $Revision: 1.10 $ $Date: 2004/04/01 02:55:31 $
28   */
29  public class JDOMAttributePointer extends NodePointer {
30      private Attribute attr;
31  
32      public JDOMAttributePointer(NodePointer parent, Attribute attr) {
33          super(parent);
34          this.attr = attr;
35      }
36  
37      public QName getName() {
38          return new QName(
39              JDOMNodePointer.getPrefix(attr),
40              JDOMNodePointer.getLocalName(attr));
41      }
42  
43      public String getNamespaceURI() {
44          String uri = attr.getNamespaceURI();
45          if (uri != null && uri.equals("")) {
46              uri = null;
47          }
48          return uri;
49      }
50  
51      public Object getValue() {
52          return attr.getValue();
53      }    
54      
55      public Object getBaseValue() {
56          return attr;
57      }
58      
59      public boolean isCollection() {
60          return false;
61      }
62  
63      public int getLength() {
64          return 1;
65      }    
66  
67      public Object getImmediateNode() {
68          return attr;
69      }
70  
71      public boolean isActual() {
72          return true;
73      }
74  
75      public boolean isLeaf() {
76          return true;
77      }
78  
79      /***
80       * Sets the value of this attribute.
81       */
82      public void setValue(Object value) {
83          attr.setValue((String) TypeUtils.convert(value, String.class));
84      }
85  
86      public void remove() {
87          attr.getParent().removeAttribute(attr);
88      }
89  
90      /***
91       */
92      public String asPath() {
93          StringBuffer buffer = new StringBuffer();
94          if (parent != null) {
95              buffer.append(parent.asPath());
96              if (buffer.length() == 0
97                  || buffer.charAt(buffer.length() - 1) != '/') {
98                  buffer.append('/');
99              }
100         }
101         buffer.append('@');
102         buffer.append(getName());
103         return buffer.toString();
104     }
105 
106     public int hashCode() {
107         return System.identityHashCode(attr);
108     }
109 
110     public boolean equals(Object object) {
111         if (object == this) {
112             return true;
113         }
114 
115         if (!(object instanceof JDOMAttributePointer)) {
116             return false;
117         }
118 
119         JDOMAttributePointer other = (JDOMAttributePointer) object;
120         return attr == other.attr;
121     }
122 
123     public int compareChildNodePointers(
124             NodePointer pointer1,
125             NodePointer pointer2) 
126     {
127         // Won't happen - attributes don't have children
128         return 0;
129     }
130 }