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 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class BeanElement extends DefaultElement { |
32 |
| |
33 |
| |
34 |
| private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance(); |
35 |
| |
36 |
| |
37 |
| private Object bean; |
38 |
| |
39 |
| |
40 |
0
| public BeanElement(String name, Object bean) {
|
41 |
0
| this( DOCUMENT_FACTORY.createQName(name), bean );
|
42 |
| } |
43 |
| |
44 |
0
| public BeanElement(String name,Namespace namespace, Object bean) {
|
45 |
0
| this( DOCUMENT_FACTORY.createQName(name, namespace), bean );
|
46 |
| } |
47 |
| |
48 |
0
| public BeanElement(QName qname, Object bean) {
|
49 |
0
| super( qname);
|
50 |
0
| this.bean = bean;
|
51 |
| } |
52 |
| |
53 |
6
| public BeanElement(QName qname) {
|
54 |
6
| super( qname);
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
8
| public Object getData() {
|
60 |
8
| return bean;
|
61 |
| } |
62 |
| |
63 |
4
| public void setData(Object bean) {
|
64 |
4
| this.bean = bean;
|
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
4
| setAttributeList(null);
|
70 |
| } |
71 |
| |
72 |
4
| public Attribute attribute(String name) {
|
73 |
4
| return getBeanAttributeList().attribute(name);
|
74 |
| } |
75 |
| |
76 |
0
| public Attribute attribute(QName qname) {
|
77 |
0
| return getBeanAttributeList().attribute(qname);
|
78 |
| } |
79 |
| |
80 |
4
| public Element addAttribute(String name, String value) {
|
81 |
4
| Attribute attribute = attribute(name);
|
82 |
4
| if (attribute != null ) {
|
83 |
4
| attribute.setValue(value);
|
84 |
| } |
85 |
4
| return this;
|
86 |
| } |
87 |
| |
88 |
0
| public Element addAttribute(QName qName, String value) {
|
89 |
0
| Attribute attribute = attribute(qName);
|
90 |
0
| if (attribute != null ) {
|
91 |
0
| attribute.setValue(value);
|
92 |
| } |
93 |
0
| return this;
|
94 |
| } |
95 |
| |
96 |
0
| public void setAttributes(List attributes) {
|
97 |
0
| throw new UnsupportedOperationException( "setAttributes(List) is not supported yet!" );
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
6
| public void setAttributes(Attributes attributes,
|
103 |
| NamespaceStack namespaceStack, |
104 |
| boolean noNamespaceAttributes) { |
105 |
6
| String className = attributes.getValue("class");
|
106 |
6
| if (className != null) {
|
107 |
4
| try {
|
108 |
4
| Class beanClass = Class.forName(className,
|
109 |
| true, |
110 |
| BeanElement.class.getClassLoader()); |
111 |
4
| this.setData(beanClass.newInstance());
|
112 |
| |
113 |
4
| for( int i=0; i<attributes.getLength(); i++){
|
114 |
8
| String attributeName = attributes.getLocalName(i);
|
115 |
8
| if( !"class".equalsIgnoreCase(attributeName) ){
|
116 |
4
| addAttribute(attributeName, attributes.getValue(i));
|
117 |
| } |
118 |
| } |
119 |
| } |
120 |
| catch (Exception ex) { |
121 |
| |
122 |
0
| ( (BeanDocumentFactory)this.getDocumentFactory()).handleException(ex);
|
123 |
| } |
124 |
| } |
125 |
| else { |
126 |
2
| super.setAttributes(attributes, namespaceStack, noNamespaceAttributes);
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
10
| protected DocumentFactory getDocumentFactory() {
|
135 |
10
| return DOCUMENT_FACTORY;
|
136 |
| } |
137 |
| |
138 |
4
| protected BeanAttributeList getBeanAttributeList() {
|
139 |
4
| return (BeanAttributeList) attributeList();
|
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
4
| protected List createAttributeList() {
|
146 |
4
| return new BeanAttributeList(this);
|
147 |
| } |
148 |
0
| protected List createAttributeList(int size) {
|
149 |
0
| 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 |
| |