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