~ubuntu-branches/ubuntu/maverick/libcommons-digester-java/maverick

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/digester/ObjectCreateRule.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2004-08-13 01:59:24 UTC
  • Revision ID: james.westby@ubuntu.com-20040813015924-kxkbfi0a1u5cbxbr
Tags: 1.5.0.1-3
rebuild with J2SDK1.3 comparible mode.(closes: #265253)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.13 $
 
4
 * $Date: 2003/02/02 16:09:53 $
 
5
 *
 
6
 * ====================================================================
 
7
 *
 
8
 * The Apache Software License, Version 1.1
 
9
 *
 
10
 * Copyright (c) 1999-2003 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
 */
 
61
 
 
62
 
 
63
package org.apache.commons.digester;
 
64
 
 
65
 
 
66
import org.xml.sax.Attributes;
 
67
 
 
68
 
 
69
/**
 
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
 
73
 *
 
74
 * @author Craig McClanahan
 
75
 * @author Scott Sanders
 
76
 * @version $Revision: 1.13 $ $Date: 2003/02/02 16:09:53 $
 
77
 */
 
78
 
 
79
public class ObjectCreateRule extends Rule {
 
80
 
 
81
 
 
82
    // ----------------------------------------------------------- Constructors
 
83
 
 
84
 
 
85
    /**
 
86
     * Construct an object create rule with the specified class name.
 
87
     *
 
88
     * @param digester The associated Digester
 
89
     * @param className Java class name of the object to be created
 
90
     *
 
91
     * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
 
92
     * Use {@link #ObjectCreateRule(String className)} instead.
 
93
     */
 
94
    public ObjectCreateRule(Digester digester, String className) {
 
95
 
 
96
        this(className);
 
97
 
 
98
    }
 
99
 
 
100
 
 
101
    /**
 
102
     * Construct an object create rule with the specified class.
 
103
     *
 
104
     * @param digester The associated Digester
 
105
     * @param clazz Java class name of the object to be created
 
106
     *
 
107
     * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
 
108
     * Use {@link #ObjectCreateRule(Class clazz)} instead.
 
109
     */
 
110
    public ObjectCreateRule(Digester digester, Class clazz) {
 
111
 
 
112
        this(clazz);
 
113
 
 
114
    }
 
115
 
 
116
 
 
117
    /**
 
118
     * Construct an object create rule with the specified class name and an
 
119
     * optional attribute name containing an override.
 
120
     *
 
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
 
125
     *
 
126
     * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
 
127
     * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
 
128
     */
 
129
    public ObjectCreateRule(Digester digester, String className,
 
130
                            String attributeName) {
 
131
 
 
132
        this (className, attributeName);
 
133
 
 
134
    }
 
135
 
 
136
 
 
137
    /**
 
138
     * Construct an object create rule with the specified class and an
 
139
     * optional attribute name containing an override.
 
140
     *
 
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
 
145
     *
 
146
     * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
 
147
     * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
 
148
     */
 
149
    public ObjectCreateRule(Digester digester,
 
150
                            String attributeName,
 
151
                            Class clazz) {
 
152
 
 
153
        this(attributeName, clazz);
 
154
 
 
155
    }
 
156
 
 
157
    /**
 
158
     * Construct an object create rule with the specified class name.
 
159
     *
 
160
     * @param className Java class name of the object to be created
 
161
     */
 
162
    public ObjectCreateRule(String className) {
 
163
 
 
164
        this(className, (String) null);
 
165
 
 
166
    }
 
167
 
 
168
 
 
169
    /**
 
170
     * Construct an object create rule with the specified class.
 
171
     *
 
172
     * @param clazz Java class name of the object to be created
 
173
     */
 
174
    public ObjectCreateRule(Class clazz) {
 
175
 
 
176
        this(clazz.getName(), (String) null);
 
177
 
 
178
    }
 
179
 
 
180
 
 
181
    /**
 
182
     * Construct an object create rule with the specified class name and an
 
183
     * optional attribute name containing an override.
 
184
     *
 
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
 
188
     */
 
189
    public ObjectCreateRule(String className,
 
190
                            String attributeName) {
 
191
 
 
192
        this.className = className;
 
193
        this.attributeName = attributeName;
 
194
 
 
195
    }
 
196
 
 
197
 
 
198
    /**
 
199
     * Construct an object create rule with the specified class and an
 
200
     * optional attribute name containing an override.
 
201
     *
 
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
 
205
     */
 
206
    public ObjectCreateRule(String attributeName,
 
207
                            Class clazz) {
 
208
 
 
209
        this(clazz.getName(), attributeName);
 
210
 
 
211
    }
 
212
 
 
213
    // ----------------------------------------------------- Instance Variables
 
214
 
 
215
 
 
216
    /**
 
217
     * The attribute containing an override class name if it is present.
 
218
     */
 
219
    protected String attributeName = null;
 
220
 
 
221
 
 
222
    /**
 
223
     * The Java class name of the object to be created.
 
224
     */
 
225
    protected String className = null;
 
226
 
 
227
 
 
228
    // --------------------------------------------------------- Public Methods
 
229
 
 
230
 
 
231
    /**
 
232
     * Process the beginning of this element.
 
233
     *
 
234
     * @param attributes The attribute list of this element
 
235
     */
 
236
    public void begin(Attributes attributes) throws Exception {
 
237
 
 
238
        // Identify the name of the class to instantiate
 
239
        String realClassName = className;
 
240
        if (attributeName != null) {
 
241
            String value = attributes.getValue(attributeName);
 
242
            if (value != null) {
 
243
                realClassName = value;
 
244
            }
 
245
        }
 
246
        if (digester.log.isDebugEnabled()) {
 
247
            digester.log.debug("[ObjectCreateRule]{" + digester.match +
 
248
                    "}New " + realClassName);
 
249
        }
 
250
 
 
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);
 
255
 
 
256
    }
 
257
 
 
258
 
 
259
    /**
 
260
     * Process the end of this element.
 
261
     */
 
262
    public void end() throws Exception {
 
263
 
 
264
        Object top = digester.pop();
 
265
        if (digester.log.isDebugEnabled()) {
 
266
            digester.log.debug("[ObjectCreateRule]{" + digester.match +
 
267
                    "} Pop " + top.getClass().getName());
 
268
        }
 
269
 
 
270
    }
 
271
 
 
272
 
 
273
    /**
 
274
     * Render a printable version of this Rule.
 
275
     */
 
276
    public String toString() {
 
277
 
 
278
        StringBuffer sb = new StringBuffer("ObjectCreateRule[");
 
279
        sb.append("className=");
 
280
        sb.append(className);
 
281
        sb.append(", attributeName=");
 
282
        sb.append(attributeName);
 
283
        sb.append("]");
 
284
        return (sb.toString());
 
285
 
 
286
    }
 
287
 
 
288
 
 
289
}