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.beans;
18  
19  import org.apache.commons.jxpath.JXPathContext;
20  import org.apache.commons.jxpath.ri.QName;
21  import org.apache.commons.jxpath.ri.model.NodePointer;
22  
23  /**
24   * Used when there is a need to construct a Pointer for a collection element
25   * that does not exist.  For example, if the path is "foo[3]", but the
26   * collection "foo" only has one element or is empty or is null, the
27   * NullElementPointer can be used to capture this situation without putting a
28   * regular NodePointer into an invalid state.  Just create a NullElementPointer
29   * with index 2 (= 3 - 1) and a "foo" pointer as the parent.
30   *
31   * @author Dmitri Plotnikov
32   * @version $Revision: 652884 $ $Date: 2008-05-02 16:02:00 -0400 (Fri, 02 May 2008) $
33   */
34  public class NullElementPointer extends CollectionPointer {
35  
36      private static final long serialVersionUID = 8714236818791036721L;
37  
38      /**
39       * Create a new NullElementPointer.
40       * @param parent parent pointer
41       * @param index int
42       */
43      public NullElementPointer(NodePointer parent, int index) {
44          super(parent, (Object) null);
45          this.index = index;
46      }
47  
48      public QName getName() {
49          return null;
50      }
51  
52      public Object getBaseValue() {
53          return null;
54      }
55  
56      public Object getImmediateNode() {
57          return null;
58      }
59  
60      public boolean isLeaf() {
61          return true;
62      }
63  
64      public boolean isCollection() {
65          return false;
66      }
67  
68      /**
69       * Get the property pointer for this.
70       * @return PropertyPointer
71       */
72      public PropertyPointer getPropertyPointer() {
73          return new NullPropertyPointer(this);
74      }
75  
76      public NodePointer getValuePointer() {
77          return new NullPointer(this, getName());
78      }
79  
80      public void setValue(Object value) {
81          throw new UnsupportedOperationException(
82              "Collection element does not exist: " + this);
83      }
84  
85      public boolean isActual() {
86          return false;
87      }
88  
89      public boolean isContainer() {
90          return true;
91      }
92  
93      public NodePointer createPath(JXPathContext context) {
94          return parent.createChild(context, null, index);
95      }
96  
97      public NodePointer createPath(JXPathContext context, Object value) {
98          return parent.createChild(context, null, index, value);
99      }
100 
101     public int hashCode() {
102         return getImmediateParentPointer().hashCode() + index;
103     }
104 
105     public boolean equals(Object object) {
106         if (object == this) {
107             return true;
108         }
109 
110         if (!(object instanceof NullElementPointer)) {
111             return false;
112         }
113 
114         NullElementPointer other = (NullElementPointer) object;
115         return getImmediateParentPointer() == other.getImmediateParentPointer()
116                 && index == other.index;
117     }
118 
119     public int getLength() {
120         return 0;
121     }
122 
123     public String asPath() {
124         StringBuffer buffer = new StringBuffer();
125         NodePointer parent = getImmediateParentPointer();
126         if (parent != null) {
127             buffer.append(parent.asPath());
128         }
129         if (index != WHOLE_COLLECTION) {
130             // Address the list[1][2] case
131             if (parent != null && parent.getIndex() != WHOLE_COLLECTION) {
132                 buffer.append("/.");
133             }
134             else if (parent != null
135                     && parent.getImmediateParentPointer() != null
136                     && parent.getImmediateParentPointer().getIndex() != WHOLE_COLLECTION) {
137                 buffer.append("/.");
138             }
139             buffer.append("[").append(index + 1).append(']');
140         }
141 
142         return buffer.toString();
143     }
144 }