001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath.ri.model.beans;
018
019import java.util.Locale;
020
021import org.apache.commons.jxpath.JXPathContext;
022import org.apache.commons.jxpath.ri.QName;
023import org.apache.commons.jxpath.ri.model.NodePointer;
024
025/**
026 * Pointer whose value is <code>null</code>.
027 * @author Dmitri Plotnikov
028 * @version $Revision: 652915 $ $Date: 2008-05-02 23:12:57 +0200 (Fr, 02 Mai 2008) $
029 */
030public class NullPointer extends PropertyOwnerPointer {
031    private QName name;
032    private String id;
033
034    private static final long serialVersionUID = 2193425983220679887L;
035
036    /**
037     * Create a new NullPointer.
038     * @param name node name
039     * @param locale Locale
040     */
041    public NullPointer(QName name, Locale locale) {
042        super(null, locale);
043        this.name = name;
044    }
045
046    /**
047     * Used for the root node.
048     * @param parent parent pointer
049     * @param name node name
050     */
051    public NullPointer(NodePointer parent, QName name) {
052        super(parent);
053        this.name = name;
054    }
055
056    /**
057     * Create a new NullPointer.
058     * @param locale Locale
059     * @param id String
060     */
061    public NullPointer(Locale locale, String id) {
062        super(null, locale);
063        this.id = id;
064    }
065
066    public QName getName() {
067        return name;
068    }
069
070    public Object getBaseValue() {
071        return null;
072    }
073
074    public boolean isCollection() {
075        return false;
076    }
077
078    public boolean isLeaf() {
079        return true;
080    }
081
082    public boolean isActual() {
083        return false;
084    }
085
086    public PropertyPointer getPropertyPointer() {
087        return new NullPropertyPointer(this);
088    }
089
090    public NodePointer createPath(JXPathContext context, Object value) {
091        if (parent != null) {
092            return parent.createPath(context, value).getValuePointer();
093        }
094        throw new UnsupportedOperationException(
095            "Cannot create the root object: " + asPath());
096    }
097
098    public NodePointer createPath(JXPathContext context) {
099        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}