View Javadoc

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