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