commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <si...@ecnetwork.co.nz>
Subject Re: Question About Digester
Date Wed, 01 Sep 2004 23:11:36 GMT
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


Mime
View raw message