1 16 package org.apache.commons.jxpath.ri.model.beans; 17 18 import java.util.Locale ; 19 20 import org.apache.commons.jxpath.JXPathBeanInfo; 21 import org.apache.commons.jxpath.JXPathIntrospector; 22 import org.apache.commons.jxpath.ri.QName; 23 import org.apache.commons.jxpath.ri.model.NodePointer; 24 25 34 public class BeanPointer extends PropertyOwnerPointer { 35 private QName name; 36 private Object bean; 37 private JXPathBeanInfo beanInfo; 38 39 public BeanPointer( 40 QName name, 41 Object bean, 42 JXPathBeanInfo beanInfo, 43 Locale locale) 44 { 45 super(null, locale); 46 this.name = name; 47 this.bean = bean; 48 this.beanInfo = beanInfo; 49 } 50 51 54 public BeanPointer( 55 NodePointer parent, 56 QName name, 57 Object bean, 58 JXPathBeanInfo beanInfo) 59 { 60 super(parent); 61 this.name = name; 62 this.bean = bean; 63 this.beanInfo = beanInfo; 64 } 65 66 public PropertyPointer getPropertyPointer() { 67 return new BeanPropertyPointer(this, beanInfo); 68 } 69 70 public QName getName() { 71 return name; 72 } 73 74 77 public Object getBaseValue() { 78 return bean; 79 } 80 81 84 public boolean isCollection() { 85 return false; 86 } 87 88 91 public int getLength() { 92 return 1; 93 } 94 95 public boolean isLeaf() { 96 Object value = getNode(); 97 return value == null 98 || JXPathIntrospector.getBeanInfo(value.getClass()).isAtomic(); 99 } 100 101 public int hashCode() { 102 return name == null ? 0 : name.hashCode(); 103 } 104 105 public boolean equals(Object object) { 106 if (object == this) { 107 return true; 108 } 109 110 if (!(object instanceof BeanPointer)) { 111 return false; 112 } 113 114 BeanPointer other = (BeanPointer) object; 115 if (parent != other.parent) { 116 if (parent == null || !parent.equals(other.parent)) { 117 return false; 118 } 119 } 120 121 if ((name == null && other.name != null) 122 || (name != null && !name.equals(other.name))) { 123 return false; 124 } 125 126 int iThis = (index == WHOLE_COLLECTION ? 0 : index); 127 int iOther = (other.index == WHOLE_COLLECTION ? 0 : other.index); 128 if (iThis != iOther) { 129 return false; 130 } 131 132 if (bean instanceof Number 133 || bean instanceof String 134 || bean instanceof Boolean ) { 135 return bean.equals(other.bean); 136 } 137 return bean == other.bean; 138 } 139 140 146 public String asPath() { 147 if (parent != null) { 148 return super.asPath(); 149 } 150 else if (bean == null) { 151 return "null()"; 152 } 153 else if (bean instanceof Number ) { 154 String string = bean.toString(); 155 if (string.endsWith(".0")) { 156 string = string.substring(0, string.length() - 2); 157 } 158 return string; 159 } 160 else if (bean instanceof Boolean ) { 161 return ((Boolean ) bean).booleanValue() ? "true()" : "false()"; 162 } 163 else if (bean instanceof String ) { 164 return "'" + bean + "'"; 165 } 166 return "/"; 167 } 168 } | Popular Tags |