2
* $Header: /home/cvspublic/jakarta-commons/digester/src/java/org/apache/commons/digester/ObjectCreateRule.java,v 1.13 2003/02/02 16:09:53 rdonkin Exp $
4
* $Date: 2003/02/02 16:09:53 $
6
* ====================================================================
8
* The Apache Software License, Version 1.1
10
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
13
* Redistribution and use in source and binary forms, with or without
14
* modification, are permitted provided that the following conditions
17
* 1. Redistributions of source code must retain the above copyright
18
* notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
53
* ====================================================================
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/>.
63
package org.apache.commons.digester;
66
import org.xml.sax.Attributes;
70
* Rule implementation that creates a new object and pushes it
71
* onto the object stack. When the element is complete, the
72
* object will be popped
74
* @author Craig McClanahan
75
* @author Scott Sanders
76
* @version $Revision: 1.13 $ $Date: 2003/02/02 16:09:53 $
79
public class ObjectCreateRule extends Rule {
82
// ----------------------------------------------------------- Constructors
86
* Construct an object create rule with the specified class name.
88
* @param digester The associated Digester
89
* @param className Java class name of the object to be created
91
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
92
* Use {@link #ObjectCreateRule(String className)} instead.
94
public ObjectCreateRule(Digester digester, String className) {
102
* Construct an object create rule with the specified class.
104
* @param digester The associated Digester
105
* @param clazz Java class name of the object to be created
107
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
108
* Use {@link #ObjectCreateRule(Class clazz)} instead.
110
public ObjectCreateRule(Digester digester, Class clazz) {
118
* Construct an object create rule with the specified class name and an
119
* optional attribute name containing an override.
121
* @param digester The associated Digester
122
* @param className Java class name of the object to be created
123
* @param attributeName Attribute name which, if present, contains an
124
* override of the class name to create
126
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
127
* Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
129
public ObjectCreateRule(Digester digester, String className,
130
String attributeName) {
132
this (className, attributeName);
138
* Construct an object create rule with the specified class and an
139
* optional attribute name containing an override.
141
* @param digester The associated Digester
142
* @param attributeName Attribute name which, if present, contains an
143
* @param clazz Java class name of the object to be created
144
* override of the class name to create
146
* @deprecated The digester instance is now set in the {@link Digester#addRule} method.
147
* Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
149
public ObjectCreateRule(Digester digester,
150
String attributeName,
153
this(attributeName, clazz);
158
* Construct an object create rule with the specified class name.
160
* @param className Java class name of the object to be created
162
public ObjectCreateRule(String className) {
164
this(className, (String) null);
170
* Construct an object create rule with the specified class.
172
* @param clazz Java class name of the object to be created
174
public ObjectCreateRule(Class clazz) {
176
this(clazz.getName(), (String) null);
182
* Construct an object create rule with the specified class name and an
183
* optional attribute name containing an override.
185
* @param className Java class name of the object to be created
186
* @param attributeName Attribute name which, if present, contains an
187
* override of the class name to create
189
public ObjectCreateRule(String className,
190
String attributeName) {
192
this.className = className;
193
this.attributeName = attributeName;
199
* Construct an object create rule with the specified class and an
200
* optional attribute name containing an override.
202
* @param attributeName Attribute name which, if present, contains an
203
* @param clazz Java class name of the object to be created
204
* override of the class name to create
206
public ObjectCreateRule(String attributeName,
209
this(clazz.getName(), attributeName);
213
// ----------------------------------------------------- Instance Variables
217
* The attribute containing an override class name if it is present.
219
protected String attributeName = null;
223
* The Java class name of the object to be created.
225
protected String className = null;
228
// --------------------------------------------------------- Public Methods
232
* Process the beginning of this element.
234
* @param attributes The attribute list of this element
236
public void begin(Attributes attributes) throws Exception {
238
// Identify the name of the class to instantiate
239
String realClassName = className;
240
if (attributeName != null) {
241
String value = attributes.getValue(attributeName);
243
realClassName = value;
246
if (digester.log.isDebugEnabled()) {
247
digester.log.debug("[ObjectCreateRule]{" + digester.match +
248
"}New " + realClassName);
251
// Instantiate the new object and push it on the context stack
252
Class clazz = digester.getClassLoader().loadClass(realClassName);
253
Object instance = clazz.newInstance();
254
digester.push(instance);
260
* Process the end of this element.
262
public void end() throws Exception {
264
Object top = digester.pop();
265
if (digester.log.isDebugEnabled()) {
266
digester.log.debug("[ObjectCreateRule]{" + digester.match +
267
"} Pop " + top.getClass().getName());
274
* Render a printable version of this Rule.
276
public String toString() {
278
StringBuffer sb = new StringBuffer("ObjectCreateRule[");
279
sb.append("className=");
280
sb.append(className);
281
sb.append(", attributeName=");
282
sb.append(attributeName);
284
return (sb.toString());