mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-10 12:05:06 +00:00
reformatted to match other Lang code
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0028e37c1c
commit
dce7e4c095
@ -36,10 +36,12 @@
|
||||
* support any "format" operations applied to the objects before substitution
|
||||
* into the target string.
|
||||
*
|
||||
* Originally org.apache.commons.digester.substitution.MultiVariableExpander
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
|
||||
public class MappedMessageFormat {
|
||||
|
||||
private int nEntries = 0;
|
||||
private ArrayList markers = new ArrayList(2);
|
||||
private ArrayList sources = new ArrayList(2);
|
||||
@ -52,7 +54,7 @@ public MappedMessageFormat() {
|
||||
* strings passed to the "format" methods.
|
||||
*/
|
||||
public void addSource(String marker, Map source) {
|
||||
++nEntries;
|
||||
nEntries++;
|
||||
markers.add(marker);
|
||||
sources.add(source);
|
||||
}
|
||||
@ -65,11 +67,8 @@ public void addSource(String marker, Map source) {
|
||||
* a variable which is not known to the specified source.
|
||||
*/
|
||||
public String format(String param) {
|
||||
for(int i=0; i<nEntries; ++i) {
|
||||
param = format(
|
||||
param,
|
||||
(String) markers.get(i),
|
||||
(Map) sources.get(i));
|
||||
for(int i=0; i<nEntries; i++) {
|
||||
param = format( param, (String) markers.get(i), (Map) sources.get(i));
|
||||
}
|
||||
return param;
|
||||
}
|
||||
@ -94,33 +93,26 @@ public static String format(String str, String marker, Map source) {
|
||||
int markLen = startMark.length();
|
||||
|
||||
int index = 0;
|
||||
for(;;)
|
||||
{
|
||||
while(true) {
|
||||
index = str.indexOf(startMark, index);
|
||||
if (index == -1)
|
||||
{
|
||||
if (index == -1) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int startIndex = index + markLen;
|
||||
if (startIndex > str.length())
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"var expression starts at end of string");
|
||||
if (startIndex > str.length()) {
|
||||
throw new IllegalArgumentException("var expression starts at end of string");
|
||||
}
|
||||
|
||||
int endIndex = str.indexOf("}", index + markLen);
|
||||
if (endIndex == -1)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"var expression starts but does not end");
|
||||
if (endIndex == -1) {
|
||||
throw new IllegalArgumentException("var expression starts but does not end");
|
||||
}
|
||||
|
||||
String key = str.substring(index+markLen, endIndex);
|
||||
Object value = source.get(key);
|
||||
if (value == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"parameter [" + key + "] is not defined.");
|
||||
throw new IllegalArgumentException("parameter [" + key + "] is not defined.");
|
||||
}
|
||||
String varValue = value.toString();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user