Author: skitching
Date: Mon Feb 14 19:56:01 2005
New Revision: 153891

URL: http://svn.apache.org/viewcvs?view=rev&rev=153891
Log:
Class LinkObjectsAction now provides the functionality of the 
digester1.x classes SetNextRule, SetTopRule, SetRootRule.

Added:
    
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java
   (contents, props changed)
      - copied, changed from r153753, 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/SetNextAction.java
Removed:
    
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/SetNextAction.java
Modified:
    
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/factory/ActionFactory.java

Copied: 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java
 (from r153753, 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/SetNextAction.java)
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java?view=diff&rev=153891&p1=jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/SetNextAction.java&r1=153753&p2=jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java&r2=153891
==============================================================================
--- 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/SetNextAction.java
 (original)
+++ 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java
 Mon Feb 14 19:56:01 2005
@@ -1,6 +1,6 @@
-/* $Id: $
+/* $Id$
  *
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,18 +26,44 @@
 import org.apache.commons.digester2.ParseException;
 
 /**
- * <p>An Action that calls a method on the (top-1) (parent) object, passing 
- * the top object (child) as an argument.  It is commonly used to establish 
- * parent-child relationships between objects on the digester stack.</p>
+ * An Action that builds relationships between objects on the digester
+ * object stack, usually parent/child relationships.
+ * <p>
+ * The default behaviour calls a method on the parent (top-1) object, passing 
+ * the child (top) object as an argument. The parent object is then expected
+ * to store a reference to that child object for later use.
+ * <p>
+ * Providing non-default values for sourceOffset and targetOffset to the
+ * constructor of this class produce actions that can (for example):
+ * <ul>
+ * <li> pass the parent (top-1) object to a method on the child (top) object, 
or
+ * <li> pass the child (top) object to a method on the object at the root of 
+ *  the digester object stack.
+ * </ul>
+ * <p>
+ * For users of the Digester 1.x series, this action is equivalent to the
+ * SetNextRule, SetTopRule and SetRootRule classes.
  */
 
-public class SetNextAction extends AbstractAction {
+public class LinkObjectsAction extends AbstractAction {
 
     // ----------------------------------------------------- 
     // Instance Variables
     // ----------------------------------------------------- 
 
     /**
+     * The offset on the digester object stack of the object that will be
+     * passed as a parameter.
+     */
+    protected int sourceOffset;
+    
+    /**
+     * The offset on the digester object stack of the object on which the
+     * method will be invoked.
+     */
+    protected int targetOffset;
+    
+    /**
      * The method name to call on the parent object.
      */
     protected String methodName = null;
@@ -54,24 +80,69 @@
     // ----------------------------------------------------------- 
 
     /**
-     * Construct an action which invokes the specified method name.
+     * Construct an action which invokes the specified method name on the
+     * parent (top-1) object, passing the child (top) object.
      *
      * @param methodName Method name of the parent method to call
      */
-    public SetNextAction(String methodName) {
-        this(methodName, null);
+    public LinkObjectsAction(String methodName) {
+        this(0, 1, methodName, null);
     }
 
     /**
-     * Construct a "set next" rule with the specified method name.
+     * Construct an action which invokes the specified method name on the
+     * parent (top-1) object, passing the child (top) object, and that
+     * the standard type-conversion facilities should be applied to the
+     * child object first.
+     * <p>
+     * Note that when type-conversion is applied, this generally means that
+     * the object passed to the parent is <i>not</i> actually a reference to
+     * the object on top of the stack, but instead a reference to some new
+     * object derived from it. This implies that other actions which operate
+     * on the top object on the stack (ie the child) might not affect the
+     * object passed to the target method.
      *
      * @param methodName Method name of the parent method to call
      * @param paramType Java class of the parent method's argument
      *  (if you wish to use a primitive type, specify the corresonding
      *  Java wrapper class instead, such as <code>java.lang.Boolean</code>
-     *  for a <code>boolean</code> parameter)
+     *  for a <code>boolean</code> parameter). The value null may be
+     *  passed to indicate that no special type override will be done.
+     */
+    public LinkObjectsAction(String methodName, String paramType) {
+        this(0, 1, methodName, paramType);
+    }
+
+
+    /**
+     * Construct an action which invokes the specified method name on the
+     * specified target object passing the specified source object.
+     * <p>
+     * A stack offset of zero indicates the top (newest) object on the
+     * stack. Positive values are used to specify objects relative to the
+     * top of the stack. A stack offset of -1 indicates the oldest (root)
+     * object on the stack, and increasingly negative values are used to 
+     * specify objects relative to the root of the stack.
+     *
+     * @param sourceOffset is the offset on the digester stack of the
+     *  object that should be passed as a parameter.
+     * 
+     * @param targetOffset is the offset on the digester stack of the
+     *  objct that should have a method invoked on it.
+     *
+     * @param methodName Method name of the method to call on the target 
object.
+     *
+     * @param paramType Java class of the target method's argument
+     *  (if you wish to use a primitive type, specify the corresonding
+     *  Java wrapper class instead, such as <code>java.lang.Boolean</code>
+     *  for a <code>boolean</code> parameter). The value null may be
+     *  passed to indicate that no special type override will be done.
      */
-    public SetNextAction(String methodName, String paramType) {
+    public LinkObjectsAction(
+    int sourceOffset, int targetOffset,
+    String methodName, String paramType) {
+        this.sourceOffset = sourceOffset;
+        this.targetOffset = targetOffset;
         this.methodName = methodName;
         this.paramType = paramType;
     }
@@ -85,19 +156,21 @@
      */
     public void end(Context context, String namespace, String name) 
     throws ParseException {
-        // Identify the objects to be used
-        Object child = context.peek(0);
-        Object parent = context.peek(1);
         Log log = context.getLogger();
+
+        // Identify the objects to be used
+        Object sourceObject = context.peek(sourceOffset);
+        Object targetObject = context.peek(targetOffset);
+
         if (log.isDebugEnabled()) {
-            if (parent == null) {
-                log.debug("[SetNextRule]{" + context.getMatchPath() +
-                        "} Call [NULL PARENT]." +
-                        methodName + "(" + child + ")");
+            if (targetObject == null) {
+                log.debug("[LinkObjectsAction]{" + context.getMatchPath() +
+                        "} Call [NULL TARGET]." +
+                        methodName + "(" + sourceObject + ")");
             } else {
-                log.debug("[SetNextRule]{" + context.getMatchPath() +
-                        "} Call " + parent.getClass().getName() + "." +
-                        methodName + "(" + child + ")");
+                log.debug("[LinkObjectsAction]{" + context.getMatchPath() +
+                        "} Call " + targetObject.getClass().getName() + "." +
+                        methodName + "(" + sourceObject + ")");
             }
         }
 
@@ -111,12 +184,12 @@
                 throw new ParseException(ex);
             }
         } else {
-            paramTypes[0] = child.getClass();
+            paramTypes[0] = sourceObject.getClass();
         }
         
         try {
-            MethodUtils.invokeMethod(parent, methodName,
-                new Object[]{ child }, paramTypes);
+            MethodUtils.invokeMethod(targetObject, methodName,
+                new Object[]{ sourceObject }, paramTypes);
         } catch(NoSuchMethodException ex) {
             throw new ParseException(ex);
         } catch(IllegalAccessException ex) {
@@ -130,12 +203,16 @@
      * Render a printable version of this Rule.
      */
     public String toString() {
-        StringBuffer sb = new StringBuffer("SetNextRule[");
-        sb.append("methodName=");
+        StringBuffer sb = new StringBuffer("LinkObjectsAction[");
+        sb.append("sourceOffset=");
+        sb.append(sourceOffset);
+        sb.append(", targetOffset=");
+        sb.append(targetOffset);
+        sb.append(", methodName=");
         sb.append(methodName);
         sb.append(", paramType=");
         sb.append(paramType);
         sb.append("]");
-        return (sb.toString());
+        return sb.toString();
     }
 }

Propchange: 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/actions/LinkObjectsAction.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/factory/ActionFactory.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/factory/ActionFactory.java?view=diff&r1=153890&r2=153891
==============================================================================
--- 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/factory/ActionFactory.java
 (original)
+++ 
jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/factory/ActionFactory.java
 Mon Feb 14 19:56:01 2005
@@ -243,20 +243,20 @@
     }
 
     /**
-     * Add a "set next" rule for the specified parameters.
+     * Add a "link objects" rule for the specified parameters.
      *
      * @param pattern Element matching pattern
      * @param methodName Method name to call on the parent element
      * @see SetNextAction
      */
-    public void addSetNext(String pattern, String methodName)
+    public void addLinkObjects(String pattern, String methodName)
     throws InvalidRuleException {
         addRule(pattern,
-                new SetNextAction(methodName));
+                new LinkObjectsAction(methodName));
     }
 
     /**
-     * Add a "set next" rule for the specified parameters.
+     * Add a "link objects" rule for the specified parameters.
      *
      * @param pattern Element matching pattern
      * @param methodName Method name to call on the parent element
@@ -266,13 +266,13 @@
      *  for a <code>boolean</code> parameter)
      * @see SetNextAction
      */
-    public void addSetNext(
+    public void addLinkObjects(
     String pattern, 
     String methodName,
     String paramType)
     throws InvalidRuleException {
         addRule(pattern,
-                new SetNextAction(methodName, paramType));
+                new LinkObjectsAction(methodName, paramType));
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to