On Mon, 8 Jul 2002, Tero P Paananen wrote:

> Date: Mon, 8 Jul 2002 14:36:25 -0400
> From: Tero P Paananen <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: RE:  XML based application resources?
>
> > Is there an extension or maybe some trick in Struts which
> > would allow me to have xml-based application resource files?
>
> Extend PropertyMessageResources.java (& the corresponding factory
> class) and implement your own loadLocale() method, then, in your
> struts-config.xml, point application and factory attributes to
> your classes.
>
> > <ApplicationResources>
> >   <message key="color">
> >     <value lang="en_US">Color</value>
> >     <value lang="de">Farbe</value>
> >     <value lang="hr">Boja</value>
> >   </message>
> >   <!-- etc... -->
> > </ApplicationResources>
>
> I don't think you can't parse something like that with the
> Digester class.
>

Actually, parsing this with Digester would be pretty easy.  Consider that
you have a MyMessageBean class with the following method signatures:

  public void setKey(String key);
  public void addTranslation(String language, String value);
  public String findTranslation(String key, String language);

on it.  You could have digester rules something like this:

  digester.push(...); // Object with an addMessage() method defined

  digester.addObjectCreate("ApplicationResources/message",
                           "com.mycompany.MyMessageBean");
  digester.addSetProperties("ApplicationResources/message");
  digester.addSetNext("ApplicationResources/message",
                      "addMessage",
                      "com.mycompany.MyMessageBean");

  digester.addCallMethod("ApplicationResources/message/value",
                         "addTranslation", 2);
  digester.addCallParam("ApplicationResources/message/value",
                        0, "lang"); // Picks up "lang" attribute
  digester.addCallParam("ApplicationResources/message/value",
                        1);         // Picks up element body

  digester.parse(...);

Thus, you'd end up with a HashMap or something of MyMessageBean instances,
keyed by the message key.  This data structure could then be accessed by a
custom MessageResources implementation that used the specified Locale to
figure out which language you wanted.

>                               -TPP
>

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to