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 java.util.Locale;
20  
21  import org.apache.commons.jxpath.JXPathContext;
22  import org.apache.commons.jxpath.ri.QName;
23  import org.apache.commons.jxpath.ri.model.NodePointer;
24  
25  /**
26   * Pointer whose value is <code>null</code>.
27   * @author Dmitri Plotnikov
28   * @version $Revision: 652915 $ $Date: 2008-05-02 17:12:57 -0400 (Fri, 02 May 2008) $
29   */
30  public class NullPointer extends PropertyOwnerPointer {
31      private QName name;
32      private String id;
33  
34      private static final long serialVersionUID = 2193425983220679887L;
35  
36      /**
37       * Create a new NullPointer.
38       * @param name node name
39       * @param locale Locale
40       */
41      public NullPointer(QName name, Locale locale) {
42          super(null, locale);
43          this.name = name;
44      }
45  
46      /**
47       * Used for the root node.
48       * @param parent parent pointer
49       * @param name node name
50       */
51      public NullPointer(NodePointer parent, QName name) {
52          super(parent);
53          this.name = name;
54      }
55  
56      /**
57       * Create a new NullPointer.
58       * @param locale Locale
59       * @param id String
60       */
61      public NullPointer(Locale locale, String id) {
62          super(null, locale);
63          this.id = id;
64      }
65  
66      public QName getName() {
67          return name;
68      }
69  
70      public Object getBaseValue() {
71          return null;
72      }
73  
74      public boolean isCollection() {
75          return false;
76      }
77  
78      public boolean isLeaf() {
79          return true;
80      }
81  
82      public boolean isActual() {
83          return false;
84      }
85  
86      public PropertyPointer getPropertyPointer() {
87          return new NullPropertyPointer(this);
88      }
89  
90      public NodePointer createPath(JXPathContext context, Object value) {
91          if (parent != null) {
92              return parent.createPath(context, value).getValuePointer();
93          }
94          throw new UnsupportedOperationException(
95              "Cannot create the root object: " + asPath());
96      }
97  
98      public NodePointer createPath(JXPathContext context) {
99          if (parent != null) {
100             return parent.createPath(context).getValuePointer();
101         }
102         throw new UnsupportedOperationException(
103             "Cannot create the root object: " + asPath());
104     }
105 
106     public NodePointer createChild(
107         JXPathContext context,
108         QName name,
109         int index) {
110         return createPath(context).createChild(context, name, index);
111     }
112 
113     public NodePointer createChild(
114         JXPathContext context,
115         QName name,
116         int index,
117         Object value) {
118         return createPath(context).createChild(context, name, index, value);
119     }
120 
121     public int hashCode() {
122         return name == null ? 0 : name.hashCode();
123     }
124 
125     public boolean equals(Object object) {
126         if (object == this) {
127             return true;
128         }
129 
130         if (!(object instanceof NullPointer)) {
131             return false;
132         }
133 
134         NullPointer other = (NullPointer) object;
135         return name == other.name || name != null && name.equals(other.name);
136     }
137 
138     public String asPath() {
139         if (id != null) {
140             return "id(" + id + ")";
141         }
142         return parent == null ? "null()" : super.asPath();
143     }
144 
145     public int getLength() {
146         return 0;
147     }
148 }