Return-Path: Mailing-List: contact cocoon-dev-help@xml.apache.org; run by ezmlm Delivered-To: mailing list cocoon-dev@xml.apache.org Received: (qmail 47789 invoked from network); 23 Jun 2000 08:14:39 -0000 Received: from unknown (HELO envision.asiaconnect.com.my) (root@202.190.60.242) by locus.apache.org with SMTP; 23 Jun 2000 08:14:39 -0000 Received: from localbar.com (IDENT:niclas@localhost [127.0.0.1]) by envision.asiaconnect.com.my (8.9.3/8.8.7) with ESMTP id QAA29869 for ; Fri, 23 Jun 2000 16:24:22 +0800 Sender: niclas@envision.asiaconnect.com.my Message-ID: <39531EB5.BA6B0152@localbar.com> Date: Fri, 23 Jun 2000 16:24:21 +0800 From: Niclas Hedhman Organization: Bali Automation Sdn Bhd X-Mailer: Mozilla 4.61 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 To: cocoon-dev@xml.apache.org Subject: Re: XMLResourceBundle References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N 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: > > > > > > > find: > what: > where: > my contacts > all members > > > > > all > first name > last name > phone > e-mail > > Search > > > > > > 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"));