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 * <p>
24 * <code>BeanElement</code> uses a Java Bean to store its attributes.
25 * </p>
26 *
27 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
28 * @version $Revision: 1.15 $
29 */
30 public class BeanElement extends DefaultElement {
31 /*** The <code>DocumentFactory</code> instance used by default */
32 private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory
33 .getInstance();
34
35 /*** The JavaBean which defines my attributes */
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 /***
56 * DOCUMENT ME!
57 *
58 * @return the JavaBean associated with this element
59 */
60 public Object getData() {
61 return bean;
62 }
63
64 public void setData(Object data) {
65 this.bean = data;
66
67
68
69
70 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
106 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
125 ((BeanDocumentFactory) this.getDocumentFactory())
126 .handleException(ex);
127 }
128 } else {
129 super.setAttributes(attributes, namespaceStack,
130 noNamespaceAttributes);
131 }
132 }
133
134
135
136 protected DocumentFactory getDocumentFactory() {
137 return DOCUMENT_FACTORY;
138 }
139
140 protected BeanAttributeList getBeanAttributeList() {
141 return (BeanAttributeList) attributeList();
142 }
143
144 /***
145 * A Factory Method pattern which lazily creates a List implementation used
146 * to store content
147 *
148 * @return DOCUMENT ME!
149 */
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
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