Hi
I have got the behaviour I want by extending the CallMethodRule as per the
code below (basically setting the bodyText to the element name). Then
configuring it as
        digester.addCallMethod("FXTRADE/sale_ccy", "addCurrencyMapping", 2);
        digester.addCallParam("FXTRADE/sale_ccy", 0);
        digester.addRule("FXTRADE/sale_ccy", new ElementNameParamRule(1));
My question is really given my two days experience of Digester, Is this a
sensible solution.

package org.apache.commons.digester;
import java.lang.reflect.Method;
import org.xml.sax.Attributes;
/**
 * Rule implementation that saves a parameter from either an attribute of
 * this element, or from the element body, to be used in a call generated
 * by a surrounding CallMethodRule rule.
 *
 * @author Craig McClanahan
 * @version $Revision: 1.7 $ $Date: 2002/03/23 17:45:57 $
 */
public class ElementNameParamRule extends CallParamRule {
    // -----------------------------------------------------------
Constructors
    /**
     * Construct a "call parameter" rule that will save the body text of
this
     * element as the parameter value.
     *
     * @param paramIndex The zero-relative parameter number
     */
    public ElementNameParamRule(int paramIndex) {
        super(paramIndex);
    }
    /**
     * Construct a "call parameter" rule that will save the value of the
     * specified attribute as the parameter value.
     *
     * @param paramIndex The zero-relative parameter number
     * @param attributeName The name of the attribute to save
     */
    public ElementNameParamRule(int paramIndex,
                         String attributeName) {
     super (paramIndex, attributeName);
    }
    // --------------------------------------------------------- Public
Methods
    /**
     * Process the start of this element.
     *
     * @param attributes The attribute list for this element
     */
    public void begin(Attributes attributes) throws Exception {

        if (attributeName != null) {
            bodyText = digester.getCurrentElementName();
        }
    }
    /**
     * Render a printable version of this Rule.
     */
    public String toString() {

        StringBuffer sb = new StringBuffer("ElementNameParamRule[");
        sb.append("paramIndex=");
        sb.append(paramIndex);
        sb.append(", attributeName=");
        sb.append(attributeName);
        sb.append("]");
        return (sb.toString());
    }
}


:-) Tom Lister
Technical Architecture Group
* - 020 7756 5014
* - [EMAIL PROTECTED]


-----Original Message-----
From: Simon Kitching [mailto:[EMAIL PROTECTED]
Sent: 09 July 2003 00:25
To: Jakarta Commons Users List
Subject: Re: [Digester] element names


On Wed, 2003-07-09 at 04:54, Lister, Tom (ANTS) wrote:
> Hi
> I'm need to parse an XML document and cross reference selected elements
with
> a mapping database to resolve different naming conventions between
systems.
> We are using XML via MQ for real time integration.
> 
> My problem is that when I get the content of an element to call a mapping
> method call, I also want to pass the name of the element as a second
> parameter to the method. I can't see a way to get this information.
> Alternatively a way to pass in a hard coded parameter would also work as I
> know which elements I am defining rules for.
> 
> I can't see any way to pass parameters to a method call other than element
> contents - or have I missed something basic.
> 
> Do I need to build a custom rule for this?

I don't think what you want to do can be accomplished without extending
Digester in one way or another.


Digester class has a getCurrentElementName() method.
The Rule class (which is extended by every type of Rule) has a
getDigester() method. So yes I think you could write a custom rule which
passes the element name.

Your suggestion about passing a constant string as a parameter also
sounds reasonable but as far as I am aware, CallMethodRule/CallParamRule
do not currently support passing constants as parameters. I think that
this feature would be a nice addition to Digester; eg adding a new
constructor:
  public CallParamRule(int paramIndex, Object paramObj)

If you did implement constant parameters, I think it would be a good
candidate for inclusion in Digester (not that my opinion counts for
anything :-). 


Cheers,

Simon



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


***************************************************************************
This communication (including any attachments) contains confidential information.  If 
you are not the intended recipient and you have received this communication in error, 
you should destroy it without copying, disclosing or otherwise using its contents.  
Please notify the sender immediately of the error.

Internet communications are not necessarily secure and may be intercepted or changed 
after they are sent.  Abbey National Treasury Services plc does not accept liability 
for any loss you may suffer as a result of interception or any liability for such 
changes.  If you wish to confirm the origin or content of this communication, please 
contact the sender by using an alternative means of communication.

This communication does not create or modify any contract and, unless otherwise 
stated, is not intended to be contractually binding.

Abbey National Treasury Services plc. Registered Office:  Abbey National House, 2 
Triton Square, Regents Place, London NW1 3AN.  Registered in England under Company 
Registration Number: 2338548.  Regulated by the Financial Services Authority (FSA).
***************************************************************************


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

Reply via email to