<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f68261f6-e8b8-46c4-8eed-cf78e5c40a06"><ac:plain-text-body><![CDATA[ |
[http://jakarta.apache.org/commons/resources/ Home] |
[wiki:Resources Wiki] |
[wiki:ResourcesUserGuide Guide] |
[wiki:ResourcesUserGuideIntro Getting Started] |
[wiki:ResourcesUserGuideMessages Messages] |
[wiki:ResourcesUserGuideImplementations Standard] |
[wiki:ResourcesUserGuideCreating Creating] |
[http://jakarta.apache.org/commons/resources/apidocs/ API] |
[http://jakarta.apache.org/commons/resources/xref/ XREF] |
]]></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="b9006dce-b0c0-4eda-a8c1-245120beb878"><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="9229f698-a55e-4186-86d9-17d32d086c42"><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="263b1475-21ac-4559-9cc4-927978cc6a60"><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="42f467a3-17a6-44c9-966a-eeda5dd46765"><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="362caeaa-e772-482d-9a58-3f64d2c2b7e2"><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="0e5f86d2-70da-4142-87af-de8a14948e93"><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 |
|
|
default resources |
|
en |
English resources |
|
en_US |
US English resources |
|
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.getResources("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 |
|
|
default resources |
|
en |
English resources |
|
en_US |
US English resources |
|
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.getResources("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.getResources("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.getResources("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.getResources("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.getResources("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="442c25ac-c375-4f9f-a13f-3d893c84f3ce"><ac:plain-text-body><![CDATA[ |
[http://jakarta.apache.org/commons/resources/ Home] |
[wiki:Resources Wiki] |
[wiki:ResourcesUserGuide Guide] |
[wiki:ResourcesUserGuideIntro Getting Started] |
[wiki:ResourcesUserGuideMessages Messages] |
[wiki:ResourcesUserGuideImplementations Standard] |
[wiki:ResourcesUserGuideCreating Creating] |
[http://jakarta.apache.org/commons/resources/apidocs/ API] |
[http://jakarta.apache.org/commons/resources/xref/ XREF] |
]]></ac:plain-text-body></ac:structured-macro> |