You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jaap van Hengstum <Ja...@indicia.nl> on 2003/07/28 18:21:22 UTC

[Digester] NodeCreateRule

I'm at a loss using NodeCreateRule. I have the following xml structure:

<init>
	<item class="nl.indicia.quokka.init.TestContextItem">
		<configuration-file>foo</configuration-file>
		<properties>
			<TestString>xxxx</TestString>
			<TestInt>1234</TestInt>
		</properties>
	</item>
</init>

I want to parse it with a digester. I also have the following class (TestContextItem inherits this class):

public abstract class ContextItem
{
	protected String configurationFile;
	protected Node propertiesRoot;
	
	// -------------------------------------------------------------------------
	
	public String getConfigurationFile()
	{
		return this.configurationFile;
	}
	
	public void setConfigurationFile(String configurationFile)
	{
		this.configurationFile = configurationFile;
	}
  
	public Node getNode()
	{
		return this.propertiesRoot;
	}
  
	public void setNode(Node node)
	{
	    this.propertiesRoot = node;
	}

	// -------------------------------------------------------------------------
	
	public abstract void initialize() throws FrameworkException;
	public abstract void reinitialize() throws FrameworkException;
}

What I understand of the NodeCreateRule, it should allow me to consider everything between the <properties> tags as a xml node and pass this node to the instantiated ContextItem class using the setNode method on that class. But I can't figure out how to do that. 

I have the following digester configuration:

	...
(1)	this.addObjectCreate("init/item", "class", ContextItem.class);
(2)	this.addSetNext("init/item", "addItem");
(3)	this.addCallMethod("init/item/configuration-file", "setConfigurationFile", 0);
(4)	this.addRule("init/item/properties", new NodeCreateRule());
	...

How do I continue after the NodeCreateRule? That is, how do I pass the contents of the node on the object stack I  create in line (4) to the SetNode method on the TestContextItem instance created in line (1)??? 

(and if this is not possible using the standard digester rules, what new rule should I create?)

If someone could answer this, he or she would make me very happy :-)

Jaap.

Re: [Digester] NodeCreateRule

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2003-07-29 at 04:21, Jaap van Hengstum wrote:
> I'm at a loss using NodeCreateRule. I have the following xml structure:
> What I understand of the NodeCreateRule, it should allow me to consider everything between the <properties> tags as a xml node and pass this node to the instantiated ContextItem class using the setNode method on that class. But I can't figure out how to do that. 
[snip]
> 
> I have the following digester configuration:
> 
> 	...
> (1)	this.addObjectCreate("init/item", "class", ContextItem.class);
> (2)	this.addSetNext("init/item", "addItem");
> (3)	this.addCallMethod("init/item/configuration-file", "setConfigurationFile", 0);
> (4)	this.addRule("init/item/properties", new NodeCreateRule());
> 	...
> 
> How do I continue after the NodeCreateRule? That is, how do I pass the contents of the node on the object stack I  create in line (4) to the SetNode method on the TestContextItem instance created in line (1)??? 

I haven't used NodeCreateRule myself, but I'm willing to have a stab at
an answer...

According to the Digester docs:
  The created Node will be pushed on Digester's object stack when done.

So I would guess that you just follow (4) above with
  this.addSetNext("init/item/properties", "setNode");

This will grab the second-to-last object on the stack (which is your
ContextItem object), and call its setNode method passing the last object
on the stack (which is the Node object just created & pushed there by
the NodeCreateRule).

If you find you get an exception indicating "no such method", then try
using CallMethodRule in place of SetNextRule, which allows you to
specify the parameter types for the target method.

If this doesn't work, then I suggest turning up digester logging to
maximum, which will provide you with *all* the debug info you will ever
need :-). Digester uses commons-logging, so see that module for how to
adjust logging levels.

Regards,

Simon