aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/org/apache/fop/util/text/AdvancedMessageFormat.java')
-rw-r--r--src/java/org/apache/fop/util/text/AdvancedMessageFormat.java90
1 files changed, 45 insertions, 45 deletions
diff --git a/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java b/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
index a2169156a..8c26bd622 100644
--- a/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
+++ b/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
@@ -45,13 +45,13 @@ public class AdvancedMessageFormat {
/** Regex that matches "," but not "\," (escaped comma) */
static final Pattern COMMA_SEPARATOR_REGEX = Pattern.compile("(?<!\\\\),");
-
+
private static final Map PART_FACTORIES = new java.util.HashMap();
private static final List OBJECT_FORMATTERS = new java.util.ArrayList();
private static final Map FUNCTIONS = new java.util.HashMap();
-
+
private CompositePart rootPart;
-
+
static {
Iterator iter;
iter = Service.providers(PartFactory.class, true);
@@ -69,7 +69,7 @@ public class AdvancedMessageFormat {
FUNCTIONS.put(function.getName(), function);
}
}
-
+
/**
* Construct a new message format.
* @param pattern the message format pattern.
@@ -77,13 +77,13 @@ public class AdvancedMessageFormat {
public AdvancedMessageFormat(CharSequence pattern) {
parsePattern(pattern);
}
-
+
private void parsePattern(CharSequence pattern) {
rootPart = new CompositePart(false);
StringBuffer sb = new StringBuffer();
parseInnerPattern(pattern, rootPart, sb, 0);
}
-
+
private int parseInnerPattern(CharSequence pattern, CompositePart parent,
StringBuffer sb, int start) {
assert sb.length() == 0;
@@ -155,7 +155,7 @@ public class AdvancedMessageFormat {
}
return i - start;
}
-
+
private Part parseField(String field) {
String[] parts = COMMA_SEPARATOR_REGEX.split(field, 3);
String fieldName = parts[0];
@@ -203,20 +203,20 @@ public class AdvancedMessageFormat {
public void format(Map params, StringBuffer target) {
rootPart.write(target, params);
}
-
+
/**
* Represents a message template part. This interface is implemented by various variants of
* the single curly braces pattern ({field}, {field,if,yes,no} etc.).
*/
public interface Part {
-
+
/**
* Writes the formatted part to a string buffer.
* @param sb the target string buffer
* @param params the parameters to work with
*/
void write(StringBuffer sb, Map params);
-
+
/**
* Indicates whether there is any content that is generated by this message part.
* @param params the parameters to work with
@@ -224,12 +224,12 @@ public class AdvancedMessageFormat {
*/
boolean isGenerated(Map params);
}
-
+
/**
* Implementations of this interface parse a field part and return message parts.
*/
public interface PartFactory {
-
+
/**
* Creates a new part by parsing the values parameter to configure the part.
* @param fieldName the field name
@@ -237,26 +237,26 @@ public class AdvancedMessageFormat {
* @return the new message part
*/
Part newPart(String fieldName, String values);
-
+
/**
* Returns the name of the message part format.
* @return the name of the message part format
*/
String getFormat();
}
-
+
/**
* Implementations of this interface format certain objects to strings.
*/
public interface ObjectFormatter {
-
+
/**
* Formats an object to a string and writes the result to a string buffer.
* @param sb the target string buffer
* @param obj the object to be formatted
*/
void format(StringBuffer sb, Object obj);
-
+
/**
* Indicates whether a given object is supported.
* @param obj the object
@@ -264,40 +264,40 @@ public class AdvancedMessageFormat {
*/
boolean supportsObject(Object obj);
}
-
+
/**
* Implementations of this interface do some computation based on the message parameters
* given to it. Note: at the moment, this has to be done in a local-independent way since
* there is no locale information.
*/
public interface Function {
-
+
/**
* Executes the function.
* @param params the message parameters
* @return the function result
*/
Object evaluate(Map params);
-
+
/**
* Returns the name of the function.
* @return the name of the function
*/
Object getName();
}
-
+
private static class TextPart implements Part {
-
+
private String text;
-
+
public TextPart(String text) {
this.text = text;
}
-
+
public void write(StringBuffer sb, Map params) {
sb.append(text);
}
-
+
public boolean isGenerated(Map params) {
return true;
}
@@ -307,15 +307,15 @@ public class AdvancedMessageFormat {
return this.text;
}
}
-
+
private static class SimpleFieldPart implements Part {
-
+
private String fieldName;
-
+
public SimpleFieldPart(String fieldName) {
this.fieldName = fieldName;
}
-
+
public void write(StringBuffer sb, Map params) {
if (!params.containsKey(fieldName)) {
throw new IllegalArgumentException(
@@ -329,13 +329,13 @@ public class AdvancedMessageFormat {
Object obj = params.get(fieldName);
return obj != null;
}
-
+
/** {@inheritDoc} */
public String toString() {
return "{" + this.fieldName + "}";
}
}
-
+
/**
* Formats an object to a string and writes the result to a string buffer. This method
* usually uses the object's <code>toString()</code> method unless there is an
@@ -363,18 +363,18 @@ public class AdvancedMessageFormat {
}
}
}
-
+
private static class FunctionPart implements Part {
-
+
private Function function;
-
+
public FunctionPart(String functionName) {
this.function = getFunction(functionName);
if (this.function == null) {
throw new IllegalArgumentException("Unknown function: " + functionName);
}
}
-
+
public void write(StringBuffer sb, Map params) {
Object obj = this.function.evaluate(params);
formatObject(obj, sb);
@@ -384,28 +384,28 @@ public class AdvancedMessageFormat {
Object obj = this.function.evaluate(params);
return obj != null;
}
-
+
/** {@inheritDoc} */
public String toString() {
return "{#" + this.function.getName() + "}";
}
}
-
+
private static class CompositePart implements Part {
-
+
protected List parts = new java.util.ArrayList();
private boolean conditional;
private boolean hasSections = false;
-
+
public CompositePart(boolean conditional) {
this.conditional = conditional;
}
-
+
private CompositePart(List parts) {
this.parts.addAll(parts);
this.conditional = true;
}
-
+
public void addChild(Part part) {
if (part == null) {
throw new NullPointerException("part must not be null");
@@ -417,7 +417,7 @@ public class AdvancedMessageFormat {
this.parts.add(part);
}
}
-
+
public void newSection() {
if (!hasSections) {
List p = this.parts;
@@ -428,7 +428,7 @@ public class AdvancedMessageFormat {
}
this.parts.add(new CompositePart(true));
}
-
+
public void write(StringBuffer sb, Map params) {
if (hasSections) {
Iterator iter = this.parts.iterator();
@@ -473,14 +473,14 @@ public class AdvancedMessageFormat {
return true;
}
}
-
+
/** {@inheritDoc} */
public String toString() {
return this.parts.toString();
}
}
-
-
+
+
static String unescapeComma(String string) {
return string.replaceAll("\\\\,", ",");
}