1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java,v 1.10 2003/01/08 22:07:21 rdonkin Exp $
3 * $Revision: 1.10 $
4 * $Date: 2003/01/08 22:07:21 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: BeanReader.java,v 1.10 2003/01/08 22:07:21 rdonkin Exp $
61 */
62 package org.apache.commons.betwixt.io;
63
64 import java.beans.IntrospectionException;
65 import java.util.HashSet;
66 import java.util.Set;
67
68 import javax.xml.parsers.SAXParser;
69
70 import org.apache.commons.betwixt.ElementDescriptor;
71 import org.apache.commons.betwixt.XMLBeanInfo;
72 import org.apache.commons.betwixt.XMLIntrospector;
73 import org.apache.commons.digester.Digester;
74 import org.apache.commons.digester.Rule;
75 import org.apache.commons.logging.Log;
76 import org.apache.commons.logging.LogFactory;
77 import org.xml.sax.XMLReader;
78
79 /*** <p><code>BeanReader</code> reads a tree of beans from an XML document.</p>
80 *
81 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
82 * @version $Revision: 1.10 $
83 */
84 public class BeanReader extends Digester {
85
86 /*** Introspector used */
87 private XMLIntrospector introspector = new XMLIntrospector();
88 /*** Log used for logging (Doh!) */
89 private Log log = LogFactory.getLog( BeanReader.class );
90 /*** The registered classes */
91 private Set registeredClasses = new HashSet();
92 /*** Should the reader use <code>ID</code>'s to match */
93 private boolean matchIDs = true;
94
95 /***
96 * Construct a new BeanReader with default properties.
97 */
98 public BeanReader() {
99 }
100
101 /***
102 * Construct a new BeanReader, allowing a SAXParser to be passed in. This
103 * allows BeanReader to be used in environments which are unfriendly to
104 * JAXP1.1 (such as WebLogic 6.0). Thanks for the request to change go to
105 * James House (james@interobjective.com). This may help in places where
106 * you are able to load JAXP 1.1 classes yourself.
107 *
108 * @param parser use this <code>SAXParser</code>
109 */
110 public BeanReader(SAXParser parser) {
111 super(parser);
112 }
113
114 /***
115 * Construct a new BeanReader, allowing an XMLReader to be passed in. This
116 * allows BeanReader to be used in environments which are unfriendly to
117 * JAXP1.1 (such as WebLogic 6.0). Note that if you use this option you
118 * have to configure namespace and validation support yourself, as these
119 * properties only affect the SAXParser and emtpy constructor.
120 *
121 * @param reader use this <code>XMLReader</code> as source for SAX events
122 */
123 public BeanReader(XMLReader reader) {
124 super(reader);
125 }
126
127
128 /***
129 * Registers a bean class which is used by the reader
130 * to deduce the digester rules.
131 *
132 * @param beanClass the <code>Class</code> to be registered
133 * @throws IntrospectionException if the bean introspection fails
134 */
135 public void registerBeanClass(Class beanClass) throws IntrospectionException {
136 if ( ! registeredClasses.contains( beanClass ) ) {
137 if ( log.isTraceEnabled() ) {
138 log.trace( "Registering class " + beanClass );
139 }
140 registeredClasses.add( beanClass );
141
142 // introspect and find the ElementDescriptor to use as the root
143 XMLBeanInfo xmlInfo = introspector.introspect( beanClass );
144 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor();
145
146 String path = elementDescriptor.getQualifiedName();
147 if (log.isTraceEnabled()) {
148 log.trace("Added path: " + path + ", mapped to: " + beanClass.getName());
149 }
150 addBeanCreateRule( path, elementDescriptor, beanClass );
151 addBeanCreateRule( "*/" + path, elementDescriptor, beanClass );
152 }
153 }
154
155 /***
156 * Registers a bean class at the given path expression
157 * which is used by the reader to deduce the digester rules.
158 *
159 * @param path the xml path expression where the class is to registered.
160 * This should be in digester path notation
161 * @param beanClass the <code>Class</code> to be registered
162 * @throws IntrospectionException if the bean introspection fails
163 */
164 public void registerBeanClass(String path, Class beanClass) throws IntrospectionException {
165 if ( ! registeredClasses.contains( beanClass ) ) {
166 registeredClasses.add( beanClass );
167
168 // introspect and find the ElementDescriptor to use as the root
169 XMLBeanInfo xmlInfo = introspector.introspect( beanClass );
170 ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor();
171
172 addBeanCreateRule( path, elementDescriptor, beanClass );
173 } else {
174 log.warn("Cannot add class " + beanClass.getName() + " since it already exists");
175 }
176 }
177
178 // Properties
179 //-------------------------------------------------------------------------
180
181 /***
182 * <p> Get the introspector used. </p>
183 *
184 * <p> The {@link XMLBeanInfo} used to map each bean is
185 * created by the <code>XMLIntrospector</code>.
186 * One way in which the mapping can be customized is by
187 * altering the <code>XMLIntrospector</code>. </p>
188 *
189 * @return the <code>XMLIntrospector</code> used for the introspection
190 */
191 public XMLIntrospector getXMLIntrospector() {
192 return introspector;
193 }
194
195
196 /***
197 * <p> Set the introspector to be used. </p>
198 *
199 * <p> The {@link XMLBeanInfo} used to map each bean is
200 * created by the <code>XMLIntrospector</code>.
201 * One way in which the mapping can be customized is by
202 * altering the <code>XMLIntrospector</code>. </p>
203 *
204 * @param introspector use this introspector
205 */
206 public void setXMLIntrospector(XMLIntrospector introspector) {
207 this.introspector = introspector;
208 }
209
210 /***
211 * <p> Get the current level for logging. </p>
212 *
213 * @return the <code>Log</code> implementation this class logs to
214 */
215 public Log getLog() {
216 return log;
217 }
218
219 /***
220 * <p> Set the current logging level. </p>
221 *
222 * @param log the <code>Log</code>implementation to use for logging
223 */
224 public void setLog(Log log) {
225 this.log = log;
226 setLogger(log);
227 }
228
229 /***
230 * Should the reader use <code>ID</code> attributes to match beans.
231 *
232 * @return true if <code>ID</code> and <code>IDREF</code>
233 * attributes should be used to match instances
234 */
235 public boolean getMatchIDs() {
236 return matchIDs;
237 }
238
239 /***
240 * Set whether the read should use <code>ID</code> attributes to match beans.
241 *
242 * @param matchIDs pass true if <code>ID</code>'s should be matched
243 */
244 public void setMatchIDs(boolean matchIDs) {
245 this.matchIDs = matchIDs;
246 }
247
248 // Implementation methods
249 //-------------------------------------------------------------------------
250
251 /***
252 * Adds a new bean create rule for the specified path
253 *
254 * @param path the digester path at which this rule should be added
255 * @param elementDescriptor the <code>ElementDescriptor</code> describes the expected element
256 * @param beanClass the <code>Class</code> of the bean created by this rule
257 */
258 protected void addBeanCreateRule(
259 String path,
260 ElementDescriptor elementDescriptor,
261 Class beanClass ) {
262 Rule rule = new BeanCreateRule( elementDescriptor, beanClass, path + "/" , matchIDs);
263 addRule( path, rule );
264
265 if ( log.isDebugEnabled() ) {
266 log.debug( "Added root rule to path: " + path + " rule: " + rule );
267 }
268 }
269
270 }
This page was automatically generated by Maven