You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Andrew Steiger <an...@andrewsteiger.com> on 2004/09/01 15:12:46 UTC

Question About Digester

Hello everyone,

I am trying to use Digester to load a Properties object from an XML file.  When
I run the parse method off of the Digester object, the Object that is returned
doesn't have anything in it.  I am not sure if this is a bug or that I am
missing something in the implementation.  Here is my code:

//create digester based on the rules
digester = DigesterLoader.createDigester(rulesUrl);

//add properties to object stack --
//this is so you can get the properties object that was created
digester.push(result);

//parse xml files against rules
result = (Stack)digester.parse(propertiesXml);

//get properties object
props = (Properties)result.pop();
-----------------------

My rules xml files looks like this:

<digester-rules>
	<pattern value="NamingResources">
		<!--create a Stack object to hold a reference to the Properties object created
in the 'ResourceParams' pattern -->
		<object-create-rule classname="java.util.Stack"/>

		<pattern value="ResourceParams">
			<!-- instantiate a Properties object -->
			<object-create-rule classname="java.util.Properties"/>

			<!-- each parameter node is a key-value pair in the Properties object -->
			<pattern value="parameter">
				<!-- call the 'setProperty' method -->
				<call-method-rule methodname="setProperty" paramcount="2"/>
				<call-param-rule pattern="name" paramnumber="0"/>
				<call-param-rule pattern="value" paramnumber="1"/>
			</pattern>

			<!-- push the Properties object created onto the Stack object -->
			<set-next-rule methodname="push" />
		</pattern>
	</pattern>
</digester-rules>
-----------------------

The following excepts from the log generated so that all the pieces seem to be
functioning:

2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  - Popping params
2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
[CallMethodRule](0)testWhileIdle
2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
[CallMethodRule](1)false
2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
[CallMethodRule]{NamingResources/ResourceParams/parameter} Call
java.util.Properties.setProperty(testWhileIdle/java.lang.String,false/java.lang.String)
2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  - Matching
name=setProperty on class java.util.Properties
2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  - Found
straight match: public synchronized java.lang.Object
java.util.Properties.setProperty(java.lang.String,java.lang.String)
2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  -
isPublic:true

...

2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - Matching
name=push on class java.util.Stack
2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - Found
matching name:
2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - public
java.lang.Object java.util.Stack.push(java.lang.Object)
2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  -
Param=java.util.Properties
2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  -
Method=java.lang.Object
2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - public
java.lang.Object java.util.Stack.push(java.lang.Object) accessible version of
public java.lang.Object java.util.Stack.push(java.lang.Object)

...

2004/09/01 09:07:19 DEBUG org.apache.commons.digester.Digester  -
[ObjectCreateRule]{NamingResources} Pop java.util.Stack
-----------------------

Any help would be greatly appreciated.

Thanks,
Andrew.

--
Andrew Steiger
EMail:   andrew@andrewsteiger.com
Web:     http://www.andrewsteiger.com

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


Re: Question About Digester

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Thu, 2004-09-02 at 01:12, Andrew Steiger wrote:
> Hello everyone,
> 
> I am trying to use Digester to load a Properties object from an XML file.  When
> I run the parse method off of the Digester object, the Object that is returned
> doesn't have anything in it.  I am not sure if this is a bug or that I am
> missing something in the implementation.  Here is my code:
> 
> //create digester based on the rules
> digester = DigesterLoader.createDigester(rulesUrl);
> 
> //add properties to object stack --
> //this is so you can get the properties object that was created
> digester.push(result);
> 
> //parse xml files against rules
> result = (Stack)digester.parse(propertiesXml);
> 
> //get properties object
> props = (Properties)result.pop();
> -----------------------
> 
> My rules xml files looks like this:
> 
> <digester-rules>
> 	<pattern value="NamingResources">
> 		<!--create a Stack object to hold a reference to the Properties object created
> in the 'ResourceParams' pattern -->
> 		<object-create-rule classname="java.util.Stack"/>
> 
> 		<pattern value="ResourceParams">
> 			<!-- instantiate a Properties object -->
> 			<object-create-rule classname="java.util.Properties"/>
> 
> 			<!-- each parameter node is a key-value pair in the Properties object -->
> 			<pattern value="parameter">
> 				<!-- call the 'setProperty' method -->
> 				<call-method-rule methodname="setProperty" paramcount="2"/>
> 				<call-param-rule pattern="name" paramnumber="0"/>
> 				<call-param-rule pattern="value" paramnumber="1"/>
> 			</pattern>
> 
> 			<!-- push the Properties object created onto the Stack object -->
> 			<set-next-rule methodname="push" />
> 		</pattern>
> 	</pattern>
> </digester-rules>
> -----------------------

You first push a Properties object onto the stack for some reason.

The digester rules then:
 * create a Stack object every time a NamingResources tag is found
 * create a Properties object each time a ResourceParams tag is found
 * add (name, value) pairs to the Properties
 * push the properties object onto the stack
but the Stack object is never linked to the Properties object you pushed
at any time. This is why at parsing end you have an unaltered Properties
object.

I don't understand why you push the original Properties object on the
stack. Nor why you chose "Stack" to hold the properties objects
corresponding to <ResourceParams> tags - you only ever push, so isn't a
List of some sort a more appropriate collection type?

If I understand what you are trying to achieve correctly, you have two
options:
(a) 
push a LinkedList or ArrayList or similar onto the digester stack
before starting the parse. Do *not* have an object-create-rule
associated with <NamingResources> - you have already pushed an object
onto the stack which "represents" this tag. Also, change the "push"
method call to an "add".
(b)
Don't push anything onto the stack at start. Have an object-create-rule
associated with the document root (just as you have now); have it create
some kind of List object rather than a Stack [unless you have some
special reason for wanting a Stack object that I don't know about].
At the end of parsing, this List object will not be on the stack (it
gets popped off at </NamingResources>), but there are two ways of
getting access to it: it is returned by the parse() method, and it is
returned by the Digester.getRoot() method.

I hope this helps.

Regards,

Simon



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


Re: Question About Digester

Posted by Craig McClanahan <cr...@gmail.com>.
In addition to Simon's comments, the XML you're trying to parse looks
suspiciously like the metadata used to define JNDI resources in a
Tomcat server.xml file :-).  If that's what you are really trying to
do, it would be worth downloading the Tomcat source code, and looking
at the Digester rules that Tomcat itself uses to parse this file (in
package org.apache.catalina.startup).  You might get some helpful
ideas for your own use.

Craig

On Wed,  1 Sep 2004 06:12:46 -0700, Andrew Steiger
<an...@andrewsteiger.com> wrote:
> Hello everyone,
> 
> I am trying to use Digester to load a Properties object from an XML file.  When
> I run the parse method off of the Digester object, the Object that is returned
> doesn't have anything in it.  I am not sure if this is a bug or that I am
> missing something in the implementation.  Here is my code:
> 
> //create digester based on the rules
> digester = DigesterLoader.createDigester(rulesUrl);
> 
> //add properties to object stack --
> //this is so you can get the properties object that was created
> digester.push(result);
> 
> //parse xml files against rules
> result = (Stack)digester.parse(propertiesXml);
> 
> //get properties object
> props = (Properties)result.pop();
> -----------------------
> 
> My rules xml files looks like this:
> 
> <digester-rules>
>         <pattern value="NamingResources">
>                 <!--create a Stack object to hold a reference to the Properties object created
> in the 'ResourceParams' pattern -->
>                 <object-create-rule classname="java.util.Stack"/>
> 
>                 <pattern value="ResourceParams">
>                         <!-- instantiate a Properties object -->
>                         <object-create-rule classname="java.util.Properties"/>
> 
>                         <!-- each parameter node is a key-value pair in the Properties object -->
>                         <pattern value="parameter">
>                                 <!-- call the 'setProperty' method -->
>                                 <call-method-rule methodname="setProperty" paramcount="2"/>
>                                 <call-param-rule pattern="name" paramnumber="0"/>
>                                 <call-param-rule pattern="value" paramnumber="1"/>
>                         </pattern>
> 
>                         <!-- push the Properties object created onto the Stack object -->
>                         <set-next-rule methodname="push" />
>                 </pattern>
>         </pattern>
> </digester-rules>
> -----------------------
> 
> The following excepts from the log generated so that all the pieces seem to be
> functioning:
> 
> 2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  - Popping params
> 2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
> [CallMethodRule](0)testWhileIdle
> 2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
> [CallMethodRule](1)false
> 2004/08/31 17:48:01 DEBUG org.apache.commons.digester.Digester  -
> [CallMethodRule]{NamingResources/ResourceParams/parameter} Call
> java.util.Properties.setProperty(testWhileIdle/java.lang.String,false/java.lang.String)
> 2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  - Matching
> name=setProperty on class java.util.Properties
> 2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  - Found
> straight match: public synchronized java.lang.Object
> java.util.Properties.setProperty(java.lang.String,java.lang.String)
> 2004/08/31 17:48:01 DEBUG org.apache.commons.beanutils.MethodUtils  -
> isPublic:true
> 
> ...
> 
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - Matching
> name=push on class java.util.Stack
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - Found
> matching name:
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - public
> java.lang.Object java.util.Stack.push(java.lang.Object)
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  -
> Param=java.util.Properties
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  -
> Method=java.lang.Object
> 2004/09/01 09:07:19 DEBUG org.apache.commons.beanutils.MethodUtils  - public
> java.lang.Object java.util.Stack.push(java.lang.Object) accessible version of
> public java.lang.Object java.util.Stack.push(java.lang.Object)
> 
> ...
> 
> 2004/09/01 09:07:19 DEBUG org.apache.commons.digester.Digester  -
> [ObjectCreateRule]{NamingResources} Pop java.util.Stack
> -----------------------
> 
> Any help would be greatly appreciated.
> 
> Thanks,
> Andrew.
> 
> --
> Andrew Steiger
> EMail:   andrew@andrewsteiger.com
> Web:     http://www.andrewsteiger.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
>

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