You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e18eff07-6100-4327-b63f-78622d45e49c"><ac:plain-text-body><![CDATA[

[wiki:ResourcesUserGuide Contents]

[wiki:ResourcesUserGuideIntro Getting Started]

[wiki:ResourcesUserGuideMessages Messages]

[wiki:ResourcesUserGuideImplementations Standard]

[wiki:ResourcesUserGuideCreating Creating]

]]></ac:plain-text-body></ac:structured-macro>


3. Standard Resources / ResourcesFactory Implementations

Commons Resources ships with the following implementations of [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/Resources.html Resources] and [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/ResourcesFactory.html ResourcesFactory] provided:

Resources

ResourcesFactory

Description

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5ff2f839-8ad2-46f5-b38d-9d25c5a8947a"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/PropertyResources.html PropertyResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/PropertyResourcesFactory.html PropertyResourcesFactory]

retrieves values from properties files

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="22090d9d-1d5a-4070-8881-7c6845bcadfc"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/XMLResources.html XMLResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/XMLResourcesFactory.html XMLResourcesFactory]

retrieves values from XML documents

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="831d2ab3-e077-43f4-bb9a-8f485fd83a80"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/JDBCResources.html JDBCResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/PropertyResourcesFactory.html PropertyResourcesFactory]

retrieves values from a database using JDBC

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8b495216-5423-4920-b5a5-44af281b2ca0"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappPropertyResources.html WebappPropertyResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappPropertyResourcesFactory.html WebappPropertyResourcesFactory]

Web App: retrieves values from properties files.

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b6ff0df5-f4bd-4db1-9dfa-794ad78eb5d4"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappXMLResources.html WebappXMLResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappXMLResourcesFactory.html WebappXMLResourcesFactory]

Web App: retrieves values from XML documents

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="49eec8b4-4a16-481a-91fb-448290d576d9"><ac:plain-text-body><![CDATA[

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/ResourceBundleResources.html ResourceBundleResources]

[http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.html ResourceBundleResourcesFactory]

wraps java.util.ResourceBundle

]]></ac:plain-text-body></ac:structured-macro>

The following sections describe how to use these implementations:

  • 3.1 Property Resources
  • 3.2 XML Resources
  • 3.3 JDBC Resources
  • 3.4 Web Application Property Resources
  • 3.5 Web Application XML Resources
  • 3.6 java.util.ResourceBundle Resources

3.1 Property Resources

PropertyResources wrap a family (one per Locale) of properties files that share a base URL and have name suffixes reflecting the Locale.

Resources are looked up in a hierarchy of properties files identical to that performed by java.util.ResourceBundle. For example, if the configuration URL is passed as file:c:/myapp/foo/Bar, then...

File

Locale Suffix

for

c:/myapp/foo/Bar.properties

 

default resources

c:/myapp/foo/Bar_en.properties

en

English resources

c:/myapp/foo/Bar_en_US.properties

en_US

US English resources

c:/myapp/foo/Bar_fr_GB.properties

en_GB

UK English resources

N.B. PropertyResources assumes all files will end with .properties

In these properties files, you specify key/value pairs in the normal way, for example...

     error.date=Date is invalid.
     error.number=Number is invalid.

To use PropertyResources, you create it using it's factory and then use in the normal way...

       // Create the ResourcesFactory
       ResourcesFactory factory = new PropertyResourcesFactory();

       // Create the Resources
       Resources resources = factory.createResources("Bar", "file:c:/myapp/foo/Bar");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See PropertyResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/PropertyResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/PropertyResources.html source] and PropertyResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/PropertyResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/PropertyResourcesFactory.html source].


3.2 XML Resources

XMLResources works in pretty much the same way as PropertyResources, except the files are in XML format and XMLResources assumes a .xml file extension. Using the same example, if the configuration URL is passed as file:c:/myapp/foo/Bar then... ...

File

Locale Suffix

for

c:/myapp/foo/Bar.xml

 

default resources

c:/myapp/foo/Bar_en.xml

en

English resources

c:/myapp/foo/Bar_en_US.xml

en_US

US English resources

c:/myapp/foo/Bar_fr_GB.xml

en_GB

UK English resources

In these XML files, you specify resources in the following way...

     <resources>

           <resource id="error.date">
                 Date is invalid.
           </resource>

           <resource id="error.number">
                 Number is invalid.
           </resource>

     </resources>

To use XMLResources, you create it using it's factory and then use in the normal way...

       // Create the ResourcesFactory
       ResourcesFactory factory = new XMLResourcesFactory();

       // Create the Resources
       Resources resources = factory.createResources("Bar", "file:c:myapp/foo/Bar");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See XMLResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/XMLResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/XMLResources.html source] and XMLResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/XMLResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/XMLResourcesFactory.html source].


3.3 JDBC Resources

JDBCResources retrieves messages from a database using JDBC. The table you use to store the messages needs three columns containing:

  • the locale value (e.g. "en_US" for US English)
  • the message key
  • the message text

The names of these columns and the table is configured, along with the Driver information, in a JDBC Properties file. For example in c:/myapp/foo/Bar.properties...

        jdbc.connect.driver   = org.gjt.mm.mysql.Driver
        jdbc.connect.url      = jdbc:mysql://localhost/MyDatabase
        jdbc.connect.login    = MyUserName
        jdbc.connect.password = MyPassword

        jdbc.sql.table         = MyMessages
        jdbc.sql.locale.column = locale_key
        jdbc.sql.key.column    = message_key
        jdbc.sql.val.column    = message_text

N.B. JDBCResources appends .properties to the URL you supply for this file.

To use JDBCResources, you create it using it's factory, with the URL for the JDBC Properties file and then use in the normal way...

       // Create the ResourcesFactory
       ResourcesFactory factory = new JDBCResourcesFactory();

       // Create the Resources
       Resources resources = factory.createResources("Bar", "file:c:myapp/foo/Bar");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See JDBCResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/JDBCResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/JDBCResources.html source] and JDBCResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/JDBCResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/JDBCResourcesFactory.html source].


3.4 Web Application Property Resources

WebappPropertyResources works the same way as PropertyResources, but in a Web Application environment. The only difference is you specify a context relative URL and you need to initialize the ServletContext....

       // Create the ResourcesFactory
       ResourcesFactory factory = new WebappPropertyResourcesFactory();
       factory.setServletContext(servletContext);

       // Create the Resources
       Resources resources = factory.createResources("Bar", "/org/apache/struts/Bar");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See WebappPropertyResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappPropertyResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/WebappPropertyResources.html source] and WebappPropertyResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappPropertyResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/WebappPropertyResourcesFactory.html source].


3.5 Web Application XML Resources

WebappXMLResources works the same way as XMLResources, but in a Web Application environment. The only difference is you specify a context relative URL and you need to initialize the ServletContext....

       // Create the ResourcesFactory
       ResourcesFactory factory = new WebappXMLResourcesFactory();
       factory.setServletContext(servletContext);

       // Create the Resources
       Resources resources = factory.createResources("Bar", "/org/apache/struts/Bar");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See !WebappXMLResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappXMLResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/WebappXMLResources.html source] and WebappXMLResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/WebappXMLResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/WebappXMLResourcesFactory.html source].


3.6 java.util.ResourceBundle Resources

ResourceBundleResources is a Resources implementation that wraps a java.util.ResourceBundle instance.

       // Create the ResourcesFactory
       ResourcesFactory factory = new ResourceBundleResourcesFactory();

       // Create the Resources
       Resources resources = factory.createResources("Bar", "BarResources");

       // Retrieve an i18n String value
       String msg = resources.getString("some.key", locale, null);

See ResourceBundleResources [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/ResourceBundleResources.html JavaDoc] /
[http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/ResourceBundleResources.html source] and ResourceBundleResourcesFactory [http://jakarta.apache.org/commons/resources/apidocs/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.html JavaDoc] / [http://jakarta.apache.org/commons/resources/xref/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.html source].


<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="010042cf-042c-4244-809b-bef914c0f4e0"><ac:plain-text-body><![CDATA[

[wiki:ResourcesUserGuide Contents]

[wiki:ResourcesUserGuideIntro Getting Started]

[wiki:ResourcesUserGuideMessages Messages]

[wiki:ResourcesUserGuideImplementations Standard]

[wiki:ResourcesUserGuideCreating Creating]

]]></ac:plain-text-body></ac:structured-macro>

  • No labels