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: BeanElement.java,v 1.13 2004/06/25 08:03:34 maartenc Exp $
8    */
9   
10  package org.dom4j.bean;
11  
12  import java.util.List;
13  
14  import org.xml.sax.Attributes;
15  
16  import org.dom4j.Attribute;
17  import org.dom4j.DocumentFactory;
18  import org.dom4j.Element;
19  import org.dom4j.Namespace;
20  import org.dom4j.QName;
21  import org.dom4j.tree.DefaultElement;
22  import org.dom4j.tree.NamespaceStack;
23  
24  
25  
26  /*** <p><code>BeanElement</code> uses a Java Bean to store its attributes.</p>
27    *
28    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29    * @version $Revision: 1.13 $
30    */
31  public class BeanElement extends DefaultElement {
32  
33      /*** The <code>DocumentFactory</code> instance used by default */
34      private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance();
35  
36      /*** The JavaBean which defines my attributes */
37      private Object bean;
38  
39  
40      public BeanElement(String name, Object bean) {
41          this( DOCUMENT_FACTORY.createQName(name), bean );
42      }
43  
44      public BeanElement(String name,Namespace namespace, Object bean) {
45          this( DOCUMENT_FACTORY.createQName(name, namespace), bean );
46      }
47  
48      public BeanElement(QName qname, Object bean) {
49          super( qname);
50          this.bean = bean;
51      }
52  
53      public BeanElement(QName qname) {
54          super( qname);
55      }
56  
57      /*** @return the JavaBean associated with this element
58        */
59      public Object getData() {
60          return bean;
61      }
62  
63      public void setData(Object bean) {
64          this.bean = bean;
65  
66          // force the attributeList to be lazily
67          // created next time an attribute related
68          // method is called again.
69          setAttributeList(null);
70      }
71  
72      public Attribute attribute(String name) {
73          return getBeanAttributeList().attribute(name);
74      }
75  
76      public Attribute attribute(QName qname) {
77          return getBeanAttributeList().attribute(qname);
78      }
79  
80      public Element addAttribute(String name, String value) {
81          Attribute attribute = attribute(name);
82          if (attribute != null ) {
83              attribute.setValue(value);
84          }
85          return this;
86      }
87  
88      public Element addAttribute(QName qName, String value) {
89          Attribute attribute = attribute(qName);
90          if (attribute != null ) {
91              attribute.setValue(value);
92          }
93          return this;
94      }
95  
96      public void setAttributes(List attributes) {
97          throw new UnsupportedOperationException( "setAttributes(List) is not supported yet!" );
98      }
99  
100 
101     //Method overridden from AbstractElement
102     public void setAttributes(Attributes attributes,
103                               NamespaceStack namespaceStack,
104                               boolean noNamespaceAttributes) {
105       String className = attributes.getValue("class");
106       if (className != null) {
107         try {
108           Class beanClass = Class.forName(className,
109                                           true,
110                                           BeanElement.class.getClassLoader());
111           this.setData(beanClass.newInstance());
112 
113           for( int i=0; i<attributes.getLength(); i++){
114             String attributeName = attributes.getLocalName(i);
115             if( !"class".equalsIgnoreCase(attributeName) ){
116               addAttribute(attributeName, attributes.getValue(i));
117             }
118           }
119         }
120         catch (Exception ex) {
121           //What to do here?
122           ( (BeanDocumentFactory)this.getDocumentFactory()).handleException(ex);
123         }
124       }
125       else {
126         super.setAttributes(attributes, namespaceStack, noNamespaceAttributes);
127       }
128     }
129 
130 
131 
132     // Implementation methods
133     //-------------------------------------------------------------------------
134     protected DocumentFactory getDocumentFactory() {
135         return DOCUMENT_FACTORY;
136     }
137 
138     protected BeanAttributeList getBeanAttributeList() {
139         return (BeanAttributeList) attributeList();
140     }
141 
142     /*** A Factory Method pattern which lazily creates
143       * a List implementation used to store content
144       */
145     protected List createAttributeList() {
146         return new BeanAttributeList(this);
147     }
148     protected List createAttributeList(int size) {
149         return new BeanAttributeList(this);
150     }
151 }
152 
153 
154 
155 
156 /*
157  * Redistribution and use of this software and associated documentation
158  * ("Software"), with or without modification, are permitted provided
159  * that the following conditions are met:
160  *
161  * 1. Redistributions of source code must retain copyright
162  *    statements and notices.  Redistributions must also contain a
163  *    copy of this document.
164  *
165  * 2. Redistributions in binary form must reproduce the
166  *    above copyright notice, this list of conditions and the
167  *    following disclaimer in the documentation and/or other
168  *    materials provided with the distribution.
169  *
170  * 3. The name "DOM4J" must not be used to endorse or promote
171  *    products derived from this Software without prior written
172  *    permission of MetaStuff, Ltd.  For written permission,
173  *    please contact dom4j-info@metastuff.com.
174  *
175  * 4. Products derived from this Software may not be called "DOM4J"
176  *    nor may "DOM4J" appear in their names without prior written
177  *    permission of MetaStuff, Ltd. DOM4J is a registered
178  *    trademark of MetaStuff, Ltd.
179  *
180  * 5. Due credit should be given to the DOM4J Project - 
181  *    http://dom4j.org/
182  *
183  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
184  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
185  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
186  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
187  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
188  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
189  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
190  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
191  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
192  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
193  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
194  * OF THE POSSIBILITY OF SUCH DAMAGE.
195  *
196  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
197  *
198  * $Id: BeanElement.java,v 1.13 2004/06/25 08:03:34 maartenc Exp $
199  */