KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > bean > BeanElement


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.List JavaDoc;
11
12 import org.dom4j.Attribute;
13 import org.dom4j.DocumentFactory;
14 import org.dom4j.Element;
15 import org.dom4j.Namespace;
16 import org.dom4j.QName;
17 import org.dom4j.tree.DefaultElement;
18 import org.dom4j.tree.NamespaceStack;
19
20 import org.xml.sax.Attributes JavaDoc;
21
22 /**
23  * <p>
24  * <code>BeanElement</code> uses a Java Bean to store its attributes.
25  * </p>
26  *
27  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
28  * @version $Revision: 1.15 $
29  */

30 public class BeanElement extends DefaultElement {
31     /** The <code>DocumentFactory</code> instance used by default */
32     private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory
33             .getInstance();
34
35     /** The JavaBean which defines my attributes */
36     private Object JavaDoc bean;
37
38     public BeanElement(String JavaDoc name, Object JavaDoc bean) {
39         this(DOCUMENT_FACTORY.createQName(name), bean);
40     }
41
42     public BeanElement(String JavaDoc name, Namespace namespace, Object JavaDoc bean) {
43         this(DOCUMENT_FACTORY.createQName(name, namespace), bean);
44     }
45
46     public BeanElement(QName qname, Object JavaDoc bean) {
47         super(qname);
48         this.bean = bean;
49     }
50
51     public BeanElement(QName qname) {
52         super(qname);
53     }
54
55     /**
56      * DOCUMENT ME!
57      *
58      * @return the JavaBean associated with this element
59      */

60     public Object JavaDoc getData() {
61         return bean;
62     }
63
64     public void setData(Object JavaDoc data) {
65         this.bean = data;
66
67         // force the attributeList to be lazily
68
// created next time an attribute related
69
// method is called again.
70
setAttributeList(null);
71     }
72
73     public Attribute attribute(String JavaDoc name) {
74         return getBeanAttributeList().attribute(name);
75     }
76
77     public Attribute attribute(QName qname) {
78         return getBeanAttributeList().attribute(qname);
79     }
80
81     public Element addAttribute(String JavaDoc name, String JavaDoc value) {
82         Attribute attribute = attribute(name);
83
84         if (attribute != null) {
85             attribute.setValue(value);
86         }
87
88         return this;
89     }
90
91     public Element addAttribute(QName qName, String JavaDoc value) {
92         Attribute attribute = attribute(qName);
93
94         if (attribute != null) {
95             attribute.setValue(value);
96         }
97
98         return this;
99     }
100
101     public void setAttributes(List JavaDoc attributes) {
102         throw new UnsupportedOperationException JavaDoc("Not implemented yet.");
103     }
104
105     // Method overridden from AbstractElement
106
public void setAttributes(Attributes JavaDoc attributes,
107             NamespaceStack namespaceStack, boolean noNamespaceAttributes) {
108         String JavaDoc className = attributes.getValue("class");
109
110         if (className != null) {
111             try {
112                 Class JavaDoc beanClass = Class.forName(className, true,
113                         BeanElement.class.getClassLoader());
114                 this.setData(beanClass.newInstance());
115
116                 for (int i = 0; i < attributes.getLength(); i++) {
117                     String JavaDoc attributeName = attributes.getLocalName(i);
118
119                     if (!"class".equalsIgnoreCase(attributeName)) {
120                         addAttribute(attributeName, attributes.getValue(i));
121                     }
122                 }
123             } catch (Exception JavaDoc ex) {
124                 // What to do here?
125
((BeanDocumentFactory) this.getDocumentFactory())
126                         .handleException(ex);
127             }
128         } else {
129             super.setAttributes(attributes, namespaceStack,
130                     noNamespaceAttributes);
131         }
132     }
133
134     // Implementation methods
135
// -------------------------------------------------------------------------
136
protected DocumentFactory getDocumentFactory() {
137         return DOCUMENT_FACTORY;
138     }
139
140     protected BeanAttributeList getBeanAttributeList() {
141         return (BeanAttributeList) attributeList();
142     }
143
144     /**
145      * A Factory Method pattern which lazily creates a List implementation used
146      * to store content
147      *
148      * @return DOCUMENT ME!
149      */

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

195
Popular Tags