1 7 8 package org.dom4j.bean; 9 10 import java.util.List ; 11 12 import org.dom4j.Attribute; 13 import org.dom4j.DocumentFactory; 14 import org.dom4j.Element; 15 import org.dom4j.Namespace; 16 import org.dom4j.QName; 17 import org.dom4j.tree.DefaultElement; 18 import org.dom4j.tree.NamespaceStack; 19 20 import org.xml.sax.Attributes ; 21 22 30 public class BeanElement extends DefaultElement { 31 32 private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory 33 .getInstance(); 34 35 36 private Object bean; 37 38 public BeanElement(String name, Object bean) { 39 this(DOCUMENT_FACTORY.createQName(name), bean); 40 } 41 42 public BeanElement(String name, Namespace namespace, Object bean) { 43 this(DOCUMENT_FACTORY.createQName(name, namespace), bean); 44 } 45 46 public BeanElement(QName qname, Object bean) { 47 super(qname); 48 this.bean = bean; 49 } 50 51 public BeanElement(QName qname) { 52 super(qname); 53 } 54 55 60 public Object getData() { 61 return bean; 62 } 63 64 public void setData(Object data) { 65 this.bean = data; 66 67 setAttributeList(null); 71 } 72 73 public Attribute attribute(String name) { 74 return getBeanAttributeList().attribute(name); 75 } 76 77 public Attribute attribute(QName qname) { 78 return getBeanAttributeList().attribute(qname); 79 } 80 81 public Element addAttribute(String name, String value) { 82 Attribute attribute = attribute(name); 83 84 if (attribute != null) { 85 attribute.setValue(value); 86 } 87 88 return this; 89 } 90 91 public Element addAttribute(QName qName, String value) { 92 Attribute attribute = attribute(qName); 93 94 if (attribute != null) { 95 attribute.setValue(value); 96 } 97 98 return this; 99 } 100 101 public void setAttributes(List attributes) { 102 throw new UnsupportedOperationException ("Not implemented yet."); 103 } 104 105 public void setAttributes(Attributes attributes, 107 NamespaceStack namespaceStack, boolean noNamespaceAttributes) { 108 String className = attributes.getValue("class"); 109 110 if (className != null) { 111 try { 112 Class beanClass = Class.forName(className, true, 113 BeanElement.class.getClassLoader()); 114 this.setData(beanClass.newInstance()); 115 116 for (int i = 0; i < attributes.getLength(); i++) { 117 String attributeName = attributes.getLocalName(i); 118 119 if (!"class".equalsIgnoreCase(attributeName)) { 120 addAttribute(attributeName, attributes.getValue(i)); 121 } 122 } 123 } catch (Exception ex) { 124 ((BeanDocumentFactory) this.getDocumentFactory()) 126 .handleException(ex); 127 } 128 } else { 129 super.setAttributes(attributes, namespaceStack, 130 noNamespaceAttributes); 131 } 132 } 133 134 protected DocumentFactory getDocumentFactory() { 137 return DOCUMENT_FACTORY; 138 } 139 140 protected BeanAttributeList getBeanAttributeList() { 141 return (BeanAttributeList) attributeList(); 142 } 143 144 150 protected List createAttributeList() { 151 return new BeanAttributeList(this); 152 } 153 154 protected List createAttributeList(int size) { 155 return new BeanAttributeList(this); 156 } 157 } 158 159 195 | Popular Tags |