View Javadoc

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