| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 6 |  |  | 
| 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 |  |  | 
| 23 |  |  | 
| 24 |  |  | 
| 25 |  |  | 
| 26 |  |  | 
| 27 |  |  | 
| 28 |  |  | 
| 29 |  |  | 
| 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 | 0 | public BeanElement(String name, Object bean) { | 
| 39 | 0 | this(DOCUMENT_FACTORY.createQName(name), bean); | 
| 40 |  | } | 
| 41 |  |  | 
| 42 | 0 | public BeanElement(String name, Namespace namespace, Object bean) { | 
| 43 | 0 | this(DOCUMENT_FACTORY.createQName(name, namespace), bean); | 
| 44 |  | } | 
| 45 |  |  | 
| 46 | 0 | public BeanElement(QName qname, Object bean) { | 
| 47 | 0 | super(qname); | 
| 48 | 0 | this.bean = bean; | 
| 49 |  | } | 
| 50 |  |  | 
| 51 | 3 | public BeanElement(QName qname) { | 
| 52 | 3 | super(qname); | 
| 53 |  | } | 
| 54 |  |  | 
| 55 |  |  | 
| 56 |  |  | 
| 57 |  |  | 
| 58 |  |  | 
| 59 |  |  | 
| 60 | 4 | public Object getData() { | 
| 61 | 4 | return bean; | 
| 62 |  | } | 
| 63 |  |  | 
| 64 | 2 | public void setData(Object data) { | 
| 65 | 2 | this.bean = data; | 
| 66 |  |  | 
| 67 |  |  | 
| 68 |  |  | 
| 69 |  |  | 
| 70 | 2 | setAttributeList(null); | 
| 71 |  | } | 
| 72 |  |  | 
| 73 | 2 | public Attribute attribute(String name) { | 
| 74 | 2 | return getBeanAttributeList().attribute(name); | 
| 75 |  | } | 
| 76 |  |  | 
| 77 | 0 | public Attribute attribute(QName qname) { | 
| 78 | 0 | return getBeanAttributeList().attribute(qname); | 
| 79 |  | } | 
| 80 |  |  | 
| 81 | 2 | public Element addAttribute(String name, String value) { | 
| 82 | 2 | Attribute attribute = attribute(name); | 
| 83 |  |  | 
| 84 | 2 | if (attribute != null) { | 
| 85 | 2 | attribute.setValue(value); | 
| 86 |  | } | 
| 87 |  |  | 
| 88 | 2 | return this; | 
| 89 |  | } | 
| 90 |  |  | 
| 91 | 0 | public Element addAttribute(QName qName, String value) { | 
| 92 | 0 | Attribute attribute = attribute(qName); | 
| 93 |  |  | 
| 94 | 0 | if (attribute != null) { | 
| 95 | 0 | attribute.setValue(value); | 
| 96 |  | } | 
| 97 |  |  | 
| 98 | 0 | return this; | 
| 99 |  | } | 
| 100 |  |  | 
| 101 | 0 | public void setAttributes(List attributes) { | 
| 102 | 0 | throw new UnsupportedOperationException("Not implemented yet."); | 
| 103 |  | } | 
| 104 |  |  | 
| 105 |  |  | 
| 106 | 3 | public void setAttributes(Attributes attributes, | 
| 107 |  | NamespaceStack namespaceStack, boolean noNamespaceAttributes) { | 
| 108 | 3 | String className = attributes.getValue("class"); | 
| 109 |  |  | 
| 110 | 3 | if (className != null) { | 
| 111 | 2 | try { | 
| 112 | 2 | Class beanClass = Class.forName(className, true, | 
| 113 |  | BeanElement.class.getClassLoader()); | 
| 114 | 2 | this.setData(beanClass.newInstance()); | 
| 115 |  |  | 
| 116 | 2 | for (int i = 0; i < attributes.getLength(); i++) { | 
| 117 | 4 | String attributeName = attributes.getLocalName(i); | 
| 118 |  |  | 
| 119 | 4 | if (!"class".equalsIgnoreCase(attributeName)) { | 
| 120 | 2 | addAttribute(attributeName, attributes.getValue(i)); | 
| 121 |  | } | 
| 122 |  | } | 
| 123 |  | } catch (Exception ex) { | 
| 124 |  |  | 
| 125 | 0 | ((BeanDocumentFactory) this.getDocumentFactory()) | 
| 126 |  | .handleException(ex); | 
| 127 |  | } | 
| 128 |  | } else { | 
| 129 | 1 | super.setAttributes(attributes, namespaceStack, | 
| 130 |  | noNamespaceAttributes); | 
| 131 |  | } | 
| 132 |  | } | 
| 133 |  |  | 
| 134 |  |  | 
| 135 |  |  | 
| 136 | 5 | protected DocumentFactory getDocumentFactory() { | 
| 137 | 5 | return DOCUMENT_FACTORY; | 
| 138 |  | } | 
| 139 |  |  | 
| 140 | 2 | protected BeanAttributeList getBeanAttributeList() { | 
| 141 | 2 | return (BeanAttributeList) attributeList(); | 
| 142 |  | } | 
| 143 |  |  | 
| 144 |  |  | 
| 145 |  |  | 
| 146 |  |  | 
| 147 |  |  | 
| 148 |  |  | 
| 149 |  |  | 
| 150 | 2 | protected List createAttributeList() { | 
| 151 | 2 | return new BeanAttributeList(this); | 
| 152 |  | } | 
| 153 |  |  | 
| 154 | 0 | protected List createAttributeList(int size) { | 
| 155 | 0 | return new BeanAttributeList(this); | 
| 156 |  | } | 
| 157 |  | } | 
| 158 |  |  | 
| 159 |  |  | 
| 160 |  |  | 
| 161 |  |  | 
| 162 |  |  | 
| 163 |  |  | 
| 164 |  |  | 
| 165 |  |  | 
| 166 |  |  | 
| 167 |  |  | 
| 168 |  |  | 
| 169 |  |  | 
| 170 |  |  | 
| 171 |  |  | 
| 172 |  |  | 
| 173 |  |  | 
| 174 |  |  | 
| 175 |  |  | 
| 176 |  |  | 
| 177 |  |  | 
| 178 |  |  | 
| 179 |  |  | 
| 180 |  |  | 
| 181 |  |  | 
| 182 |  |  | 
| 183 |  |  | 
| 184 |  |  | 
| 185 |  |  | 
| 186 |  |  | 
| 187 |  |  | 
| 188 |  |  | 
| 189 |  |  | 
| 190 |  |  | 
| 191 |  |  | 
| 192 |  |  | 
| 193 |  |  | 
| 194 |  |  |