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