You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dmitry Beransky <db...@dembel.org> on 2006/12/01 23:58:08 UTC

[digester] setting parent properties after the child's

Hi,

I can't figure out how to properly set up rules so that this test does not fail:

    public void testTitle() throws IOException, SAXException {
        String xml = "<menu>\n" +
                "   <item title=\"Copy\">\n" +
                "      <action
class=\"javax.swing.text.DefaultEditorKit$CopyAction\"/>\n" +
                "   </item>\n" +
                "</menu>";

        Digester digester = new Digester();
        digester.addObjectCreate("*/menu", "javax.swing.JMenu", "class");
        digester.addObjectCreate("*/menu/item",
"javax.swing.JMenuItem", "class");
        digester.addObjectCreate("*/menu/item/action", null, "class");
        digester.addSetNext("*/menu/item/action", "setAction");
        digester.addSetProperties("*/menu/item", "title", "text");
        digester.addSetNext("*/menu/item", "add");

        JMenu menu = (JMenu) digester.parse(new StringReader(xml));

        assertEquals("Copy", ((JMenuItem)menu.getMenuComponent(0)).getText());
    }

Result:
   junit.framework.ComparisonFailure:
   Expected :Copy
   Actual  :copy-to-clipboard

The problem is that the rules above result in roughly the following
sequence of calls:

   JMenu menu = new JMenu()
   JMenuItem item = new JMenuItem();
   item.setTitle("Copy");
   Action action = new DefaultEditorKit.CopyAction();
   item.setAction(action);

and setting action on a menu item will override previously set text.
What I need to do is to set the item's text AFTER setting the action:

   JMenu menu = new JMenu()
   JMenuItem item = new JMenuItem();
   Action action = new DefaultEditorKit.CopyAction();
   item.setAction(action);
   item.setTitle("Copy");

any recommendations on how to change digester rules to accomplish that?


Thanks
Dmitry

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


Re: [digester] setting parent properties after the child's

Posted by Simon Kitching <sk...@apache.org>.
On Fri, 2006-12-01 at 16:24 -0800, Dmitry Beransky wrote:
> > The section titled
> >  "How do I get CallMethodRule to fire before SetNextRule?"
> > might be what you need..
> 
> thanks.  it did help.  I had to replace addSetProperties with
> addCallMethod, so the final sequence of rules now looks like:
> 
>         digester.addObjectCreate("*/menu", "javax.swing.JMenu", "class");
>         digester.addObjectCreate("*/menu/item",
> "javax.swing.JMenuItem", "class");
>         digester.addObjectCreate("*/menu/item/action", null, "class");
>         digester.addCallMethod("*/menu/item", "setText", 1, new
> Class[]{String.class});
>         digester.addCallParam("*/menu/item/", 0, "title");
>         digester.addSetNext("*/menu/item/action", "setAction");
>         digester.addSetNext("*/menu/item", "add");
> 
> wow, this feels like such a hack to me, though.  Is this the only way
> to get the behavior I want?

The fact that the setAction method on your class resets the title
property could also be considered "a hack" :-)

You are always free to write a Rule variant that does exactly what you
want; Digester is explicitly designed to allow custom rule classes to be
used.

Regards,

Simon


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


Re: [digester] setting parent properties after the child's

Posted by Dmitry Beransky <db...@dembel.org>.
> The section titled
>  "How do I get CallMethodRule to fire before SetNextRule?"
> might be what you need..

thanks.  it did help.  I had to replace addSetProperties with
addCallMethod, so the final sequence of rules now looks like:

        digester.addObjectCreate("*/menu", "javax.swing.JMenu", "class");
        digester.addObjectCreate("*/menu/item",
"javax.swing.JMenuItem", "class");
        digester.addObjectCreate("*/menu/item/action", null, "class");
        digester.addCallMethod("*/menu/item", "setText", 1, new
Class[]{String.class});
        digester.addCallParam("*/menu/item/", 0, "title");
        digester.addSetNext("*/menu/item/action", "setAction");
        digester.addSetNext("*/menu/item", "add");

wow, this feels like such a hack to me, though.  Is this the only way
to get the behavior I want?


Thanks
Dmitry

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


Re: [digester] setting parent properties after the child's

Posted by Simon Kitching <sk...@apache.org>.
On Fri, 2006-12-01 at 14:58 -0800, Dmitry Beransky wrote:
> Hi,
> 
> I can't figure out how to properly set up rules so that this test does not fail:

[snip] 

> The problem is that the rules above result in roughly the following
> sequence of calls:
> 
>    JMenu menu = new JMenu()
>    JMenuItem item = new JMenuItem();
>    item.setTitle("Copy");
>    Action action = new DefaultEditorKit.CopyAction();
>    item.setAction(action);
> 
> and setting action on a menu item will override previously set text.
> What I need to do is to set the item's text AFTER setting the action:
> 
>    JMenu menu = new JMenu()
>    JMenuItem item = new JMenuItem();
>    Action action = new DefaultEditorKit.CopyAction();
>    item.setAction(action);
>    item.setTitle("Copy");
> 
> any recommendations on how to change digester rules to accomplish that?

The digester FAQ is here:
  http://wiki.apache.org/jakarta-commons/Digester/FAQ

The section titled
  "How do I get CallMethodRule to fire before SetNextRule?"
might be what you need..

Regards,

Simon





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