You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Pavel Lisovin <fo...@4me2.com> on 2003/07/28 12:50:22 UTC

[DIGESTER] is it possible to get xpath

To everyone,

I am trying to parse XML file to get custom objects using digester-rules xml
file
and wondering if there is an easy way to get XPath or match pattern when
custom method is invoked.

example :

rules.xml
-----------
<?xml version="1.0"?>
<digester-rules>
   <object-create-rule pattern="main" classname="TestConfig"/>
   <call-method-rule pattern="main/log" methodname="regConf" paramcount="1"
/>
   <pattern value="main/log">
      <object-create-rule classname="TestConfigElement" />
      <call-method-rule pattern="logFile" methodname="addAttribute"
paramcount="0" />
      <set-next-rule methodname="regConf" />
   </pattern>
</digester-rules>

input.xml
--------------
<main>
	<log>
		<configFile>log_config_file.cfg</configFile>
	</log>
</main>

File rules = new File("/rules.xml");
File input = new File("/input.xml");
Digester digester = DigesterLoader.createDigester(rules.toURL());
TestConfig testConf = (TestConfig)digester.parse(input);

I want somehow to access the created object TestConfigElement by let's say
name "main/log"?
for example

if TestConf has a method

public TestConfigElement getElement(String path) {
 ....
}

it will return my "main/log" element with all attributes.

Already found some ways how to do this but all of them requires aplying
patch to digester source ( i.e.
/org/apache/commons/digester/SetNextRule.java )
maybe there is an existing way how to this?

any ideas?

thanks

Pavel


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


Re: [DIGESTER] is it possible to get xpath

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2003-07-29 at 10:36, robert burrell donkin wrote:
> On Monday, July 28, 2003, at 11:50 AM, Pavel Lisovin wrote:
> 
> > To everyone,
> >
> > I am trying to parse XML file to get custom objects using digester-rules 
> > xml
> > file
> > and wondering if there is an easy way to get XPath or match pattern when
> > custom method is invoked.
> >
> > any ideas?
> 
> i think that the easiest way to achieve this would be to create a custom 
> Rule implementation. this would be based on the SetNextRule but would push 
> the value of digester.getMatch onto the parameter stack.
> 
> if you do go down this road and feel like contributing your Rule back to 
> the Apache Software Foundation then that'd be cool.
> 
> - robert

There is an ObjectParamRule, which allows arbitrary objects (including
strings) to be passed to the target of a CallMethodRule. I don't know
whether this is accessable via xmlrules, but if it is, it might do what
you want. It can't pass "the current match pattern", but you could pass
a literal "main/log" for example.

Regards,

Simon


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


Re: [DIGESTER] is it possible to get xpath

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Monday, July 28, 2003, at 11:50 AM, Pavel Lisovin wrote:

> To everyone,
>
> I am trying to parse XML file to get custom objects using digester-rules 
> xml
> file
> and wondering if there is an easy way to get XPath or match pattern when
> custom method is invoked.
>
> example :
>
> rules.xml
> -----------
> <?xml version="1.0"?>
> <digester-rules>
>    <object-create-rule pattern="main" classname="TestConfig"/>
>    <call-method-rule pattern="main/log" methodname="regConf" 
> paramcount="1"
> />
>    <pattern value="main/log">
>       <object-create-rule classname="TestConfigElement" />
>       <call-method-rule pattern="logFile" methodname="addAttribute"
> paramcount="0" />
>       <set-next-rule methodname="regConf" />
>    </pattern>
> </digester-rules>
>
> input.xml
> --------------
> <main>
> 	<log>
> 		<configFile>log_config_file.cfg</configFile>
> 	</log>
> </main>
>
> File rules = new File("/rules.xml");
> File input = new File("/input.xml");
> Digester digester = DigesterLoader.createDigester(rules.toURL());
> TestConfig testConf = (TestConfig)digester.parse(input);
>
> I want somehow to access the created object TestConfigElement by let's say
> name "main/log"?
> for example
>
> if TestConf has a method
>
> public TestConfigElement getElement(String path) {
>  ....
> }
>
> it will return my "main/log" element with all attributes.
>
> Already found some ways how to do this but all of them requires aplying
> patch to digester source ( i.e.
> /org/apache/commons/digester/SetNextRule.java )
> maybe there is an existing way how to this?
>
> any ideas?

i think that the easiest way to achieve this would be to create a custom 
Rule implementation. this would be based on the SetNextRule but would push 
the value of digester.getMatch onto the parameter stack.

if you do go down this road and feel like contributing your Rule back to 
the Apache Software Foundation then that'd be cool.

- robert


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