| /* | ||
| * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | ||
| * | ||
| * This software is open source. | ||
| * See the bottom of this file for the licence. | ||
| * | ||
| * $Id: BeanAttributeList.java,v 1.7 2004/06/25 08:03:33 maartenc Exp $ | ||
| */ | ||
| package org.dom4j.bean; | ||
| import java.util.AbstractList; | ||
| import org.dom4j.Attribute; | ||
| import org.dom4j.QName; | ||
| /** <p><code>BeanAttributeList</code> implements a list of Attributes | ||
| * which are the properties of a JavaBean.</p> | ||
| * | ||
| * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> | ||
| * @version $Revision: 1.7 $ | ||
| */ | ||
| public class BeanAttributeList extends AbstractList { | ||
| /** The BeanElement that this */ | ||
| private BeanElement parent; | ||
| /** The BeanElement that this */ | ||
| private BeanMetaData beanMetaData; | ||
| /** The attributes */ | ||
| private BeanAttribute[] attributes; | ||
| 0x | public BeanAttributeList(BeanElement parent, BeanMetaData beanMetaData) { | |
| 0x | this.parent = parent; | |
| 0x | this.beanMetaData = beanMetaData; | |
| 0x | this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ]; | |
| 0x | } | |
| 0x | public BeanAttributeList(BeanElement parent) { | |
| 0x | this.parent = parent; | |
| 0x | Object data = parent.getData(); | |
| 0/2 0x | Class beanClass = (data != null) ? data.getClass() : null; | |
| 0x | this.beanMetaData = BeanMetaData.get( beanClass ); | |
| 0x | this.attributes = new BeanAttribute[ beanMetaData.attributeCount() ]; | |
| 0x | } | |
| public Attribute attribute(String name) { | ||
| 0x | int index = beanMetaData.getIndex(name); | |
| 0x | return attribute(index); | |
| } | ||
| public Attribute attribute(QName qname) { | ||
| 0x | int index = beanMetaData.getIndex(qname); | |
| 0x | return attribute(index); | |
| } | ||
| public BeanAttribute attribute(int index) { | ||
| 0/4 0x | if ( index >= 0 && index <= attributes.length ) { | |
| 0x | BeanAttribute attribute = attributes[index]; | |
| 0/2 0x | if ( attribute == null ) { | |
| 0x | attribute = createAttribute( parent, index ); | |
| 0x | attributes[index] = attribute; | |
| } | ||
| 0x | return attribute; | |
| } | ||
| 0x | return null; | |
| } | ||
| public BeanElement getParent() { | ||
| 0x | return parent; | |
| } | ||
| public QName getQName(int index) { | ||
| 0x | return beanMetaData.getQName(index); | |
| } | ||
| public Object getData(int index) { | ||
| 0x | return beanMetaData.getData(index, parent.getData()); | |
| } | ||
| public void setData(int index, Object data) { | ||
| 0x | beanMetaData.setData(index, parent.getData(), data); | |
| 0x | } | |
| // List interface | ||
| //------------------------------------------------------------------------- | ||
| public int size() { | ||
| 0x | return attributes.length; | |
| } | ||
| public Object get(int index) { | ||
| 0x | BeanAttribute attribute = attributes[index]; | |
| 0/2 0x | if ( attribute == null ) { | |
| 0x | attribute = createAttribute( parent, index ); | |
| 0x | attributes[index] = attribute; | |
| } | ||
| 0x | return attribute; | |
| } | ||
| public boolean add(Object object) { | ||
| 0x | throw new UnsupportedOperationException( "add(int, Object) is not supported" ); | |
| } | ||
| public void add(int index, Object object) { | ||
| 0x | throw new UnsupportedOperationException( "add(int, Object) is not supported" ); | |
| } | ||
| public Object set(int index, Object object) { | ||
| 0x | throw new UnsupportedOperationException( "set(int, Object) is not supported" ); | |
| } | ||
| public boolean remove(Object object) { | ||
| 0x | return false; | |
| } | ||
| public Object remove(int index) { | ||
| 0x | BeanAttribute attribute = (BeanAttribute) get(index); | |
| 0x | Object oldValue = attribute.getValue(); | |
| 0x | attribute.setValue(null); | |
| 0x | return oldValue; | |
| } | ||
| public void clear() { | ||
| 0/2 0x | for ( int i = 0, size = attributes.length; i < size; i++ ) { | |
| 0x | BeanAttribute attribute = attributes[i]; | |
| 0/2 0x | if ( attribute != null ) { | |
| 0x | attribute.setValue( null ); | |
| } | ||
| } | ||
| 0x | } | |
| // Implementation methods | ||
| //------------------------------------------------------------------------- | ||
| protected BeanAttribute createAttribute( BeanElement parent, int index ) { | ||
| 0x | return new BeanAttribute( this, index ); | |
| } | ||
| } | ||
| /* | ||
| * Redistribution and use of this software and associated documentation | ||
| * ("Software"), with or without modification, are permitted provided | ||
| * that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain copyright | ||
| * statements and notices. Redistributions must also contain a | ||
| * copy of this document. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the | ||
| * above copyright notice, this list of conditions and the | ||
| * following disclaimer in the documentation and/or other | ||
| * materials provided with the distribution. | ||
| * | ||
| * 3. The name "DOM4J" must not be used to endorse or promote | ||
| * products derived from this Software without prior written | ||
| * permission of MetaStuff, Ltd. For written permission, | ||
| * please contact dom4j-info@metastuff.com. | ||
| * | ||
| * 4. Products derived from this Software may not be called "DOM4J" | ||
| * nor may "DOM4J" appear in their names without prior written | ||
| * permission of MetaStuff, Ltd. DOM4J is a registered | ||
| * trademark of MetaStuff, Ltd. | ||
| * | ||
| * 5. Due credit should be given to the DOM4J Project - | ||
| * http://www.dom4j.org | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | ||
| * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | ||
| * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
| * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
| * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | ||
| * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| * | ||
| * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | ||
| * | ||
| * $Id: BeanAttributeList.java,v 1.7 2004/06/25 08:03:33 maartenc Exp $ | ||
| */ |