commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Jakarta-commons Wiki] Update of "ResourcesUserGuideImplementations" by NiallPemberton
Date Mon, 28 Nov 2005 10:04:20 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by NiallPemberton:
http://wiki.apache.org/jakarta-commons/ResourcesUserGuideImplementations

The comment on the change is:
add User Guide implementations page

New page:
|| [wiki:ResourcesUserGuide Home] || [wiki:ResourcesUserGuideMessage Previous] ||  [wiki:ResourcesUserGuideCreating
Next] ||
----
= 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''' ||
|| [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''' ||
|| [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''' ||
|| [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''' ||
|| [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'''.
||
|| [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''' ||
|| [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''' ||

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 '''XML!ResourcesFactory''' [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 '''JDBC!ResourcesFactory''' [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 '''WebappXML!ResourcesFactory''' [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].


----
|| [wiki:ResourcesUserGuide Home] || [wiki:ResourcesUserGuideMessage Previous] ||  [wiki:ResourcesUserGuideCreating
Next] ||
----

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


Mime
View raw message