001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: BeanAttributeList.java,v 1.3 2001/03/06 16:40:19 jstrachan Exp $ 008 */ 009 010package org.dom4j.bean; 011 012import java.util.AbstractList; 013import java.util.Iterator; 014import java.util.List; 015 016import org.dom4j.Attribute; 017import org.dom4j.Element; 018import org.dom4j.QName; 019 020/** <p><code>BeanAttributeList</code> implements a list of Attributes 021 * which are the properties of a JavaBean.</p> 022 * 023 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 024 * @version $Revision: 1.3 $ 025 */ 026public class BeanAttributeList extends AbstractList { 027 028 /** The BeanElement that this */ 029 private BeanElement parent; 030 031 /** The BeanElement that this */ 032 private BeanMetaData beanMetaData; 033 034 /** The attributes */ 035 private BeanAttribute[] attributes; 036 037 038 public BeanAttributeList(BeanElement parent, BeanMetaData beanMetaData) { 039 this.parent = parent; 040 this.beanMetaData = beanMetaData; 041 this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ]; 042 } 043 044 public BeanAttributeList(BeanElement parent) { 045 this.parent = parent; 046 047 Object data = parent.getData(); 048 Class beanClass = (data != null) ? data.getClass() : null; 049 this.beanMetaData = BeanMetaData.get( beanClass ); 050 this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ]; 051 } 052 053 public Attribute attribute(String name) { 054 int index = beanMetaData.getIndex(name); 055 return attribute(index); 056 } 057 058 public Attribute attribute(QName qname) { 059 int index = beanMetaData.getIndex(qname); 060 return attribute(index); 061 } 062 063 public BeanAttribute attribute(int index) { 064 if ( index >= 0 && index <= attributes.length ) { 065 BeanAttribute attribute = attributes[index]; 066 if ( attribute == null ) { 067 attribute = createAttribute( parent, index ); 068 attributes[index] = attribute; 069 } 070 return attribute; 071 } 072 return null; 073 } 074 075 public BeanElement getParent() { 076 return parent; 077 } 078 079 public QName getQName(int index) { 080 return beanMetaData.getQName(index); 081 } 082 083 public Object getData(int index) { 084 return beanMetaData.getData(index, parent.getData()); 085 } 086 087 public void setData(int index, Object data) { 088 beanMetaData.setData(index, parent.getData(), data); 089 } 090 091 092 // List interface 093 //------------------------------------------------------------------------- 094 public int size() { 095 return attributes.length; 096 } 097 098 public Object get(int index) { 099 BeanAttribute attribute = attributes[index]; 100 if ( attribute == null ) { 101 attribute = createAttribute( parent, index ); 102 attributes[index] = attribute; 103 } 104 return attribute; 105 } 106 107 public boolean add(Object object) { 108 throw new UnsupportedOperationException( "add(int, Object) is not supported" ); 109 } 110 111 public void add(int index, Object object) { 112 throw new UnsupportedOperationException( "add(int, Object) is not supported" ); 113 } 114 115 public Object set(int index, Object object) { 116 throw new UnsupportedOperationException( "set(int, Object) is not supported" ); 117 } 118 119 public boolean remove(Object object) { 120 return false; 121 } 122 123 public Object remove(int index) { 124 BeanAttribute attribute = (BeanAttribute) get(index); 125 Object oldValue = attribute.getValue(); 126 attribute.setValue(null); 127 return oldValue; 128 } 129 130 public void clear() { 131 for ( int i = 0, size = attributes.length; i < size; i++ ) { 132 BeanAttribute attribute = attributes[i]; 133 if ( attribute != null ) { 134 attribute.setValue( null ); 135 } 136 } 137 } 138 139 140 // Implementation methods 141 //------------------------------------------------------------------------- 142 protected BeanAttribute createAttribute( BeanElement parent, int index ) { 143 return new BeanAttribute( this, index ); 144 } 145} 146 147 148 149 150/* 151 * Redistribution and use of this software and associated documentation 152 * ("Software"), with or without modification, are permitted provided 153 * that the following conditions are met: 154 * 155 * 1. Redistributions of source code must retain copyright 156 * statements and notices. Redistributions must also contain a 157 * copy of this document. 158 * 159 * 2. Redistributions in binary form must reproduce the 160 * above copyright notice, this list of conditions and the 161 * following disclaimer in the documentation and/or other 162 * materials provided with the distribution. 163 * 164 * 3. The name "DOM4J" must not be used to endorse or promote 165 * products derived from this Software without prior written 166 * permission of MetaStuff, Ltd. For written permission, 167 * please contact dom4j-info@metastuff.com. 168 * 169 * 4. Products derived from this Software may not be called "DOM4J" 170 * nor may "DOM4J" appear in their names without prior written 171 * permission of MetaStuff, Ltd. DOM4J is a registered 172 * trademark of MetaStuff, Ltd. 173 * 174 * 5. Due credit should be given to the DOM4J Project 175 * (http://dom4j.org/). 176 * 177 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 178 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 179 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 180 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 181 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 182 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 183 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 184 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 185 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 186 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 187 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 188 * OF THE POSSIBILITY OF SUCH DAMAGE. 189 * 190 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 191 * 192 * $Id: BeanAttributeList.java,v 1.3 2001/03/06 16:40:19 jstrachan Exp $ 193 */