KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > ObjectCreateRule


1 /* $Id: ObjectCreateRule.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester;
20
21
22 import org.xml.sax.Attributes JavaDoc;
23
24
25 /**
26  * Rule implementation that creates a new object and pushes it
27  * onto the object stack. When the element is complete, the
28  * object will be popped
29  */

30
31 public class ObjectCreateRule extends Rule {
32
33
34     // ----------------------------------------------------------- Constructors
35

36
37     /**
38      * Construct an object create rule with the specified class name.
39      *
40      * @param digester The associated Digester
41      * @param className Java class name of the object to be created
42      *
43      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
44      * Use {@link #ObjectCreateRule(String className)} instead.
45      */

46     public ObjectCreateRule(Digester digester, String JavaDoc className) {
47
48         this(className);
49
50     }
51
52
53     /**
54      * Construct an object create rule with the specified class.
55      *
56      * @param digester The associated Digester
57      * @param clazz Java class name of the object to be created
58      *
59      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
60      * Use {@link #ObjectCreateRule(Class clazz)} instead.
61      */

62     public ObjectCreateRule(Digester digester, Class JavaDoc clazz) {
63
64         this(clazz);
65
66     }
67
68
69     /**
70      * Construct an object create rule with the specified class name and an
71      * optional attribute name containing an override.
72      *
73      * @param digester The associated Digester
74      * @param className Java class name of the object to be created
75      * @param attributeName Attribute name which, if present, contains an
76      * override of the class name to create
77      *
78      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
79      * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
80      */

81     public ObjectCreateRule(Digester digester, String JavaDoc className,
82                             String JavaDoc attributeName) {
83
84         this (className, attributeName);
85
86     }
87
88
89     /**
90      * Construct an object create rule with the specified class and an
91      * optional attribute name containing an override.
92      *
93      * @param digester The associated Digester
94      * @param attributeName Attribute name which, if present, contains an
95      * @param clazz Java class name of the object to be created
96      * override of the class name to create
97      *
98      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
99      * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
100      */

101     public ObjectCreateRule(Digester digester,
102                             String JavaDoc attributeName,
103                             Class JavaDoc clazz) {
104
105         this(attributeName, clazz);
106
107     }
108
109     /**
110      * Construct an object create rule with the specified class name.
111      *
112      * @param className Java class name of the object to be created
113      */

114     public ObjectCreateRule(String JavaDoc className) {
115
116         this(className, (String JavaDoc) null);
117
118     }
119
120
121     /**
122      * Construct an object create rule with the specified class.
123      *
124      * @param clazz Java class name of the object to be created
125      */

126     public ObjectCreateRule(Class JavaDoc clazz) {
127
128         this(clazz.getName(), (String JavaDoc) null);
129
130     }
131
132
133     /**
134      * Construct an object create rule with the specified class name and an
135      * optional attribute name containing an override.
136      *
137      * @param className Java class name of the object to be created
138      * @param attributeName Attribute name which, if present, contains an
139      * override of the class name to create
140      */

141     public ObjectCreateRule(String JavaDoc className,
142                             String JavaDoc attributeName) {
143
144         this.className = className;
145         this.attributeName = attributeName;
146
147     }
148
149
150     /**
151      * Construct an object create rule with the specified class and an
152      * optional attribute name containing an override.
153      *
154      * @param attributeName Attribute name which, if present, contains an
155      * @param clazz Java class name of the object to be created
156      * override of the class name to create
157      */

158     public ObjectCreateRule(String JavaDoc attributeName,
159                             Class JavaDoc clazz) {
160
161         this(clazz.getName(), attributeName);
162
163     }
164
165     // ----------------------------------------------------- Instance Variables
166

167
168     /**
169      * The attribute containing an override class name if it is present.
170      */

171     protected String JavaDoc attributeName = null;
172
173
174     /**
175      * The Java class name of the object to be created.
176      */

177     protected String JavaDoc className = null;
178
179
180     // --------------------------------------------------------- Public Methods
181

182
183     /**
184      * Process the beginning of this element.
185      *
186      * @param attributes The attribute list of this element
187      */

188     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
189
190         // Identify the name of the class to instantiate
191
String JavaDoc realClassName = className;
192         if (attributeName != null) {
193             String JavaDoc value = attributes.getValue(attributeName);
194             if (value != null) {
195                 realClassName = value;
196             }
197         }
198         if (digester.log.isDebugEnabled()) {
199             digester.log.debug("[ObjectCreateRule]{" + digester.match +
200                     "}New " + realClassName);
201         }
202
203         // Instantiate the new object and push it on the context stack
204
Class JavaDoc clazz = digester.getClassLoader().loadClass(realClassName);
205         Object JavaDoc instance = clazz.newInstance();
206         digester.push(instance);
207
208     }
209
210
211     /**
212      * Process the end of this element.
213      */

214     public void end() throws Exception JavaDoc {
215
216         Object JavaDoc top = digester.pop();
217         if (digester.log.isDebugEnabled()) {
218             digester.log.debug("[ObjectCreateRule]{" + digester.match +
219                     "} Pop " + top.getClass().getName());
220         }
221
222     }
223
224
225     /**
226      * Render a printable version of this Rule.
227      */

228     public String JavaDoc toString() {
229
230         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ObjectCreateRule[");
231         sb.append("className=");
232         sb.append(className);
233         sb.append(", attributeName=");
234         sb.append(attributeName);
235         sb.append("]");
236         return (sb.toString());
237
238     }
239
240
241 }
242
Popular Tags