commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/digester/src/java/org/apache/commons/digester Digester.java FactoryCreateRule.java
Date Sat, 15 Mar 2003 18:37:51 GMT
rdonkin     2003/03/15 10:37:51

  Modified:    digester/src/java/org/apache/commons/digester Digester.java
                        FactoryCreateRule.java
  Log:
  Added support for non-propagation of exceptions. This will (when the correct options are
set) allow a digestion to continue when an object creation has failed
  
  Revision  Changes    Path
  1.71      +117 -14   jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java
  
  Index: Digester.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
  retrieving revision 1.70
  retrieving revision 1.71
  diff -u -r1.70 -r1.71
  --- Digester.java	2 Feb 2003 16:09:53 -0000	1.70
  +++ Digester.java	15 Mar 2003 18:37:51 -0000	1.71
  @@ -1827,34 +1827,35 @@
       
       /**
        * Add a "factory create" rule for the specified parameters.
  +     * Exceptions thrown during the object creation process will be propagated.
        *
        * @param pattern Element matching pattern
        * @param className Java class name of the object creation factory class
        */
       public void addFactoryCreate(String pattern, String className) {
   
  -        addRule(pattern,
  -                new FactoryCreateRule(className));
  +        addFactoryCreate(pattern, className, false);
   
       }
   
   
       /**
        * Add a "factory create" rule for the specified parameters.
  +     * Exceptions thrown during the object creation process will be propagated.
        *
        * @param pattern Element matching pattern
        * @param clazz Java class of the object creation factory class
        */
       public void addFactoryCreate(String pattern, Class clazz) {
   
  -        addRule(pattern,
  -                new FactoryCreateRule(clazz));
  +        addFactoryCreate(pattern, clazz, false);
   
       }
   
   
       /**
        * Add a "factory create" rule for the specified parameters.
  +     * Exceptions thrown during the object creation process will be propagated.
        *
        * @param pattern Element matching pattern
        * @param className Java class name of the object creation factory class
  @@ -1864,14 +1865,14 @@
       public void addFactoryCreate(String pattern, String className,
                                    String attributeName) {
   
  -        addRule(pattern,
  -                new FactoryCreateRule(className, attributeName));
  +        addFactoryCreate(pattern, className, attributeName, false);
   
       }
   
   
       /**
        * Add a "factory create" rule for the specified parameters.
  +     * Exceptions thrown during the object creation process will be propagated.
        *
        * @param pattern Element matching pattern
        * @param clazz Java class of the object creation factory class
  @@ -1881,14 +1882,14 @@
       public void addFactoryCreate(String pattern, Class clazz,
                                    String attributeName) {
   
  -        addRule(pattern,
  -                new FactoryCreateRule(clazz, attributeName));
  +        addFactoryCreate(pattern, clazz, attributeName, false);
   
       }
   
   
       /**
        * Add a "factory create" rule for the specified parameters.
  +     * Exceptions thrown during the object creation process will be propagated.
        *
        * @param pattern Element matching pattern
        * @param creationFactory Previously instantiated ObjectCreationFactory
  @@ -1897,12 +1898,114 @@
       public void addFactoryCreate(String pattern,
                                    ObjectCreationFactory creationFactory) {
   
  +        addFactoryCreate(pattern, creationFactory, false);
  +
  +    }
  +
  +    /**
  +     * Add a "factory create" rule for the specified parameters.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param className Java class name of the object creation factory class
  +     * @param ignoreCreateExceptions when <code>true</code> any exceptions
thrown during
  +     * object creation will be ignored.
  +     */
  +    public void addFactoryCreate(
  +                                    String pattern, 
  +                                    String className,
  +                                    boolean ignoreCreateExceptions) {
  +
  +        addRule(
  +                pattern,
  +                new FactoryCreateRule(className, ignoreCreateExceptions));
  +
  +    }
  +
  +
  +    /**
  +     * Add a "factory create" rule for the specified parameters.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param clazz Java class of the object creation factory class
  +     * @param ignoreCreateExceptions when <code>true</code> any exceptions
thrown during
  +     * object creation will be ignored.
  +     */
  +    public void addFactoryCreate(
  +                                    String pattern, 
  +                                    Class clazz,
  +                                    boolean ignoreCreateExceptions) {
  +
  +        addRule(
  +                pattern,
  +                new FactoryCreateRule(clazz, ignoreCreateExceptions));
  +
  +    }
  +
  +
  +    /**
  +     * Add a "factory create" rule for the specified parameters.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param className Java class name of the object creation factory class
  +     * @param attributeName Attribute name which, if present, overrides the
  +     *  value specified by <code>className</code>
  +     * @param ignoreCreateExceptions when <code>true</code> any exceptions
thrown during
  +     * object creation will be ignored.
  +     */
  +    public void addFactoryCreate(
  +                                String pattern, 
  +                                String className,
  +                                String attributeName,
  +                                boolean ignoreCreateExceptions) {
  +
  +        addRule(
  +                pattern,
  +                new FactoryCreateRule(className, attributeName, ignoreCreateExceptions));
  +
  +    }
  +
  +
  +    /**
  +     * Add a "factory create" rule for the specified parameters.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param clazz Java class of the object creation factory class
  +     * @param attributeName Attribute name which, if present, overrides the
  +     *  value specified by <code>className</code>
  +     * @param ignoreCreateExceptions when <code>true</code> any exceptions
thrown during
  +     * object creation will be ignored.
  +     */
  +    public void addFactoryCreate(
  +                                    String pattern, 
  +                                    Class clazz,
  +                                    String attributeName,
  +                                    boolean ignoreCreateExceptions) {
  +
  +        addRule(
  +                pattern,
  +                new FactoryCreateRule(clazz, attributeName, ignoreCreateExceptions));
  +
  +    }
  +
  +
  +    /**
  +     * Add a "factory create" rule for the specified parameters.
  +     *
  +     * @param pattern Element matching pattern
  +     * @param creationFactory Previously instantiated ObjectCreationFactory
  +     *  to be utilized
  +     * @param ignoreCreateExceptions when <code>true</code> any exceptions
thrown during
  +     * object creation will be ignored.
  +     */
  +    public void addFactoryCreate(String pattern,
  +                                 ObjectCreationFactory creationFactory,
  +                                 boolean ignoreCreateExceptions) {
  +
           creationFactory.setDigester(this);
           addRule(pattern,
  -                new FactoryCreateRule(creationFactory));
  +                new FactoryCreateRule(creationFactory, ignoreCreateExceptions));
   
       }
  -
   
       /**
        * Add an "object create" rule for the specified parameters.
  
  
  
  1.11      +177 -22   jakarta-commons/digester/src/java/org/apache/commons/digester/FactoryCreateRule.java
  
  Index: FactoryCreateRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/FactoryCreateRule.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- FactoryCreateRule.java	2 Feb 2003 16:09:53 -0000	1.10
  +++ FactoryCreateRule.java	15 Mar 2003 18:37:51 -0000	1.11
  @@ -58,6 +58,7 @@
   
   package org.apache.commons.digester;
   
  +import org.apache.commons.collections.ArrayStack;
   
   import org.xml.sax.Attributes;
   
  @@ -78,6 +79,12 @@
   
   public class FactoryCreateRule extends Rule {
   
  +    // ----------------------------------------------------------- Fields
  +    
  +    /** Should exceptions thrown by the factory be ignored? */
  +    private boolean ignoreCreateExceptions;
  +    /** Stock to manage */
  +    private ArrayStack exceptionIgnoredStack;
   
       // ----------------------------------------------------------- Constructors
   
  @@ -180,15 +187,101 @@
       }    
   
       /**
  +     * <p>Construct a factory create rule that will use the specified
  +     * class name to create an {@link ObjectCreationFactory} which will
  +     * then be used to create an object and push it on the stack.</p>
  +     *
  +     * <p>Exceptions thrown during the object creation process will be propagated.</p>
  +     *
  +     * @param className Java class name of the object creation factory class
  +     */
  +    public FactoryCreateRule(String className) {
  +
  +        this(className, false);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Construct a factory create rule that will use the specified
  +     * class to create an {@link ObjectCreationFactory} which will
  +     * then be used to create an object and push it on the stack.</p>
  +     *
  +     * <p>Exceptions thrown during the object creation process will be propagated.</p>
  +     *
  +     * @param clazz Java class name of the object creation factory class
  +     */
  +    public FactoryCreateRule(Class clazz) {
  +
  +        this(clazz, false);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Construct a factory create rule that will use the specified
  +     * class name (possibly overridden by the specified attribute if present)
  +     * to create an {@link ObjectCreationFactory}, which will then be used
  +     * to instantiate an object instance and push it onto the stack.</p>
  +     *
  +     * <p>Exceptions thrown during the object creation process will be propagated.</p>
  +     *
  +     * @param className Default Java class name of the factory class
  +     * @param attributeName Attribute name which, if present, contains an
  +     *  override of the class name of the object creation factory to create.
  +     */
  +    public FactoryCreateRule(String className, String attributeName) {
  +
  +        this(className, attributeName, false);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Construct a factory create rule that will use the specified
  +     * class (possibly overridden by the specified attribute if present)
  +     * to create an {@link ObjectCreationFactory}, which will then be used
  +     * to instantiate an object instance and push it onto the stack.</p>
  +     *
  +     * <p>Exceptions thrown during the object creation process will be propagated.</p>
  +     *
  +     * @param clazz Default Java class name of the factory class
  +     * @param attributeName Attribute name which, if present, contains an
  +     *  override of the class name of the object creation factory to create.
  +     */
  +    public FactoryCreateRule(Class clazz, String attributeName) {
  +
  +        this(clazz, attributeName, false);
  +
  +    }
  +
  +
  +    /**
  +     * <p>Construct a factory create rule using the given, already instantiated,
  +     * {@link ObjectCreationFactory}.</p>
  +     *
  +     * <p>Exceptions thrown during the object creation process will be propagated.</p>
  +     *
  +     * @param creationFactory called on to create the object.
  +     */
  +    public FactoryCreateRule(ObjectCreationFactory creationFactory) {
  +
  +        this(creationFactory, false);
  +
  +    }
  +    
  +    /**
        * Construct a factory create rule that will use the specified
        * class name to create an {@link ObjectCreationFactory} which will
        * then be used to create an object and push it on the stack.
        *
        * @param className Java class name of the object creation factory class
  +     * @param ignoreCreateException if true, exceptions thrown by the object creation factory
  +     * will be ignored.
        */
  -    public FactoryCreateRule(String className) {
  +    public FactoryCreateRule(String className, boolean ignoreCreateExceptions) {
   
  -        this(className, null);
  +        this(className, null, ignoreCreateExceptions);
   
       }
   
  @@ -199,10 +292,12 @@
        * then be used to create an object and push it on the stack.
        *
        * @param clazz Java class name of the object creation factory class
  +     * @param ignoreCreateException if true, exceptions thrown by the object creation factory
  +     * will be ignored.
        */
  -    public FactoryCreateRule(Class clazz) {
  +    public FactoryCreateRule(Class clazz, boolean ignoreCreateExceptions) {
   
  -        this(clazz, null);
  +        this(clazz, null, ignoreCreateExceptions);
   
       }
   
  @@ -216,11 +311,17 @@
        * @param className Default Java class name of the factory class
        * @param attributeName Attribute name which, if present, contains an
        *  override of the class name of the object creation factory to create.
  +     * @param ignoreCreateException if true, exceptions thrown by the object creation factory
  +     * will be ignored.
        */
  -    public FactoryCreateRule(String className, String attributeName) {
  +    public FactoryCreateRule(
  +                                String className, 
  +                                String attributeName,
  +                                boolean ignoreCreateExceptions) {
   
           this.className = className;
           this.attributeName = attributeName;
  +        this.ignoreCreateExceptions = ignoreCreateExceptions;
   
       }
   
  @@ -234,10 +335,15 @@
        * @param clazz Default Java class name of the factory class
        * @param attributeName Attribute name which, if present, contains an
        *  override of the class name of the object creation factory to create.
  +     * @param ignoreCreateException if true, exceptions thrown by the object creation factory
  +     * will be ignored.
        */
  -    public FactoryCreateRule(Class clazz, String attributeName) {
  +    public FactoryCreateRule(
  +                                Class clazz, 
  +                                String attributeName,
  +                                boolean ignoreCreateExceptions) {
   
  -        this(clazz.getName(), attributeName);
  +        this(clazz.getName(), attributeName, ignoreCreateExceptions);
   
       }
   
  @@ -247,11 +353,15 @@
        * {@link ObjectCreationFactory}.
        *
        * @param creationFactory called on to create the object.
  +     * @param ignoreCreateException if true, exceptions thrown by the object creation factory
  +     * will be ignored.
        */
  -    public FactoryCreateRule(ObjectCreationFactory creationFactory) {
  +    public FactoryCreateRule(
  +                            ObjectCreationFactory creationFactory, 
  +                            boolean ignoreCreateExceptions) {
   
           this.creationFactory = creationFactory;
  -
  +        this.ignoreCreateExceptions = ignoreCreateExceptions;
       }
   
       // ----------------------------------------------------- Instance Variables
  @@ -286,22 +396,69 @@
        *
        * @param attributes The attribute list of this element
        */
  -    public void begin(Attributes attributes) throws Exception {
  -
  -        Object instance = getFactory(attributes).createObject(attributes);
  -        if (digester.log.isDebugEnabled()) {
  -            digester.log.debug("[FactoryCreateRule]{" + digester.match +
  -                    "} New " + instance.getClass().getName());
  +    public void begin(String namespace, String name, Attributes attributes) throws Exception
{
  +        
  +        if (ignoreCreateExceptions) {
  +        
  +            if (exceptionIgnoredStack == null) {
  +                exceptionIgnoredStack = new ArrayStack();
  +            }
  +            
  +            try {
  +                Object instance = getFactory(attributes).createObject(attributes);
  +                
  +                if (digester.log.isDebugEnabled()) {
  +                    digester.log.debug("[FactoryCreateRule]{" + digester.match +
  +                            "} New " + instance.getClass().getName());
  +                }
  +                digester.push(instance);
  +                exceptionIgnoredStack.push(Boolean.FALSE);
  +                
  +            } catch (Exception e) {
  +                // log message and error
  +                if (digester.log.isInfoEnabled()) {
  +                    digester.log.info("[FactoryCreateRule] Create exception ignored: "

  +                        + ((e.getMessage() == null) ? e.getClass().getName() : e.getMessage()));
  +                    if (digester.log.isDebugEnabled()) {
  +                        digester.log.debug("[FactoryCreateRule] Ignored exception:", e);
  +                    }
  +                }
  +                exceptionIgnoredStack.push(Boolean.TRUE);
  +            }
  +            
  +        } else {
  +            Object instance = getFactory(attributes).createObject(attributes);
  +            
  +            if (digester.log.isDebugEnabled()) {
  +                digester.log.debug("[FactoryCreateRule]{" + digester.match +
  +                        "} New " + instance.getClass().getName());
  +            }
  +            digester.push(instance);
           }
  -        digester.push(instance);
  -
       }
   
   
       /**
        * Process the end of this element.
        */
  -    public void end() throws Exception {
  +    public void end(String namespace, String name) throws Exception {
  +        
  +        // check if object was created 
  +        // this only happens if an exception was thrown and we're ignoring them
  +        if (	
  +                ignoreCreateExceptions 
  +                && exceptionIgnoredStack != null 
  +                && !(exceptionIgnoredStack.empty())) {
  +                
  +            if (((Boolean) exceptionIgnoredStack.pop()).booleanValue()) {
  +                // creation exception was ignored
  +                // nothing was put onto the stack
  +                if (digester.log.isTraceEnabled()) {
  +                    digester.log.trace("[FactoryCreateRule] No creation so no push so no
pop");
  +                }
  +                return;
  +            }
  +        } 
   
           Object top = digester.pop();
           if (digester.log.isDebugEnabled()) {
  @@ -377,7 +534,5 @@
           }
           return (creationFactory);
   
  -    }
  -
  -
  +    }    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message