View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
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      // List interface
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     // Implementation methods
144     // -------------------------------------------------------------------------
145     protected BeanAttribute createAttribute(BeanElement element, int index) {
146         return new BeanAttribute(this, index);
147     }
148 }
149 
150 /*
151  * Redistribution and use of this software and associated documentation
152  * ("Software"), with or without modification, are permitted provided that the
153  * following conditions are met:
154  * 
155  * 1. Redistributions of source code must retain copyright statements and
156  * notices. Redistributions must also contain a copy of this document.
157  * 
158  * 2. Redistributions in binary form must reproduce the above copyright notice,
159  * this list of conditions and the following disclaimer in the documentation
160  * and/or other materials provided with the distribution.
161  * 
162  * 3. The name "DOM4J" must not be used to endorse or promote products derived
163  * from this Software without prior written permission of MetaStuff, Ltd. For
164  * written permission, please contact dom4j-info@metastuff.com.
165  * 
166  * 4. Products derived from this Software may not be called "DOM4J" nor may
167  * "DOM4J" appear in their names without prior written permission of MetaStuff,
168  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
169  * 
170  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
171  * 
172  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
173  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
176  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
177  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
178  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
179  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
180  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
181  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
182  * POSSIBILITY OF SUCH DAMAGE.
183  * 
184  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
185  */