1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.jxpath.ri.model.beans;
  17. import java.util.Locale;
  18. import org.apache.commons.jxpath.JXPathContext;
  19. import org.apache.commons.jxpath.ri.QName;
  20. import org.apache.commons.jxpath.ri.model.NodePointer;
  21. /**
  22. * @author Dmitri Plotnikov
  23. * @version $Revision: 1.11 $ $Date: 2004/02/29 14:17:41 $
  24. */
  25. public class NullPointer extends PropertyOwnerPointer {
  26. private QName name;
  27. private String id;
  28. public NullPointer(QName name, Locale locale) {
  29. super(null, locale);
  30. this.name = name;
  31. }
  32. /**
  33. * Used for the root node
  34. */
  35. public NullPointer(NodePointer parent, QName name) {
  36. super(parent);
  37. this.name = name;
  38. }
  39. public NullPointer(Locale locale, String id) {
  40. super(null, locale);
  41. this.id = id;
  42. }
  43. public QName getName() {
  44. return name;
  45. }
  46. public Object getBaseValue() {
  47. return null;
  48. }
  49. public boolean isCollection() {
  50. return false;
  51. }
  52. public boolean isLeaf() {
  53. return true;
  54. }
  55. public boolean isActual() {
  56. return false;
  57. }
  58. public PropertyPointer getPropertyPointer() {
  59. return new NullPropertyPointer(this);
  60. }
  61. public NodePointer createPath(JXPathContext context, Object value) {
  62. if (parent != null) {
  63. return parent.createPath(context, value).getValuePointer();
  64. }
  65. else {
  66. throw new UnsupportedOperationException(
  67. "Cannot create the root object: " + asPath());
  68. }
  69. }
  70. public NodePointer createPath(JXPathContext context) {
  71. if (parent != null) {
  72. return parent.createPath(context).getValuePointer();
  73. }
  74. else {
  75. throw new UnsupportedOperationException(
  76. "Cannot create the root object: " + asPath());
  77. }
  78. }
  79. public NodePointer createChild(
  80. JXPathContext context,
  81. QName name,
  82. int index)
  83. {
  84. return createPath(context).createChild(context, name, index);
  85. }
  86. public NodePointer createChild(
  87. JXPathContext context,
  88. QName name,
  89. int index,
  90. Object value)
  91. {
  92. return createPath(context).createChild(context, name, index, value);
  93. }
  94. public int hashCode() {
  95. return name == null ? 0 : name.hashCode();
  96. }
  97. public boolean equals(Object object) {
  98. if (object == this) {
  99. return true;
  100. }
  101. if (!(object instanceof NullPointer)) {
  102. return false;
  103. }
  104. NullPointer other = (NullPointer) object;
  105. return (name == null && other.name == null)
  106. || (name != null && name.equals(other.name));
  107. }
  108. public String asPath() {
  109. if (id != null) {
  110. return "id(" + id + ")";
  111. }
  112. if (parent != null) {
  113. return super.asPath();
  114. }
  115. return "null()";
  116. }
  117. public int getLength() {
  118. return 0;
  119. }
  120. }