import org.hsqldb.util..RefCapablePropertyResourceBundle; ... RefCapablePropertyResourceBundle bundle = RefCapablePropertyResourceBundle.getBundle("subdir.xyz"); System.out.println("Value for '1' = (" + bundle.getString("1") + ')');Just like PropertyResourceBundle, the .properties file and the referenced files are read in from the classpath by a class loader, according to the normal ResourceBundle rules. To eliminate the need to prohibit the use of any strings in the .properties values, and to enforce consistency, you must use the following rules to when putting your referenced files into place.
REFERENCED FILE DIRECTORY is a directory named with the base name of the
properties file, and in the same parent directory. So, the referenced
file directory /a/b/c/greentea
is used to hold all reference
files for properties files /a/b/c/greentea_en_us.properties
,
/a/b/c/greentea_de.properties
,
/a/b/c/greentea.properties
, etc.
(BTW, according to ResourceBundle rules, this resource should be looked
up with name "a.b.c.greentea", not "/a/b/c..." or "a/b/c").
REFERENCED FILES themselves all have the base name of the property key,
with locale appendages exactly as the referring properties files
has, plus the suffix .text
.
So, if we have the following line in
/a/b/c/greentea_de.properties
:
1: einsthen you must have a reference text file
/a/b/c/greentea/1_de.properties
:
In reference text files,
sequences of "\r", "\n" and "\r\n" are all translated to the line
delimiter for your platform (System property line.separator
).
If one of those sequences exists at the very end of the file, it will be
eliminated (so, if you really want getString() to end with a line delimiter,
end your file with two of them).
(The file itself is never modified-- I'm talking about the value returned
by getString(String)
).
To prevent throwing at runtime due to unset variables, use a wrapper class like SqltoolRB (use SqltoolRB.java as a template). To prevent throwing at runtime due to unset System Properties, or insufficient parameters passed to getString(String, String[]), set the behavior values appropriately.
Just like all Properties files, referenced files must use ISO-8859-1 encoding, with unicode escapes for characters outside of ISO-8859-1 character set. But, unlike Properties files, \ does not need to be escaped for normal usage.
The getString() methods with more than one parameter substitute for "positional" parameters of the form "%{1}". The getExpandedString() methods substitute for System Property names of the form "${1}". In both cases, you can interpose :+ and a string between the variable name and the closing }. This works just like the Bourne shell ${x:+y} feature. If "x" is set, then "y" is returned, and "y" may contain references to the original variable without the curly braces. In this file, I refer to the y text as the "conditional string". One example of each type:
Out val = (${condlSysProp:+Prop condlSysProp is set to $condlSysProp.}) Out val = (%{2:+Pos Var #2 is set to %2.}) OUTPUT if neither are set: Out val = () Out val = () OUTPUT if condlSysProp=alpha and condlPLvar=beta: Out val = (Prop condlSysProp is set to alpha.) Out val = (Pos Var #2 is set to beta.)This feature has the following limitations.
- The conditional string may only contain the primary variable.
- Inner instances of the primary variable may not use curly braces, and therefore the variable name must end at a word boundary.
- Author:
- Blaine Simpson (blaine dot simpson at admc dot com)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final String
static final int
static final int
-
Method Summary
Modifier and TypeMethodDescriptiongetBundle
(String baseName, ClassLoader loader, Class<? extends Enum<?>> loaderClass) Use like java.util.ResourceBundle.getBundle(String).Use exactly like java.util.ResourceBundle.get(String, Locale, ClassLoader).getExpandedString
(String key, int behavior) Same as getString(), but expands System Variables specified in property values like ${sysvarname}.getExpandedString
(String key, String[] subs, int missingPropertyBehavior, int missingPosValueBehavior) getKeys()
Returns value defined in this RefCapablePropertyResourceBundle's .properties file, unless that value is empty.Replaces positional substitution patterns of the form %{\d} with corresponding element of the given subs array.static String
toNativeLs
(String inString) toString()
Just identifies this RefCapablePropertyResourceBundle instance.
-
Field Details
-
LS
-
THROW_BEHAVIOR
public static final int THROW_BEHAVIOR- See Also:
-
EMPTYSTRING_BEHAVIOR
public static final int EMPTYSTRING_BEHAVIOR- See Also:
-
NOOP_BEHAVIOR
public static final int NOOP_BEHAVIOR- See Also:
-
-
Method Details
-
getKeys
-
getExpandedString
Same as getString(), but expands System Variables specified in property values like ${sysvarname}.- Parameters:
key
- Stringbehavior
- int- Returns:
- String
-
posSubst
Replaces positional substitution patterns of the form %{\d} with corresponding element of the given subs array. Note that %{\d} numbers are 1-based, so we lok for subs[x-1].- Parameters:
s
- Stringsubs
- String[]behavior
- int- Returns:
- String
-
getExpandedString
-
getString
-
toString
Just identifies this RefCapablePropertyResourceBundle instance. -
getString
Returns value defined in this RefCapablePropertyResourceBundle's .properties file, unless that value is empty. If the value in the .properties file is empty, then this returns the entire contents of the referenced text file.- Parameters:
key
- String- Returns:
- String
- See Also:
-
toNativeLs
- Parameters:
inString
- Input string with \n definitively indicating desired position for line separators.- Returns:
- If platform's line-separator is \n, then just returns inString. Otherwise returns a copy of inString, with all \n's transformed to the platform's line separators.
-
getBundle
public static RefCapablePropertyResourceBundle getBundle(String baseName, ClassLoader loader, Class<? extends Enum<?>> loaderClass) Use like java.util.ResourceBundle.getBundle(String). ClassLoader is required for our getBundles()s, since it is impossible to get the "caller's" ClassLoader without using JNI (i.e., with pure Java).- Parameters:
baseName
- Stringloader
- ClassLoaderloaderClass
- Class- Returns:
- RefCapablePropertyResourceBundle
- See Also:
-
getBundle
public static RefCapablePropertyResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader, Class<? extends Enum<?>> loaderClass) Use exactly like java.util.ResourceBundle.get(String, Locale, ClassLoader).- Parameters:
baseName
- Stringlocale
- Localeloader
- ClassLoaderloaderClass
- Class- Returns:
- RefCapablePropertyResourceBundle
- See Also:
-