1. /* $Id: ObjectCreateRule.java,v 1.19 2004/05/10 06:30:06 skitching Exp $
  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. package org.apache.commons.digester;
  18. import org.xml.sax.Attributes;
  19. /**
  20. * Rule implementation that creates a new object and pushes it
  21. * onto the object stack. When the element is complete, the
  22. * object will be popped
  23. */
  24. public class ObjectCreateRule extends Rule {
  25. // ----------------------------------------------------------- Constructors
  26. /**
  27. * Construct an object create rule with the specified class name.
  28. *
  29. * @param digester The associated Digester
  30. * @param className Java class name of the object to be created
  31. *
  32. * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
  33. * Use {@link #ObjectCreateRule(String className)} instead.
  34. */
  35. public ObjectCreateRule(Digester digester, String className) {
  36. this(className);
  37. }
  38. /**
  39. * Construct an object create rule with the specified class.
  40. *
  41. * @param digester The associated Digester
  42. * @param clazz Java class name of the object to be created
  43. *
  44. * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
  45. * Use {@link #ObjectCreateRule(Class clazz)} instead.
  46. */
  47. public ObjectCreateRule(Digester digester, Class clazz) {
  48. this(clazz);
  49. }
  50. /**
  51. * Construct an object create rule with the specified class name and an
  52. * optional attribute name containing an override.
  53. *
  54. * @param digester The associated Digester
  55. * @param className Java class name of the object to be created
  56. * @param attributeName Attribute name which, if present, contains an
  57. * override of the class name to create
  58. *
  59. * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
  60. * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
  61. */
  62. public ObjectCreateRule(Digester digester, String className,
  63. String attributeName) {
  64. this (className, attributeName);
  65. }
  66. /**
  67. * Construct an object create rule with the specified class and an
  68. * optional attribute name containing an override.
  69. *
  70. * @param digester The associated Digester
  71. * @param attributeName Attribute name which, if present, contains an
  72. * @param clazz Java class name of the object to be created
  73. * override of the class name to create
  74. *
  75. * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
  76. * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead.
  77. */
  78. public ObjectCreateRule(Digester digester,
  79. String attributeName,
  80. Class clazz) {
  81. this(attributeName, clazz);
  82. }
  83. /**
  84. * Construct an object create rule with the specified class name.
  85. *
  86. * @param className Java class name of the object to be created
  87. */
  88. public ObjectCreateRule(String className) {
  89. this(className, (String) null);
  90. }
  91. /**
  92. * Construct an object create rule with the specified class.
  93. *
  94. * @param clazz Java class name of the object to be created
  95. */
  96. public ObjectCreateRule(Class clazz) {
  97. this(clazz.getName(), (String) null);
  98. }
  99. /**
  100. * Construct an object create rule with the specified class name and an
  101. * optional attribute name containing an override.
  102. *
  103. * @param className Java class name of the object to be created
  104. * @param attributeName Attribute name which, if present, contains an
  105. * override of the class name to create
  106. */
  107. public ObjectCreateRule(String className,
  108. String attributeName) {
  109. this.className = className;
  110. this.attributeName = attributeName;
  111. }
  112. /**
  113. * Construct an object create rule with the specified class and an
  114. * optional attribute name containing an override.
  115. *
  116. * @param attributeName Attribute name which, if present, contains an
  117. * @param clazz Java class name of the object to be created
  118. * override of the class name to create
  119. */
  120. public ObjectCreateRule(String attributeName,
  121. Class clazz) {
  122. this(clazz.getName(), attributeName);
  123. }
  124. // ----------------------------------------------------- Instance Variables
  125. /**
  126. * The attribute containing an override class name if it is present.
  127. */
  128. protected String attributeName = null;
  129. /**
  130. * The Java class name of the object to be created.
  131. */
  132. protected String className = null;
  133. // --------------------------------------------------------- Public Methods
  134. /**
  135. * Process the beginning of this element.
  136. *
  137. * @param attributes The attribute list of this element
  138. */
  139. public void begin(Attributes attributes) throws Exception {
  140. // Identify the name of the class to instantiate
  141. String realClassName = className;
  142. if (attributeName != null) {
  143. String value = attributes.getValue(attributeName);
  144. if (value != null) {
  145. realClassName = value;
  146. }
  147. }
  148. if (digester.log.isDebugEnabled()) {
  149. digester.log.debug("[ObjectCreateRule]{" + digester.match +
  150. "}New " + realClassName);
  151. }
  152. // Instantiate the new object and push it on the context stack
  153. Class clazz = digester.getClassLoader().loadClass(realClassName);
  154. Object instance = clazz.newInstance();
  155. digester.push(instance);
  156. }
  157. /**
  158. * Process the end of this element.
  159. */
  160. public void end() throws Exception {
  161. Object top = digester.pop();
  162. if (digester.log.isDebugEnabled()) {
  163. digester.log.debug("[ObjectCreateRule]{" + digester.match +
  164. "} Pop " + top.getClass().getName());
  165. }
  166. }
  167. /**
  168. * Render a printable version of this Rule.
  169. */
  170. public String toString() {
  171. StringBuffer sb = new StringBuffer("ObjectCreateRule[");
  172. sb.append("className=");
  173. sb.append(className);
  174. sb.append(", attributeName=");
  175. sb.append(attributeName);
  176. sb.append("]");
  177. return (sb.toString());
  178. }
  179. }