001 /* $Id: ObjectCreateRule.java 992060 2010-09-02 19:09:47Z simonetripodi $ 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 @Deprecated 048 public ObjectCreateRule(Digester digester, String className) { 049 050 this(className); 051 052 } 053 054 055 /** 056 * Construct an object create rule with the specified class. 057 * 058 * @param digester The associated Digester 059 * @param clazz Java class name of the object to be created 060 * 061 * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 062 * Use {@link #ObjectCreateRule(Class clazz)} instead. 063 */ 064 @Deprecated 065 public ObjectCreateRule(Digester digester, Class<?> clazz) { 066 067 this(clazz); 068 069 } 070 071 072 /** 073 * Construct an object create rule with the specified class name and an 074 * optional attribute name containing an override. 075 * 076 * @param digester The associated Digester 077 * @param className Java class name of the object to be created 078 * @param attributeName Attribute name which, if present, contains an 079 * override of the class name to create 080 * 081 * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 082 * Use {@link #ObjectCreateRule(String className, String attributeName)} instead. 083 */ 084 @Deprecated 085 public ObjectCreateRule(Digester digester, String className, 086 String attributeName) { 087 088 this (className, attributeName); 089 090 } 091 092 093 /** 094 * Construct an object create rule with the specified class and an 095 * optional attribute name containing an override. 096 * 097 * @param digester The associated Digester 098 * @param attributeName Attribute name which, if present, contains an 099 * @param clazz Java class name of the object to be created 100 * override of the class name to create 101 * 102 * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 103 * Use {@link #ObjectCreateRule(String attributeName, Class clazz)} instead. 104 */ 105 @Deprecated 106 public ObjectCreateRule(Digester digester, 107 String attributeName, 108 Class<?> clazz) { 109 110 this(attributeName, clazz); 111 112 } 113 114 /** 115 * Construct an object create rule with the specified class name. 116 * 117 * @param className Java class name of the object to be created 118 */ 119 public ObjectCreateRule(String className) { 120 121 this(className, (String) null); 122 123 } 124 125 126 /** 127 * Construct an object create rule with the specified class. 128 * 129 * @param clazz Java class name of the object to be created 130 */ 131 public ObjectCreateRule(Class<?> clazz) { 132 133 this(clazz.getName(), (String) null); 134 135 } 136 137 138 /** 139 * Construct an object create rule with the specified class name and an 140 * optional attribute name containing an override. 141 * 142 * @param className Java class name of the object to be created 143 * @param attributeName Attribute name which, if present, contains an 144 * override of the class name to create 145 */ 146 public ObjectCreateRule(String className, 147 String attributeName) { 148 149 this.className = className; 150 this.attributeName = attributeName; 151 152 } 153 154 155 /** 156 * Construct an object create rule with the specified class and an 157 * optional attribute name containing an override. 158 * 159 * @param attributeName Attribute name which, if present, contains an 160 * @param clazz Java class name of the object to be created 161 * override of the class name to create 162 */ 163 public ObjectCreateRule(String attributeName, 164 Class<?> clazz) { 165 166 this(clazz.getName(), attributeName); 167 168 } 169 170 // ----------------------------------------------------- Instance Variables 171 172 173 /** 174 * The attribute containing an override class name if it is present. 175 */ 176 protected String attributeName = null; 177 178 179 /** 180 * The Java class name of the object to be created. 181 */ 182 protected String className = null; 183 184 185 // --------------------------------------------------------- Public Methods 186 187 188 /** 189 * Process the beginning of this element. 190 * 191 * @param attributes The attribute list of this element 192 */ 193 @Override 194 public void begin(Attributes attributes) throws Exception { 195 196 // Identify the name of the class to instantiate 197 String realClassName = className; 198 if (attributeName != null) { 199 String value = attributes.getValue(attributeName); 200 if (value != null) { 201 realClassName = value; 202 } 203 } 204 if (digester.log.isDebugEnabled()) { 205 digester.log.debug("[ObjectCreateRule]{" + digester.match + 206 "}New " + realClassName); 207 } 208 209 // Instantiate the new object and push it on the context stack 210 Class<?> clazz = digester.getClassLoader().loadClass(realClassName); 211 Object instance = clazz.newInstance(); 212 digester.push(instance); 213 214 } 215 216 217 /** 218 * Process the end of this element. 219 */ 220 @Override 221 public void end() throws Exception { 222 223 Object top = digester.pop(); 224 if (digester.log.isDebugEnabled()) { 225 digester.log.debug("[ObjectCreateRule]{" + digester.match + 226 "} Pop " + top.getClass().getName()); 227 } 228 229 } 230 231 232 /** 233 * Render a printable version of this Rule. 234 */ 235 @Override 236 public String toString() { 237 238 StringBuffer sb = new StringBuffer("ObjectCreateRule["); 239 sb.append("className="); 240 sb.append(className); 241 sb.append(", attributeName="); 242 sb.append(attributeName); 243 sb.append("]"); 244 return (sb.toString()); 245 246 } 247 248 249 }