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.jdom;
19  
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.model.NodePointer;
22  import org.apache.commons.jxpath.util.TypeUtils;
23  import org.jdom.Attribute;
24  
25  /**
26   * A Pointer that points to a DOM node.
27   */
28  public class JDOMAttributePointer extends NodePointer {
29  
30      private static final long serialVersionUID = 8896050354479644028L;
31  
32      /** JDOM Attribute. */
33      private final Attribute attribute;
34  
35      /**
36       * Create a JDOMAttributePointer.
37       *
38       * @param parent NodePointer parent.
39       * @param attribute   JDOM Attribute.
40       */
41      public JDOMAttributePointer(final NodePointer parent, final Attribute attribute) {
42          super(parent);
43          this.attribute = attribute;
44      }
45  
46      @Override
47      public String asPath() {
48          final StringBuilder buffer = new StringBuilder();
49          if (parent != null) {
50              buffer.append(parent.asPath());
51              if (buffer.length() == 0 || buffer.charAt(buffer.length() - 1) != '/') {
52                  buffer.append('/');
53              }
54          }
55          buffer.append('@');
56          buffer.append(getName());
57          return buffer.toString();
58      }
59  
60      @Override
61      public int compareChildNodePointers(final NodePointer pointer1, final NodePointer pointer2) {
62          // Won't happen - attributes don't have children
63          return 0;
64      }
65  
66      @Override
67      public boolean equals(final Object object) {
68          return object == this || object instanceof JDOMAttributePointer && ((JDOMAttributePointer) object).attribute == attribute;
69      }
70  
71      @Override
72      public Object getBaseValue() {
73          return attribute;
74      }
75  
76      @Override
77      public Object getImmediateNode() {
78          return attribute;
79      }
80  
81      @Override
82      public int getLength() {
83          return 1;
84      }
85  
86      @Override
87      public QName getName() {
88          return new QName(JDOMNodePointer.getPrefix(attribute), JDOMNodePointer.getLocalName(attribute));
89      }
90  
91      @Override
92      public String getNamespaceURI() {
93          String uri = attribute.getNamespaceURI();
94          if (uri != null && uri.isEmpty()) {
95              uri = null;
96          }
97          return uri;
98      }
99  
100     @Override
101     public Object getValue() {
102         return attribute.getValue();
103     }
104 
105     @Override
106     public int hashCode() {
107         return System.identityHashCode(attribute);
108     }
109 
110     @Override
111     public boolean isActual() {
112         return true;
113     }
114 
115     @Override
116     public boolean isCollection() {
117         return false;
118     }
119 
120     @Override
121     public boolean isLeaf() {
122         return true;
123     }
124 
125     @Override
126     public void remove() {
127         attribute.getParent().removeAttribute(attribute);
128     }
129 
130     @Override
131     public void setValue(final Object value) {
132         attribute.setValue((String) TypeUtils.convert(value, String.class));
133     }
134 }