001 /* $Id: ObjectCreateRule.java 729106 2008-12-23 20:48:09Z rahul $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019
020 package org.apache.commons.digester;
021
022
023 import org.xml.sax.Attributes;
024
025
026 /**
027 * Rule implementation that creates a new object and pushes it
028 * onto the object stack. When the element is complete, the
029 * object will be popped
030 */
031
032 public class ObjectCreateRule extends Rule {
033
034
035 // ----------------------------------------------------------- Constructors
036
037
038 /**
039 * Construct an object create rule with the specified class name.
040 *
041 * @param digester The associated Digester
042 * @param className Java class name of the object to be created
043 *
044 * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
045 * Use {@link #ObjectCreateRule(String className)} instead.
046 */
047 public ObjectCreateRule(Digester digester, String className) {
048
049 this(className);
050
051 }
052
053
054 /**
055 * Construct an object create rule with the specified class.
056 *
057 * @param digester The associated Digester
058 * @param clazz Java class name of the object to be created
059 *
060 * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
061 * Use {@link #ObjectCreateRule(Class clazz)} instead.
062 */
063 public ObjectCreateRule(Digester digester, Class<?> clazz) {
064
065 this(clazz);
066
067 }
068
069
070 /**
071 * Construct an object create rule with the specified class name and an
072 * optional attribute name containing an override.
073 *
074 * @param digester The associated Digester
075 * @param className Java class name of the object to be created
076 * @param attributeName Attribute name which, if present, contains an
077 * override of the class name to create
078 *
079 * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
080 * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
081 */
082 public ObjectCreateRule(Digester digester, String className,
083 String attributeName) {
084
085 this (className, attributeName);
086
087 }
088
089
090 /**
091 * Construct an object create rule with the specified class and an
092 * optional attribute name containing an override.
093 *
094 * @param digester The associated Digester
095 * @param attributeName Attribute name which, if present, contains an
096 * @param clazz Java class name of the object to be created
097 * override of the class name to create
098 *
099 * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
100 * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
101 */
102 public ObjectCreateRule(Digester digester,
103 String attributeName,
104 Class<?> clazz) {
105
106 this(attributeName, clazz);
107
108 }
109
110 /**
111 * Construct an object create rule with the specified class name.
112 *
113 * @param className Java class name of the object to be created
114 */
115 public ObjectCreateRule(String className) {
116
117 this(className, (String) null);
118
119 }
120
121
122 /**
123 * Construct an object create rule with the specified class.
124 *
125 * @param clazz Java class name of the object to be created
126 */
127 public ObjectCreateRule(Class<?> clazz) {
128
129 this(clazz.getName(), (String) null);
130
131 }
132
133
134 /**
135 * Construct an object create rule with the specified class name and an
136 * optional attribute name containing an override.
137 *
138 * @param className Java class name of the object to be created
139 * @param attributeName Attribute name which, if present, contains an
140 * override of the class name to create
141 */
142 public ObjectCreateRule(String className,
143 String attributeName) {
144
145 this.className = className;
146 this.attributeName = attributeName;
147
148 }
149
150
151 /**
152 * Construct an object create rule with the specified class and an
153 * optional attribute name containing an override.
154 *
155 * @param attributeName Attribute name which, if present, contains an
156 * @param clazz Java class name of the object to be created
157 * override of the class name to create
158 */
159 public ObjectCreateRule(String attributeName,
160 Class<?> clazz) {
161
162 this(clazz.getName(), attributeName);
163
164 }
165
166 // ----------------------------------------------------- Instance Variables
167
168
169 /**
170 * The attribute containing an override class name if it is present.
171 */
172 protected String attributeName = null;
173
174
175 /**
176 * The Java class name of the object to be created.
177 */
178 protected String className = null;
179
180
181 // --------------------------------------------------------- Public Methods
182
183
184 /**
185 * Process the beginning of this element.
186 *
187 * @param attributes The attribute list of this element
188 */
189 public void begin(Attributes attributes) throws Exception {
190
191 // Identify the name of the class to instantiate
192 String realClassName = className;
193 if (attributeName != null) {
194 String value = attributes.getValue(attributeName);
195 if (value != null) {
196 realClassName = value;
197 }
198 }
199 if (digester.log.isDebugEnabled()) {
200 digester.log.debug("[ObjectCreateRule]{" + digester.match +
201 "}New " + realClassName);
202 }
203
204 // Instantiate the new object and push it on the context stack
205 Class<?> clazz = digester.getClassLoader().loadClass(realClassName);
206 Object instance = clazz.newInstance();
207 digester.push(instance);
208
209 }
210
211
212 /**
213 * Process the end of this element.
214 */
215 public void end() throws Exception {
216
217 Object top = digester.pop();
218 if (digester.log.isDebugEnabled()) {
219 digester.log.debug("[ObjectCreateRule]{" + digester.match +
220 "} Pop " + top.getClass().getName());
221 }
222
223 }
224
225
226 /**
227 * Render a printable version of this Rule.
228 */
229 public String toString() {
230
231 StringBuffer sb = new StringBuffer("ObjectCreateRule[");
232 sb.append("className=");
233 sb.append(className);
234 sb.append(", attributeName=");
235 sb.append(attributeName);
236 sb.append("]");
237 return (sb.toString());
238
239 }
240
241
242 }