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.dom;
18  
19  import org.apache.commons.jxpath.ri.Compiler;
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.compiler.NodeTest;
22  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
23  import org.apache.commons.jxpath.ri.model.NodePointer;
24  import org.apache.commons.jxpath.util.TypeUtils;
25  import org.w3c.dom.Attr;
26  
27  /**
28   * A Pointer that points to a DOM node. Because the underlying DOM Attr is not Serializable,
29   * neither is this pointer class truly so.
30   *
31   * @author Dmitri Plotnikov
32   * @version $Revision: 670727 $ $Date: 2008-06-23 16:10:38 -0400 (Mon, 23 Jun 2008) $
33   */
34  public class DOMAttributePointer extends NodePointer {
35      private static final long serialVersionUID = 1115085175427555951L;
36  
37      private Attr attr;
38  
39      /**
40       * Create a new DOMAttributePointer.
41       * @param parent pointer
42       * @param attr pointed
43       */
44      public DOMAttributePointer(NodePointer parent, Attr attr) {
45          super(parent);
46          this.attr = attr;
47      }
48  
49      public QName getName() {
50          return new QName(
51              DOMNodePointer.getPrefix(attr),
52              DOMNodePointer.getLocalName(attr));
53      }
54  
55      public String getNamespaceURI() {
56          String prefix = DOMNodePointer.getPrefix(attr);
57          return prefix == null ? null : parent.getNamespaceURI(prefix);
58      }
59  
60      public Object getValue() {
61          String value = attr.getValue();
62          if (value == null || (value.equals("") && !attr.getSpecified())) {
63              return null;
64          }
65          return value;
66      }
67  
68      public Object getBaseValue() {
69          return attr;
70      }
71  
72      public boolean isCollection() {
73          return false;
74      }
75  
76      public int getLength() {
77          return 1;
78      }
79  
80      public Object getImmediateNode() {
81          return attr;
82      }
83  
84      public boolean isActual() {
85          return true;
86      }
87  
88      public boolean isLeaf() {
89          return true;
90      }
91  
92      public boolean testNode(NodeTest nodeTest) {
93          return nodeTest == null
94              || ((nodeTest instanceof NodeTypeTest)
95                  && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE);
96      }
97  
98      /**
99       * Sets the value of this attribute.
100      * @param value to set
101      */
102     public void setValue(Object value) {
103         attr.setValue((String) TypeUtils.convert(value, String.class));
104     }
105 
106     public void remove() {
107         attr.getOwnerElement().removeAttributeNode(attr);
108     }
109 
110     public String asPath() {
111         StringBuffer buffer = new StringBuffer();
112         if (parent != null) {
113             buffer.append(parent.asPath());
114             if (buffer.length() == 0
115                 || buffer.charAt(buffer.length() - 1) != '/') {
116                 buffer.append('/');
117             }
118         }
119         buffer.append('@');
120         buffer.append(getName());
121         return buffer.toString();
122     }
123 
124     public int hashCode() {
125         return System.identityHashCode(attr);
126     }
127 
128     public boolean equals(Object object) {
129         return object == this || object instanceof DOMAttributePointer
130                 && attr == ((DOMAttributePointer) object).attr;
131     }
132 
133     public int compareChildNodePointers(NodePointer pointer1,
134             NodePointer pointer2) {
135         // Won't happen - attributes don't have children
136         return 0;
137     }
138 }