|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BeanDocumentFactory.java | 16,7% | 26,3% | 42,9% | 28,1% |
|
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 org.dom4j.Attribute; | |
11 | import org.dom4j.DocumentFactory; | |
12 | import org.dom4j.Element; | |
13 | import org.dom4j.QName; | |
14 | import org.dom4j.tree.DefaultAttribute; | |
15 | ||
16 | import org.xml.sax.Attributes; | |
17 | ||
18 | /** | |
19 | * <p> | |
20 | * <code>BeanDocumentFactory</code> is a factory of DOM4J objects which may be | |
21 | * BeanElements which are backed by JavaBeans and their properties. | |
22 | * </p> | |
23 | * | |
24 | * <p> | |
25 | * The tree built allows full XPath expressions from anywhere on the tree. | |
26 | * </p> | |
27 | * | |
28 | * @author <a href="mailto:jstrachan@apache.org">James Strachan </a> | |
29 | * @version $Revision: 1.14 $ | |
30 | */ | |
31 | public class BeanDocumentFactory extends DocumentFactory { | |
32 | /** The Singleton instance */ | |
33 | private static BeanDocumentFactory singleton = new BeanDocumentFactory(); | |
34 | ||
35 | /** | |
36 | * <p> | |
37 | * Access to the singleton instance of this factory. | |
38 | * </p> | |
39 | * | |
40 | * @return the default singleon instance | |
41 | */ | |
42 | 3 | public static DocumentFactory getInstance() { |
43 | 3 | return singleton; |
44 | } | |
45 | ||
46 | // Factory methods | |
47 | 3 | public Element createElement(QName qname) { |
48 | 3 | Object bean = createBean(qname); |
49 | ||
50 | 3 | if (bean == null) { |
51 | 3 | return new BeanElement(qname); |
52 | } else { | |
53 | 0 | return new BeanElement(qname, bean); |
54 | } | |
55 | } | |
56 | ||
57 | 0 | public Element createElement(QName qname, Attributes attributes) { |
58 | 0 | Object bean = createBean(qname, attributes); |
59 | ||
60 | 0 | if (bean == null) { |
61 | 0 | return new BeanElement(qname); |
62 | } else { | |
63 | 0 | return new BeanElement(qname, bean); |
64 | } | |
65 | } | |
66 | ||
67 | 0 | public Attribute createAttribute(Element owner, QName qname, String value) { |
68 | 0 | return new DefaultAttribute(qname, value); |
69 | } | |
70 | ||
71 | // Implementation methods | |
72 | 3 | protected Object createBean(QName qname) { |
73 | 3 | return null; |
74 | } | |
75 | ||
76 | 0 | protected Object createBean(QName qname, Attributes attributes) { |
77 | 0 | String value = attributes.getValue("class"); |
78 | ||
79 | 0 | if (value != null) { |
80 | 0 | try { |
81 | 0 | Class beanClass = Class.forName(value, true, |
82 | BeanDocumentFactory.class.getClassLoader()); | |
83 | ||
84 | 0 | return beanClass.newInstance(); |
85 | } catch (Exception e) { | |
86 | 0 | handleException(e); |
87 | } | |
88 | } | |
89 | ||
90 | 0 | return null; |
91 | } | |
92 | ||
93 | 0 | protected void handleException(Exception e) { |
94 | // ignore introspection exceptions | |
95 | 0 | System.out.println("#### Warning: couldn't create bean: " + e); |
96 | } | |
97 | } | |
98 | ||
99 | /* | |
100 | * Redistribution and use of this software and associated documentation | |
101 | * ("Software"), with or without modification, are permitted provided that the | |
102 | * following conditions are met: | |
103 | * | |
104 | * 1. Redistributions of source code must retain copyright statements and | |
105 | * notices. Redistributions must also contain a copy of this document. | |
106 | * | |
107 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
108 | * this list of conditions and the following disclaimer in the documentation | |
109 | * and/or other materials provided with the distribution. | |
110 | * | |
111 | * 3. The name "DOM4J" must not be used to endorse or promote products derived | |
112 | * from this Software without prior written permission of MetaStuff, Ltd. For | |
113 | * written permission, please contact dom4j-info@metastuff.com. | |
114 | * | |
115 | * 4. Products derived from this Software may not be called "DOM4J" nor may | |
116 | * "DOM4J" appear in their names without prior written permission of MetaStuff, | |
117 | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd. | |
118 | * | |
119 | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org | |
120 | * | |
121 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND | |
122 | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
123 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
124 | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE | |
125 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
126 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
127 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
128 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
129 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
130 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
131 | * POSSIBILITY OF SUCH DAMAGE. | |
132 | * | |
133 | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. | |
134 | */ |
|