You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Mike Engelhart <me...@earthtrip.com> on 2000/06/21 23:27:40 UTC

XMLResourceBundle

I've put together 2 classes that are intended to be a simple equivalent to
the java.util.ResourceBundle's that have become tedious for me to use in an
XML environment. 
I've attached the source for the 2 classes XMLResourceBundleFactory and
XMLResourceBundle.

XMLResourceBundleFactory has the standard getBundle(String name) and
getBundle(String name, Locale loc) factory methods and implements a simple
cache using SoftReference's (btw is C2 skipping on JDK 1.1.x??).   There is
a lot missing in comparison to all the things that java.util.ResourceBundle
handles such as automatic ResourceBundle fallback (which shouldn't be too
hard to implement) and a number of other things.
The XMLResourceBundle class has a getString(String xPath) which takes an
xPath and returns the string value of the Element that it's pointing to (see
sample schema below - this needs to be determined or a better
getString(xPath) needs to be written).   Also there is a getRole(String
xPath) which returns a Node which can be passed to a getString(Node role,
String key) method if you're going to be using the same Node over and over.
To speed up development right now it's using the XPathAPI  which is a sample
class that comes with the Xalan distribution for doing XPath manipulation
(today is the first day I've used the XPath API so I don't really know what
I'm doing).  The method calls appear to  be very slow (it mentions this in
the inline documentation) so if anyone knows anything about the XPath
low-level API and is interested in getting the performance increased that
would be great.   

I just wanted to get these out to anyone who might be interested....

I'm using the schema that Neeme Praks had used in one of his emails but this
can easily be changed:   Here's a sample resource.xml file:

<?xml version="1.0"?>
<resources xml:lang="en">
  <forms>
      <contacts-search>
          <desc>
            <searchfor>find:</searchfor>
            <field-to-search>what:</field-to-search>
            <from>where:</from>
            <from-mycontacts>my contacts</from-mycontacts>
            <from-allmembers>all members</from-allmembers>
          </desc>
    
          <input>
              <field-to-search>
                <all>all</all>
                <firstname>first name</firstname>
                <lastname>last name</lastname>
                <phone>phone</phone>
                <email>e-mail</email>
              </field-to-search>
            <submit>Search</submit>
          </input>
      </contacts-search>
  </forms>
</resources>


and sample usage:
XMLResourceBundle bundle = XMLResourceBundleFactory.getBundle("resource");
System.out.println(bundle.getString("/resources/forms/contacts-search/desc/f
rom-mycontacts"));
System.out.println(bundle.getString("/resources/forms/contacts-search/input/
field-to-search/lastname"));
System.out.println(bundle.getString("/resources/forms/contacts-search/desc/f
rom"));
System.out.println(bundle.getString("/resources/forms/contacts-search/input/
field-to-search/phone"));
System.out.println(bundle.getString("/resources/forms/contacts-search/input/
field-to-search/email"));
System.out.println(bundle.getString("/resources/forms/contacts-search/desc/s
earchfor"));
Node role = 
bundle.getRole("/resources/forms/contacts-search/input/field-to-search");
System.out.println(bundle.getString(role, "lastname"));
System.out.println(bundle.getString(role, "firstname"));
System.out.println(bundle.getString(role, "phone"));



Mike


Re: XMLResourceBundle

Posted by Mike Engelhart <me...@earthtrip.com>.
on 6/23/00 5:47 AM, Niclas Hedhman at niclas@localbar.com wrote:

> I would like to offer an alternative layout for using XML Resource strings,
> which would allow for a DTD.
As the current version is using XPath, does it really matter what DTD you
have?  I mean Cocoon itself may want to require a localization DTD, but
XMLResourceBundle doesn't really care as long as you pass in the correct
path??  Am I missing something?

Mike


Re: XMLResourceBundle

Posted by Niclas Hedhman <ni...@localbar.com>.
I would like to offer an alternative layout for using XML Resource strings,
which would allow for a DTD.

Using Mike's example;

<?xml version="1.0"?>
  <resources xml:lang="en">
    <forms>
      <form id="contacts-search">
        <labels>
          <label id="searchfor">find:</label>
          <label id="field-to-search">what:</label>
          <label id="from">where:</label>
          <label id="from-mycontacts">my contacts</label>
          <label id="from-allmembers">all members</label>
        </labels>

        <fields>
          <field id="all">all</field>
          <field id="firstname">first name</field>
          <field id="lastname">last name</field>
          <field id="phone">phone</field>
          <field id="email">e-mail</field>
          <submit>Search</submit>
        </fields>
  </forms>
</resources>


and sample usage:
System.out.println(bundle.getString("/resources/forms/form[@id='contacts-search']/labels/label[@id='from-mycontacts']"));

Not nearly as elegant, I most certainly agree.
Advantage. A fixed DTD!!! No changes to the DTD when the resources are updated.




Re: XMLResourceBundle

Posted by Mike Engelhart <me...@earthtrip.com>.
on 6/23/00 3:24 AM, Niclas Hedhman at niclas@localbar.com wrote:

> 
> Thanks for a nice util.
> 
> And I am 'stealing' it straight off, since I was half way with a similar
> thing.
> 
> However,
> private static Document resource;
> 
> ????
> That can't be right. You would want more than one resource document, right?
> 
> Niclas
no problem.  I've done a lot to it since then.  For one (no private static
Document :-))..
Now it handles parent inheritance like java.util.ResourceBundle's allow and
also builds the whole parent chain at first access of the bundle.

For example if you have a parent hierachy of XML files such as:
*** words_en_us.xml ***
<resources>
    <words>
        <word key="THREE">
            <value>Three</value>
        <word>
    </words>
</resources>

*** words_en.xml ***
<resources>
    <words>
        <word key="ONE">
            <value>Two</value>
        <word>
    </words>
</resources>

*** words.xml ***
<resources>
    <words>
        <word key="ZERO">
            <value>Zero</value>
        <word>
    </words>
</resources>

You can call 
XMLResourceBundle bundle = XMLResourceBundle.getBundle("words", new
Locale("en", "us");
String word = 
bundle.getString("/resources/words/word[@key=\"ZERO\"]/value");

The XMLResourceBundle's getString() will keep walking the parent chain until
it finds the correct match or else throw a MissingResourceException.

Still the issue remains that this class is much, much slower for accessing
items than ResourceBundle's are.   I think that ResourceBundle's load their
properties into a Hashtable so access is very fast - in some tests, randomly
accessing items from a 2000 item Properties file took on average - 0ms
whereas with that same file stored as an XML file, the fastest random access
time (even when done in a loop) was 489ms.  I've discussed this with the
Scott on the Xalan list and there are some possible workarounds that I'm
going to look at and also Xalan 2.0.0 should speed things up considerably as
well.   For smaller sets though it works nicely.

I'm not sure if this stuff has any value for Cocoon2 (1.x?) or not so I'm
not sure if I should post it to this list or just privately mail it.  I
don't want to load up this list with source code.

Mike


Re: XMLResourceBundle

Posted by Niclas Hedhman <ni...@localbar.com>.
Thanks for a nice util.

And I am 'stealing' it straight off, since I was half way with a similar thing.

However,
private static Document resource;

????
That can't be right. You would want more than one resource document, right?

Niclas

Mike Engelhart wrote:

> I've put together 2 classes that are intended to be a simple equivalent to
> the java.util.ResourceBundle's that have become tedious for me to use in an
> XML environment.
> I've attached the source for the 2 classes XMLResourceBundleFactory and
> XMLResourceBundle.
>
> XMLResourceBundleFactory has the standard getBundle(String name) and
> getBundle(String name, Locale loc) factory methods and implements a simple
> cache using SoftReference's (btw is C2 skipping on JDK 1.1.x??).   There is
> a lot missing in comparison to all the things that java.util.ResourceBundle
> handles such as automatic ResourceBundle fallback (which shouldn't be too
> hard to implement) and a number of other things.
> The XMLResourceBundle class has a getString(String xPath) which takes an
> xPath and returns the string value of the Element that it's pointing to (see
> sample schema below - this needs to be determined or a better
> getString(xPath) needs to be written).   Also there is a getRole(String
> xPath) which returns a Node which can be passed to a getString(Node role,
> String key) method if you're going to be using the same Node over and over.
> To speed up development right now it's using the XPathAPI  which is a sample
> class that comes with the Xalan distribution for doing XPath manipulation
> (today is the first day I've used the XPath API so I don't really know what
> I'm doing).  The method calls appear to  be very slow (it mentions this in
> the inline documentation) so if anyone knows anything about the XPath
> low-level API and is interested in getting the performance increased that
> would be great.
>
> I just wanted to get these out to anyone who might be interested....
>
> I'm using the schema that Neeme Praks had used in one of his emails but this
> can easily be changed:   Here's a sample resource.xml file:
>
> <?xml version="1.0"?>
> <resources xml:lang="en">
>   <forms>
>       <contacts-search>
>           <desc>
>             <searchfor>find:</searchfor>
>             <field-to-search>what:</field-to-search>
>             <from>where:</from>
>             <from-mycontacts>my contacts</from-mycontacts>
>             <from-allmembers>all members</from-allmembers>
>           </desc>
>
>           <input>
>               <field-to-search>
>                 <all>all</all>
>                 <firstname>first name</firstname>
>                 <lastname>last name</lastname>
>                 <phone>phone</phone>
>                 <email>e-mail</email>
>               </field-to-search>
>             <submit>Search</submit>
>           </input>
>       </contacts-search>
>   </forms>
> </resources>
>
> and sample usage:
> XMLResourceBundle bundle = XMLResourceBundleFactory.getBundle("resource");
> System.out.println(bundle.getString("/resources/forms/contacts-search/desc/f
> rom-mycontacts"));
> System.out.println(bundle.getString("/resources/forms/contacts-search/input/
> field-to-search/lastname"));
> System.out.println(bundle.getString("/resources/forms/contacts-search/desc/f
> rom"));
> System.out.println(bundle.getString("/resources/forms/contacts-search/input/
> field-to-search/phone"));
> System.out.println(bundle.getString("/resources/forms/contacts-search/input/
> field-to-search/email"));
> System.out.println(bundle.getString("/resources/forms/contacts-search/desc/s
> earchfor"));
> Node role =
> bundle.getRole("/resources/forms/contacts-search/input/field-to-search");
> System.out.println(bundle.getString(role, "lastname"));
> System.out.println(bundle.getString(role, "firstname"));
> System.out.println(bundle.getString(role, "phone"));