1
2
3
4
5
6
7
8
9
10 package org.dom4j.bean;
11
12 import java.util.List;
13
14 import org.xml.sax.Attributes;
15
16 import org.dom4j.Attribute;
17 import org.dom4j.DocumentFactory;
18 import org.dom4j.Element;
19 import org.dom4j.Namespace;
20 import org.dom4j.QName;
21 import org.dom4j.tree.DefaultElement;
22 import org.dom4j.tree.NamespaceStack;
23
24
25
26 /*** <p><code>BeanElement</code> uses a Java Bean to store its attributes.</p>
27 *
28 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29 * @version $Revision: 1.13 $
30 */
31 public class BeanElement extends DefaultElement {
32
33 /*** The <code>DocumentFactory</code> instance used by default */
34 private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance();
35
36 /*** The JavaBean which defines my attributes */
37 private Object bean;
38
39
40 public BeanElement(String name, Object bean) {
41 this( DOCUMENT_FACTORY.createQName(name), bean );
42 }
43
44 public BeanElement(String name,Namespace namespace, Object bean) {
45 this( DOCUMENT_FACTORY.createQName(name, namespace), bean );
46 }
47
48 public BeanElement(QName qname, Object bean) {
49 super( qname);
50 this.bean = bean;
51 }
52
53 public BeanElement(QName qname) {
54 super( qname);
55 }
56
57 /*** @return the JavaBean associated with this element
58 */
59 public Object getData() {
60 return bean;
61 }
62
63 public void setData(Object bean) {
64 this.bean = bean;
65
66
67
68
69 setAttributeList(null);
70 }
71
72 public Attribute attribute(String name) {
73 return getBeanAttributeList().attribute(name);
74 }
75
76 public Attribute attribute(QName qname) {
77 return getBeanAttributeList().attribute(qname);
78 }
79
80 public Element addAttribute(String name, String value) {
81 Attribute attribute = attribute(name);
82 if (attribute != null ) {
83 attribute.setValue(value);
84 }
85 return this;
86 }
87
88 public Element addAttribute(QName qName, String value) {
89 Attribute attribute = attribute(qName);
90 if (attribute != null ) {
91 attribute.setValue(value);
92 }
93 return this;
94 }
95
96 public void setAttributes(List attributes) {
97 throw new UnsupportedOperationException( "setAttributes(List) is not supported yet!" );
98 }
99
100
101
102 public void setAttributes(Attributes attributes,
103 NamespaceStack namespaceStack,
104 boolean noNamespaceAttributes) {
105 String className = attributes.getValue("class");
106 if (className != null) {
107 try {
108 Class beanClass = Class.forName(className,
109 true,
110 BeanElement.class.getClassLoader());
111 this.setData(beanClass.newInstance());
112
113 for( int i=0; i<attributes.getLength(); i++){
114 String attributeName = attributes.getLocalName(i);
115 if( !"class".equalsIgnoreCase(attributeName) ){
116 addAttribute(attributeName, attributes.getValue(i));
117 }
118 }
119 }
120 catch (Exception ex) {
121
122 ( (BeanDocumentFactory)this.getDocumentFactory()).handleException(ex);
123 }
124 }
125 else {
126 super.setAttributes(attributes, namespaceStack, noNamespaceAttributes);
127 }
128 }
129
130
131
132
133
134 protected DocumentFactory getDocumentFactory() {
135 return DOCUMENT_FACTORY;
136 }
137
138 protected BeanAttributeList getBeanAttributeList() {
139 return (BeanAttributeList) attributeList();
140 }
141
142 /*** A Factory Method pattern which lazily creates
143 * a List implementation used to store content
144 */
145 protected List createAttributeList() {
146 return new BeanAttributeList(this);
147 }
148 protected List createAttributeList(int size) {
149 return new BeanAttributeList(this);
150 }
151 }
152
153
154
155
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
195
196
197
198
199