Save This Page
Home » commons-digester-1.8-src » org.apache.commons » digester » [javadoc | source]
    1   /* $Id: ObjectCreateRule.java 471661 2006-11-06 08:09:25Z skitching $
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    * 
   10    *      http://www.apache.org/licenses/LICENSE-2.0
   11    * 
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */ 
   18   
   19   
   20   package org.apache.commons.digester;
   21   
   22   
   23   import org.xml.sax.Attributes;
   24   
   25   
   26   /**
   27    * Rule implementation that creates a new object and pushes it
   28    * onto the object stack.  When the element is complete, the
   29    * object will be popped
   30    */
   31   
   32   public class ObjectCreateRule extends Rule {
   33   
   34   
   35       // ----------------------------------------------------------- Constructors
   36   
   37   
   38       /**
   39        * Construct an object create rule with the specified class name.
   40        *
   41        * @param digester The associated Digester
   42        * @param className 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(String className)} instead.
   46        */
   47       public ObjectCreateRule(Digester digester, String className) {
   48   
   49           this(className);
   50   
   51       }
   52   
   53   
   54       /**
   55        * Construct an object create rule with the specified class.
   56        *
   57        * @param digester The associated Digester
   58        * @param clazz Java class name of the object to be created
   59        *
   60        * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
   61        * Use {@link #ObjectCreateRule(Class clazz)} instead.
   62        */
   63       public ObjectCreateRule(Digester digester, Class clazz) {
   64   
   65           this(clazz);
   66   
   67       }
   68   
   69   
   70       /**
   71        * Construct an object create rule with the specified class name and an
   72        * optional attribute name containing an override.
   73        *
   74        * @param digester The associated Digester
   75        * @param className Java class name of the object to be created
   76        * @param attributeName Attribute name which, if present, contains an
   77        *  override of the class name to create
   78        *
   79        * @deprecated The digester instance is now set in the {@link Digester#addRule} method. 
   80        * Use {@link #ObjectCreateRule(String className, String attributeName)} instead.
   81        */
   82       public ObjectCreateRule(Digester digester, String className,
   83                               String attributeName) {
   84   
   85           this (className, attributeName);
   86   
   87       }
   88   
   89   
   90       /**
   91        * Construct an object create rule with the specified class and an
   92        * optional attribute name containing an override.
   93        *
   94        * @param digester The associated Digester
   95        * @param attributeName Attribute name which, if present, contains an
   96        * @param clazz Java class name of the object to be created
   97        *  override of the class name to create
   98        *
   99        * @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   }

Save This Page
Home » commons-digester-1.8-src » org.apache.commons » digester » [javadoc | source]