1 19 20 package org.apache.cayenne.xml; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.reflect.PropertyUtils; 27 import org.w3c.dom.Element ; 28 29 35 class SerializableEntity implements XMLSerializable { 36 37 Element descriptor; 38 XMLMappingDescriptor descriptorMap; 39 40 transient Object object; 42 43 public SerializableEntity(XMLMappingDescriptor descriptorMap, Element descriptor) { 44 this.descriptor = descriptor; 45 this.descriptorMap = descriptorMap; 46 } 47 48 String getName() { 49 return descriptor.getAttribute("name"); 50 } 51 52 Element getDescriptor() { 53 return descriptor; 54 } 55 56 void setObject(Object object) { 57 this.object = object; 58 } 59 60 public void encodeAsXML(XMLEncoder encoder) { 61 if (object instanceof Collection ) { 62 Collection c = (Collection ) object; 63 if (!c.isEmpty()) { 64 65 Iterator it = c.iterator(); 67 encodeObject(encoder, it.next(), true); 68 while (it.hasNext()) { 69 encodeObject(encoder, it.next(), false); 70 } 71 72 encoder.pop(); 74 } 75 } 76 else { 77 encodeObject(encoder, this.object, true); 78 79 encoder.pop(); 81 } 82 } 83 84 public void decodeFromXML(XMLDecoder decoder) { 85 throw new CayenneRuntimeException("Decoding is not supported by this object"); 86 } 87 88 void encodeObject(XMLEncoder encoder, Object object, boolean push) { 89 encoder.setRoot(descriptor.getAttribute("xmlTag"), null, push); 90 91 Iterator it = XMLUtil.getChildren(descriptor).iterator(); 92 while (it.hasNext()) { 93 94 Element property = (Element ) it.next(); 95 String xmlTag = property.getAttribute("xmlTag"); 96 String name = property.getAttribute("name"); 97 Object value = PropertyUtils.getProperty(object, name); 98 99 if (value == null) { 100 continue; 101 } 102 103 SerializableEntity relatedEntity = descriptorMap.getEntity(xmlTag); 104 if (relatedEntity != null) { 105 relatedEntity.setObject(value); 106 relatedEntity.encodeAsXML(encoder); 107 } 108 else { 109 encoder.encodeProperty(xmlTag, value, false); 110 } 111 } 112 } 113 } 114 | Popular Tags |