1
2
3
4
5
6
7
8
9
10 package org.dom4j.bean;
11
12 import java.util.AbstractList;
13
14 import org.dom4j.Attribute;
15 import org.dom4j.QName;
16
17 /*** <p><code>BeanAttributeList</code> implements a list of Attributes
18 * which are the properties of a JavaBean.</p>
19 *
20 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
21 * @version $Revision: 1.7 $
22 */
23 public class BeanAttributeList extends AbstractList {
24
25 /*** The BeanElement that this */
26 private BeanElement parent;
27
28 /*** The BeanElement that this */
29 private BeanMetaData beanMetaData;
30
31 /*** The attributes */
32 private BeanAttribute[] attributes;
33
34
35 public BeanAttributeList(BeanElement parent, BeanMetaData beanMetaData) {
36 this.parent = parent;
37 this.beanMetaData = beanMetaData;
38 this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ];
39 }
40
41 public BeanAttributeList(BeanElement parent) {
42 this.parent = parent;
43
44 Object data = parent.getData();
45 Class beanClass = (data != null) ? data.getClass() : null;
46 this.beanMetaData = BeanMetaData.get( beanClass );
47 this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ];
48 }
49
50 public Attribute attribute(String name) {
51 int index = beanMetaData.getIndex(name);
52 return attribute(index);
53 }
54
55 public Attribute attribute(QName qname) {
56 int index = beanMetaData.getIndex(qname);
57 return attribute(index);
58 }
59
60 public BeanAttribute attribute(int index) {
61 if ( index >= 0 && index <= attributes.length ) {
62 BeanAttribute attribute = attributes[index];
63 if ( attribute == null ) {
64 attribute = createAttribute( parent, index );
65 attributes[index] = attribute;
66 }
67 return attribute;
68 }
69 return null;
70 }
71
72 public BeanElement getParent() {
73 return parent;
74 }
75
76 public QName getQName(int index) {
77 return beanMetaData.getQName(index);
78 }
79
80 public Object getData(int index) {
81 return beanMetaData.getData(index, parent.getData());
82 }
83
84 public void setData(int index, Object data) {
85 beanMetaData.setData(index, parent.getData(), data);
86 }
87
88
89
90
91 public int size() {
92 return attributes.length;
93 }
94
95 public Object get(int index) {
96 BeanAttribute attribute = attributes[index];
97 if ( attribute == null ) {
98 attribute = createAttribute( parent, index );
99 attributes[index] = attribute;
100 }
101 return attribute;
102 }
103
104 public boolean add(Object object) {
105 throw new UnsupportedOperationException( "add(int, Object) is not supported" );
106 }
107
108 public void add(int index, Object object) {
109 throw new UnsupportedOperationException( "add(int, Object) is not supported" );
110 }
111
112 public Object set(int index, Object object) {
113 throw new UnsupportedOperationException( "set(int, Object) is not supported" );
114 }
115
116 public boolean remove(Object object) {
117 return false;
118 }
119
120 public Object remove(int index) {
121 BeanAttribute attribute = (BeanAttribute) get(index);
122 Object oldValue = attribute.getValue();
123 attribute.setValue(null);
124 return oldValue;
125 }
126
127 public void clear() {
128 for ( int i = 0, size = attributes.length; i < size; i++ ) {
129 BeanAttribute attribute = attributes[i];
130 if ( attribute != null ) {
131 attribute.setValue( null );
132 }
133 }
134 }
135
136
137
138
139 protected BeanAttribute createAttribute( BeanElement parent, int index ) {
140 return new BeanAttribute( this, index );
141 }
142 }
143
144
145
146
147
148
149
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