1 package org.hibernate.property; 3 4 import java.lang.reflect.Method ; 5 import java.util.Map ; 6 7 import org.dom4j.Attribute; 8 import org.dom4j.Element; 9 import org.dom4j.Node; 10 import org.hibernate.HibernateException; 11 import org.hibernate.MappingException; 12 import org.hibernate.PropertyNotFoundException; 13 import org.hibernate.engine.SessionFactoryImplementor; 14 import org.hibernate.engine.SessionImplementor; 15 import org.hibernate.type.CollectionType; 16 import org.hibernate.type.Type; 17 18 24 public class Dom4jAccessor implements PropertyAccessor { 25 private String nodeName; 26 private Type propertyType; 27 private final SessionFactoryImplementor factory; 28 29 public Dom4jAccessor(String nodeName, Type propertyType, SessionFactoryImplementor factory) { 30 this.factory = factory; 31 this.nodeName = nodeName; 32 this.propertyType = propertyType; 33 34 } 35 36 39 public Getter getGetter(Class theClass, String propertyName) 40 throws PropertyNotFoundException { 41 if (nodeName==null) { 42 throw new MappingException("no node name for property: " + propertyName); 43 } 44 if ( ".".equals(nodeName) ) { 45 return new TextGetter(propertyType, factory); 46 } 47 else if ( nodeName.indexOf('/')>-1 ) { 48 return new ElementAttributeGetter(nodeName, propertyType, factory); 49 } 50 else if ( nodeName.indexOf('@')>-1 ) { 51 return new AttributeGetter(nodeName, propertyType, factory); 52 } 53 else { 54 return new ElementGetter(nodeName, propertyType, factory); 55 } 56 } 57 58 61 public Setter getSetter(Class theClass, String propertyName) 62 throws PropertyNotFoundException { 63 if (nodeName==null) { 64 throw new MappingException("no node name for property: " + propertyName); 65 } 66 if ( ".".equals(nodeName) ) { 67 return new TextSetter(propertyType); 68 } 69 else if ( nodeName.indexOf('/')>-1 ) { 70 return new ElementAttributeSetter(nodeName, propertyType); 71 } 72 else if ( nodeName.indexOf('@')>-1 ) { 73 return new AttributeSetter(nodeName, propertyType); 74 } 75 else { 76 return new ElementSetter(nodeName, propertyType); 77 } 78 } 79 80 83 public abstract static class Dom4jGetter implements Getter { 84 protected final Type propertyType; 85 protected final SessionFactoryImplementor factory; 86 87 Dom4jGetter(Type propertyType, SessionFactoryImplementor factory) { 88 this.propertyType = propertyType; 89 this.factory = factory; 90 } 91 92 public Object getForInsert(Object owner, Map mergeMap, SessionImplementor session) 93 throws HibernateException { 94 return get( owner ); 95 } 96 97 100 public Class getReturnType() { 101 return Object .class; 102 } 103 104 107 public String getMethodName() { 108 return null; 109 } 110 111 114 public Method getMethod() { 115 return null; 116 } 117 } 118 119 public abstract static class Dom4jSetter implements Setter { 120 protected final Type propertyType; 121 122 Dom4jSetter(Type propertyType) { 123 this.propertyType = propertyType; 124 } 125 126 129 public String getMethodName() { 130 return null; 131 } 132 133 136 public Method getMethod() { 137 return null; 138 } 139 } 140 141 145 public static class TextGetter extends Dom4jGetter { 146 147 TextGetter(Type propertyType, SessionFactoryImplementor factory) { 148 super(propertyType, factory); 149 } 150 151 public Object get(Object owner) throws HibernateException { 152 Element ownerElement = (Element) owner; 153 return propertyType.fromXMLNode(ownerElement, factory); 154 } 155 156 } 157 158 162 public static class AttributeGetter extends Dom4jGetter { 163 private final String attributeName; 164 165 AttributeGetter(String name, Type propertyType, SessionFactoryImplementor factory) { 166 super(propertyType, factory); 167 attributeName = name.substring(1); 168 } 169 170 public Object get(Object owner) throws HibernateException { 171 Element ownerElement = (Element) owner; 172 Node attribute = ownerElement.attribute(attributeName); 173 return attribute==null ? null : propertyType.fromXMLNode(attribute, factory); 174 } 175 176 } 177 178 182 public static class ElementGetter extends Dom4jGetter { 183 private final String elementName; 184 185 ElementGetter(String name, Type propertyType, SessionFactoryImplementor factory) { 186 super(propertyType, factory); 187 elementName = name; 188 } 189 190 public Object get(Object owner) throws HibernateException { 191 Element ownerElement = (Element) owner; 192 Node element = ownerElement.element(elementName); 193 return element==null ? null : propertyType.fromXMLNode(element, factory); 194 } 195 196 } 197 198 202 public static class ElementAttributeGetter extends Dom4jGetter { 203 private final String elementName; 204 private final String attributeName; 205 206 ElementAttributeGetter(String name, Type propertyType, SessionFactoryImplementor factory) { 207 super(propertyType, factory); 208 elementName = name.substring( 0, name.indexOf('/') ); 209 attributeName = name.substring( name.indexOf('/')+2 ); 210 } 211 212 public Object get(Object owner) throws HibernateException { 213 Element ownerElement = (Element) owner; 214 215 Element element = ownerElement.element(elementName); 216 217 if ( element==null ) { 218 return null; 219 } 220 else { 221 Attribute attribute = element.attribute(attributeName); 222 if (attribute==null) { 223 return null; 224 } 225 else { 226 return propertyType.fromXMLNode(attribute, factory); 227 } 228 } 229 } 230 } 231 232 233 237 public static class TextSetter extends Dom4jSetter { 238 239 TextSetter(Type propertyType) { 240 super(propertyType); 241 } 242 243 public void set(Object target, Object value, SessionFactoryImplementor factory) 244 throws HibernateException { 245 Element owner = ( Element ) target; 246 if ( !propertyType.isXMLElement() ) { if (value==null) { 248 owner.setText(null); } 250 else { 251 propertyType.setToXMLNode(owner, value, factory); 252 } 253 } 254 } 255 256 } 257 258 262 public static class AttributeSetter extends Dom4jSetter { 263 private final String attributeName; 264 265 AttributeSetter(String name, Type propertyType) { 266 super(propertyType); 267 attributeName = name.substring(1); 268 } 269 270 public void set(Object target, Object value, SessionFactoryImplementor factory) 271 throws HibernateException { 272 Element owner = ( Element ) target; 273 Attribute attribute = owner.attribute(attributeName); 274 if (value==null) { 275 if (attribute!=null) attribute.detach(); 276 } 277 else { 278 if (attribute==null) { 279 owner.addAttribute(attributeName, "null"); 280 attribute = owner.attribute(attributeName); 281 } 282 propertyType.setToXMLNode(attribute, value, factory); 283 } 284 } 285 286 } 287 288 292 public static class ElementSetter extends Dom4jSetter { 293 private final String elementName; 294 295 ElementSetter(String name, Type propertyType) { 296 super(propertyType); 297 elementName = name; 298 } 299 300 public void set(Object target, Object value, SessionFactoryImplementor factory) 301 throws HibernateException { 302 if (value!=CollectionType.UNFETCHED_COLLECTION) { 303 Element owner = ( Element ) target; 304 Element existing = owner.element(elementName); 305 if (existing!=null) existing.detach(); 306 if (value!=null) { 307 Element element = owner.addElement(elementName); 308 propertyType.setToXMLNode(element, value, factory); 309 } 310 } 311 } 312 313 } 314 315 319 public static class ElementAttributeSetter extends Dom4jSetter { 320 private final String elementName; 321 private final String attributeName; 322 323 ElementAttributeSetter(String name, Type propertyType) { 324 super(propertyType); 325 elementName = name.substring( 0, name.indexOf('/') ); 326 attributeName = name.substring( name.indexOf('/')+2 ); 327 } 328 329 public void set(Object target, Object value, SessionFactoryImplementor factory) 330 throws HibernateException { 331 Element owner = ( Element ) target; 332 Element element = owner.element(elementName); 333 if (value==null) { 334 if (element!=null) element.detach(); 335 } 336 else { 337 Attribute attribute; 338 if (element==null) { 339 element = owner.addElement(elementName); 340 attribute = null; 341 } 342 else { 343 attribute = element.attribute(attributeName); 344 } 345 346 if (attribute==null) { 347 element.addAttribute(attributeName, "null"); 348 attribute = element.attribute(attributeName); 349 } 350 propertyType.setToXMLNode(attribute, value, factory); 351 } 352 } 353 354 } 355 356 357 } 358 | Popular Tags |