You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Eric Chow <er...@gmail.com> on 2011/10/06 05:02:07 UTC

Digester rules problem.

Hello,



<response query="10.70.0.2,v2p,3,1,0" result="true">

    <attribute name="ip">10.70.0.2</attribute>

    <attribute name="slot">3</attribute>

    <attribute name="port">1</attribute>

    <attribute name="status">active</attribute>
</record>



In the above XML, I have a bean as following:

public class Response {
   private String query;
   private boolean result;

   private String ip;
   private String slot;
   private String port;
   private String status;

   ....

}




For the query, result,  I can easy to use the following codes to parse.

Digester digester = new Digester();
digester.setNamespaceAware( true );
digester.setXIncludeAware( true );

digester.addObjectCreate( "response", Response .class );
digester.addSetProperties( "response", "query", "query" );
digester.addSetProperties( "response", "result", "result" );





But, how can I set those <attribute/> into the related properties?

Please help.

Thanks.
Eric

Re: Digester rules problem.

Posted by Maurizio Cucchiara <mc...@apache.org>.
Hi Eric,
I took a quick look at Digester, I have to say that I am not a
Digester guru (rather I am a newbie), but looking at source code I am
able to get what you are looking for using the code below.

public class ResponseTest {
    @Test
    public void buildTest() throws IOException, SAXException {
        Digester digester = new Digester();

        digester.addObjectCreate("response", Response.class);
        digester.addSetProperties("response", "query", "query");
        digester.addSetProperties("response", "result", "result");

        BeanPropertySetterRule rule = new BeanPropertySetterRule();
        rule.setPropertyNameFromAttribute("name");
        digester.addRule("response/attribute", rule);

        Response r =
digester.parse(this.getClass().getResourceAsStream("Response.xml"));
        assertNotNull(r);
        assertEquals("10.70.0.2", r.getIp());
        assertEquals("1", r.getPort());
        assertEquals("3", r.getSlot());
        assertEquals("active", r.getStatus());

    }
}
Maurizio Cucchiara



On 6 October 2011 09:40, Simone Tripodi <si...@apache.org> wrote:
> Hi Eric,
> I can provide you the solution but I'll be busy for the whole morning.
> Please wait I'll send you the hints ASAP.
> All the best,
> Simo
>
> http://people.apache.org/~simonetripodi/
> http://www.99soft.org/
>
>
>
> On Thu, Oct 6, 2011 at 5:02 AM, Eric Chow <er...@gmail.com> wrote:
>> Hello,
>>
>>
>>
>> <response query="10.70.0.2,v2p,3,1,0" result="true">
>>
>>    <attribute name="ip">10.70.0.2</attribute>
>>
>>    <attribute name="slot">3</attribute>
>>
>>    <attribute name="port">1</attribute>
>>
>>    <attribute name="status">active</attribute>
>> </record>
>>
>>
>>
>> In the above XML, I have a bean as following:
>>
>> public class Response {
>>   private String query;
>>   private boolean result;
>>
>>   private String ip;
>>   private String slot;
>>   private String port;
>>   private String status;
>>
>>   ....
>>
>> }
>>
>>
>>
>>
>> For the query, result,  I can easy to use the following codes to parse.
>>
>> Digester digester = new Digester();
>> digester.setNamespaceAware( true );
>> digester.setXIncludeAware( true );
>>
>> digester.addObjectCreate( "response", Response .class );
>> digester.addSetProperties( "response", "query", "query" );
>> digester.addSetProperties( "response", "result", "result" );
>>
>>
>>
>>
>>
>> But, how can I set those <attribute/> into the related properties?
>>
>> Please help.
>>
>> Thanks.
>> Eric
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Re: Digester rules problem.

Posted by Maurizio Cucchiara <mc...@apache.org>.
Digester is a good piece of software, I would love, but not at the
moment, I have so little free time and so many things to do.
Anyway, I have added it in my todo list.

Maurizio Cucchiara



On 6 October 2011 14:40, Simone Tripodi <si...@apache.org> wrote:
> About your question: currently there's NO full XPath rule matcher
> engine :( if you are interested on providing an implementation we can
> discuss about it on the dev@ ML

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


Re: Digester rules problem.

Posted by Simone Tripodi <si...@apache.org>.
Hi Mau,
thanks for providing to Eric the needed help, very appreciated! Just a
minor hint: the double SetProperties rule declaration can be replaced
by just one call:

public class ResponseTest {
   @Test
   public void buildTest() throws IOException, SAXException {
       Digester digester = new Digester();

       digester.addObjectCreate("response", Response.class);
       digester.addSetProperties("response", new String[]{ "query",
"result" }, new String[]{ "query", "result" });
[OMISS]
   }
}

since attribute names are exactly the properties names, the
attributes/properties arrays can be omitted:

public class ResponseTest {
   @Test
   public void buildTest() throws IOException, SAXException {
       Digester digester = new Digester();

       digester.addObjectCreate( "response", Response.class );
       digester.addSetProperties( "response" );
[OMISS]
   }
}

if you use the Digester3 rules binder (which I prefer, yuk yuk), you
can write the rules as

new AbstractRulesModule()
{

    @Override
    public void configure()
    {
       forPattern( "response" )
           .createObject().ofType( Response.class )
           .then()
           .setProperties()
               .addAlias( "query", "query" )
               .addAlias( "result", "result" ),
       forPattern( "response/attribute" )
           .setBeanProperty().extractPropertyNameFromAttribute( "name" );
    }

}

About your question: currently there's NO full XPath rule matcher
engine :( if you are interested on providing an implementation we can
discuss about it on the dev@ ML

HTH, have a nice day!!!
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Thu, Oct 6, 2011 at 12:38 PM, Maurizio Cucchiara
<mc...@apache.org> wrote:
> Hi Simo,
> looking at the http://s.apache.org/OQ0 I realized that Digester uses
> an xpath-like string, I am wondering if there is a way (or a rule)
> which allows using full xpath syntax (Eric could have used something
> like "response/attribute[@name='ip' ]").
> Is there any Rule implementation about this specific topic?
>
>
> Maurizio Cucchiara
>
>
>
> On 6 October 2011 09:40, Simone Tripodi <si...@apache.org> wrote:
>> Hi Eric,
>> I can provide you the solution but I'll be busy for the whole morning.
>> Please wait I'll send you the hints ASAP.
>> All the best,
>> Simo
>>
>> http://people.apache.org/~simonetripodi/
>> http://www.99soft.org/
>>
>>
>>
>> On Thu, Oct 6, 2011 at 5:02 AM, Eric Chow <er...@gmail.com> wrote:
>>> Hello,
>>>
>>>
>>>
>>> <response query="10.70.0.2,v2p,3,1,0" result="true">
>>>
>>>    <attribute name="ip">10.70.0.2</attribute>
>>>
>>>    <attribute name="slot">3</attribute>
>>>
>>>    <attribute name="port">1</attribute>
>>>
>>>    <attribute name="status">active</attribute>
>>> </record>
>>>
>>>
>>>
>>> In the above XML, I have a bean as following:
>>>
>>> public class Response {
>>>   private String query;
>>>   private boolean result;
>>>
>>>   private String ip;
>>>   private String slot;
>>>   private String port;
>>>   private String status;
>>>
>>>   ....
>>>
>>> }
>>>
>>>
>>>
>>>
>>> For the query, result,  I can easy to use the following codes to parse.
>>>
>>> Digester digester = new Digester();
>>> digester.setNamespaceAware( true );
>>> digester.setXIncludeAware( true );
>>>
>>> digester.addObjectCreate( "response", Response .class );
>>> digester.addSetProperties( "response", "query", "query" );
>>> digester.addSetProperties( "response", "result", "result" );
>>>
>>>
>>>
>>>
>>>
>>> But, how can I set those <attribute/> into the related properties?
>>>
>>> Please help.
>>>
>>> Thanks.
>>> Eric
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Re: Digester rules problem.

Posted by Maurizio Cucchiara <mc...@apache.org>.
Hi Simo,
looking at the http://s.apache.org/OQ0 I realized that Digester uses
an xpath-like string, I am wondering if there is a way (or a rule)
which allows using full xpath syntax (Eric could have used something
like "response/attribute[@name='ip' ]").
Is there any Rule implementation about this specific topic?


Maurizio Cucchiara



On 6 October 2011 09:40, Simone Tripodi <si...@apache.org> wrote:
> Hi Eric,
> I can provide you the solution but I'll be busy for the whole morning.
> Please wait I'll send you the hints ASAP.
> All the best,
> Simo
>
> http://people.apache.org/~simonetripodi/
> http://www.99soft.org/
>
>
>
> On Thu, Oct 6, 2011 at 5:02 AM, Eric Chow <er...@gmail.com> wrote:
>> Hello,
>>
>>
>>
>> <response query="10.70.0.2,v2p,3,1,0" result="true">
>>
>>    <attribute name="ip">10.70.0.2</attribute>
>>
>>    <attribute name="slot">3</attribute>
>>
>>    <attribute name="port">1</attribute>
>>
>>    <attribute name="status">active</attribute>
>> </record>
>>
>>
>>
>> In the above XML, I have a bean as following:
>>
>> public class Response {
>>   private String query;
>>   private boolean result;
>>
>>   private String ip;
>>   private String slot;
>>   private String port;
>>   private String status;
>>
>>   ....
>>
>> }
>>
>>
>>
>>
>> For the query, result,  I can easy to use the following codes to parse.
>>
>> Digester digester = new Digester();
>> digester.setNamespaceAware( true );
>> digester.setXIncludeAware( true );
>>
>> digester.addObjectCreate( "response", Response .class );
>> digester.addSetProperties( "response", "query", "query" );
>> digester.addSetProperties( "response", "result", "result" );
>>
>>
>>
>>
>>
>> But, how can I set those <attribute/> into the related properties?
>>
>> Please help.
>>
>> Thanks.
>> Eric
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Re: Digester rules problem.

Posted by Simone Tripodi <si...@apache.org>.
Hi Eric,
I can provide you the solution but I'll be busy for the whole morning.
Please wait I'll send you the hints ASAP.
All the best,
Simo

http://people.apache.org/~simonetripodi/
http://www.99soft.org/



On Thu, Oct 6, 2011 at 5:02 AM, Eric Chow <er...@gmail.com> wrote:
> Hello,
>
>
>
> <response query="10.70.0.2,v2p,3,1,0" result="true">
>
>    <attribute name="ip">10.70.0.2</attribute>
>
>    <attribute name="slot">3</attribute>
>
>    <attribute name="port">1</attribute>
>
>    <attribute name="status">active</attribute>
> </record>
>
>
>
> In the above XML, I have a bean as following:
>
> public class Response {
>   private String query;
>   private boolean result;
>
>   private String ip;
>   private String slot;
>   private String port;
>   private String status;
>
>   ....
>
> }
>
>
>
>
> For the query, result,  I can easy to use the following codes to parse.
>
> Digester digester = new Digester();
> digester.setNamespaceAware( true );
> digester.setXIncludeAware( true );
>
> digester.addObjectCreate( "response", Response .class );
> digester.addSetProperties( "response", "query", "query" );
> digester.addSetProperties( "response", "result", "result" );
>
>
>
>
>
> But, how can I set those <attribute/> into the related properties?
>
> Please help.
>
> Thanks.
> Eric
>

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