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: BeanElement.java,v 1.8 2001/11/19 11:14:23 jstrachan Exp $ 008 */ 009 010package org.dom4j.bean; 011 012import java.util.Iterator; 013import java.util.List; 014import java.util.Map; 015 016import org.dom4j.Attribute; 017import org.dom4j.Element; 018import org.dom4j.DocumentFactory; 019import org.dom4j.Namespace; 020import org.dom4j.QName; 021import org.dom4j.tree.DefaultElement; 022 023/** <p><code>BeanElement</code> uses a Java Bean to store its attributes.</p> 024 * 025 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 026 * @version $Revision: 1.8 $ 027 */ 028public class BeanElement extends DefaultElement { 029 030 /** The <code>DocumentFactory</code> instance used by default */ 031 private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance(); 032 033 /** The JavaBean which defines my attributes */ 034 private Object bean; 035 036 037 public BeanElement(String name, Object bean) { 038 this( DOCUMENT_FACTORY.createQName(name), bean ); 039 } 040 041 public BeanElement(String name,Namespace namespace, Object bean) { 042 this( DOCUMENT_FACTORY.createQName(name, namespace), bean ); 043 } 044 045 public BeanElement(QName qname, Object bean) { 046 super( qname); 047 this.bean = bean; 048 } 049 050 public BeanElement(QName qname) { 051 super( qname); 052 } 053 054 /** @return the JavaBean associated with this element 055 */ 056 public Object getData() { 057 return bean; 058 } 059 060 public void setData(Object data) { 061 this.bean = bean; 062 063 // force the attributeList to be lazily 064 // created next time an attribute related 065 // method is called again. 066 setAttributeList(null); 067 } 068 069 public Attribute attribute(String name) { 070 return getBeanAttributeList().attribute(name); 071 } 072 073 public Attribute attribute(QName qname) { 074 return getBeanAttributeList().attribute(qname); 075 } 076 077 public Element addAttribute(String name, String value) { 078 Attribute attribute = attribute(name); 079 if (attribute != null ) { 080 attribute.setValue(value); 081 } 082 return this; 083 } 084 085 public Element addAttribute(QName qName, String value) { 086 Attribute attribute = attribute(qName); 087 if (attribute != null ) { 088 attribute.setValue(value); 089 } 090 return this; 091 } 092 093 public void setAttributes(List attributes) { 094 throw new UnsupportedOperationException( "setAttributes(List) is not supported yet!" ); 095 } 096 097 098 099 // Implementation methods 100 //------------------------------------------------------------------------- 101 protected DocumentFactory getDocumentFactory() { 102 return DOCUMENT_FACTORY; 103 } 104 105 protected BeanAttributeList getBeanAttributeList() { 106 return (BeanAttributeList) attributeList(); 107 } 108 109 /** A Factory Method pattern which lazily creates 110 * a List implementation used to store content 111 */ 112 protected List createAttributeList() { 113 return new BeanAttributeList(this); 114 } 115} 116 117 118 119 120/* 121 * Redistribution and use of this software and associated documentation 122 * ("Software"), with or without modification, are permitted provided 123 * that the following conditions are met: 124 * 125 * 1. Redistributions of source code must retain copyright 126 * statements and notices. Redistributions must also contain a 127 * copy of this document. 128 * 129 * 2. Redistributions in binary form must reproduce the 130 * above copyright notice, this list of conditions and the 131 * following disclaimer in the documentation and/or other 132 * materials provided with the distribution. 133 * 134 * 3. The name "DOM4J" must not be used to endorse or promote 135 * products derived from this Software without prior written 136 * permission of MetaStuff, Ltd. For written permission, 137 * please contact dom4j-info@metastuff.com. 138 * 139 * 4. Products derived from this Software may not be called "DOM4J" 140 * nor may "DOM4J" appear in their names without prior written 141 * permission of MetaStuff, Ltd. DOM4J is a registered 142 * trademark of MetaStuff, Ltd. 143 * 144 * 5. Due credit should be given to the DOM4J Project 145 * (http://dom4j.org/). 146 * 147 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 148 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 149 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 150 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 151 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 152 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 153 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 154 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 155 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 156 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 157 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 158 * OF THE POSSIBILITY OF SUCH DAMAGE. 159 * 160 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 161 * 162 * $Id: BeanElement.java,v 1.8 2001/11/19 11:14:23 jstrachan Exp $ 163 */