001: /* $Id: ObjectCreateRule.java 471661 2006-11-06 08:09:25Z skitching $
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: package org.apache.commons.digester;
020:
021: import org.xml.sax.Attributes;
022:
023: /**
024: * Rule implementation that creates a new object and pushes it
025: * onto the object stack. When the element is complete, the
026: * object will be popped
027: */
028:
029: public class ObjectCreateRule extends Rule {
030:
031: // ----------------------------------------------------------- Constructors
032:
033: /**
034: * Construct an object create rule with the specified class name.
035: *
036: * @param digester The associated Digester
037: * @param className Java class name of the object to be created
038: *
039: * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
040: * Use {@link #ObjectCreateRule(String className)} instead.
041: */
042: public ObjectCreateRule(Digester digester, String className) {
043:
044: this (className);
045:
046: }
047:
048: /**
049: * Construct an object create rule with the specified class.
050: *
051: * @param digester The associated Digester
052: * @param clazz Java class name of the object to be created
053: *
054: * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
055: * Use {@link #ObjectCreateRule(Class clazz)} instead.
056: */
057: public ObjectCreateRule(Digester digester, Class clazz) {
058:
059: this (clazz);
060:
061: }
062:
063: /**
064: * Construct an object create rule with the specified class name and an
065: * optional attribute name containing an override.
066: *
067: * @param digester The associated Digester
068: * @param className Java class name of the object to be created
069: * @param attributeName Attribute name which, if present, contains an
070: * override of the class name to create
071: *
072: * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
073: * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
074: */
075: public ObjectCreateRule(Digester digester, String className,
076: String attributeName) {
077:
078: this (className, attributeName);
079:
080: }
081:
082: /**
083: * Construct an object create rule with the specified class and an
084: * optional attribute name containing an override.
085: *
086: * @param digester The associated Digester
087: * @param attributeName Attribute name which, if present, contains an
088: * @param clazz Java class name of the object to be created
089: * override of the class name to create
090: *
091: * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
092: * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
093: */
094: public ObjectCreateRule(Digester digester, String attributeName,
095: Class clazz) {
096:
097: this (attributeName, clazz);
098:
099: }
100:
101: /**
102: * Construct an object create rule with the specified class name.
103: *
104: * @param className Java class name of the object to be created
105: */
106: public ObjectCreateRule(String className) {
107:
108: this (className, (String) null);
109:
110: }
111:
112: /**
113: * Construct an object create rule with the specified class.
114: *
115: * @param clazz Java class name of the object to be created
116: */
117: public ObjectCreateRule(Class clazz) {
118:
119: this (clazz.getName(), (String) null);
120:
121: }
122:
123: /**
124: * Construct an object create rule with the specified class name and an
125: * optional attribute name containing an override.
126: *
127: * @param className Java class name of the object to be created
128: * @param attributeName Attribute name which, if present, contains an
129: * override of the class name to create
130: */
131: public ObjectCreateRule(String className, String attributeName) {
132:
133: this .className = className;
134: this .attributeName = attributeName;
135:
136: }
137:
138: /**
139: * Construct an object create rule with the specified class and an
140: * optional attribute name containing an override.
141: *
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: public ObjectCreateRule(String attributeName, Class clazz) {
147:
148: this (clazz.getName(), attributeName);
149:
150: }
151:
152: // ----------------------------------------------------- Instance Variables
153:
154: /**
155: * The attribute containing an override class name if it is present.
156: */
157: protected String attributeName = null;
158:
159: /**
160: * The Java class name of the object to be created.
161: */
162: protected String className = null;
163:
164: // --------------------------------------------------------- Public Methods
165:
166: /**
167: * Process the beginning of this element.
168: *
169: * @param attributes The attribute list of this element
170: */
171: public void begin(Attributes attributes) throws Exception {
172:
173: // Identify the name of the class to instantiate
174: String realClassName = className;
175: if (attributeName != null) {
176: String value = attributes.getValue(attributeName);
177: if (value != null) {
178: realClassName = value;
179: }
180: }
181: if (digester.log.isDebugEnabled()) {
182: digester.log.debug("[ObjectCreateRule]{" + digester.match
183: + "}New " + realClassName);
184: }
185:
186: // Instantiate the new object and push it on the context stack
187: Class clazz = digester.getClassLoader()
188: .loadClass(realClassName);
189: Object instance = clazz.newInstance();
190: digester.push(instance);
191:
192: }
193:
194: /**
195: * Process the end of this element.
196: */
197: public void end() throws Exception {
198:
199: Object top = digester.pop();
200: if (digester.log.isDebugEnabled()) {
201: digester.log.debug("[ObjectCreateRule]{" + digester.match
202: + "} Pop " + top.getClass().getName());
203: }
204:
205: }
206:
207: /**
208: * Render a printable version of this Rule.
209: */
210: public String toString() {
211:
212: StringBuffer sb = new StringBuffer("ObjectCreateRule[");
213: sb.append("className=");
214: sb.append(className);
215: sb.append(", attributeName=");
216: sb.append(attributeName);
217: sb.append("]");
218: return (sb.toString());
219:
220: }
221:
222: }
|