001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.util;
009:
010: import java.util.Iterator;
011: import java.util.Map;
012:
013: /**
014: * Formatting functions.
015: */
016: public class Format {
017:
018: /**
019: * Substitutes the indexed parameters.
020: *
021: * @param text the text
022: * @param param the parameter
023: * @return the new text
024: */
025: public static String substitute(String text, int param) {
026: return substitute(text, safeRegexReplacement("" + param));
027: }
028:
029: /**
030: * Substitutes the named parameters. The passed keys and values must be
031: * Strings.
032: *
033: * @param text the text
034: * @param params the parameters
035: * @return the new text
036: */
037: public static String substitute(String text, Map params) {
038: Iterator it = params.keySet().iterator();
039: while (it.hasNext()) {
040: String key = (String) it.next();
041: text = text.replaceAll("\\{" + key + "}",
042: safeRegexReplacement((String) params.get(key)));
043: }
044: return text;
045: }
046:
047: /**
048: * Substitutes the indexed parameters.
049: *
050: * @param text the text
051: * @param param the parameter
052: * @return the new text
053: */
054: public static String substitute(String text, String param) {
055: return text.replaceAll("\\{0}", safeRegexReplacement(param));
056: }
057:
058: /**
059: * Substitutes the named parameter.
060: *
061: * @param text the text
062: * @param name the parameter name
063: * @return the parameter value
064: */
065: public static String substitute(String text, String name,
066: String value) {
067: return text = text.replaceAll("\\{" + name + "}",
068: safeRegexReplacement(value));
069: }
070:
071: /**
072: * Substitutes the indexed parameters.
073: *
074: * @param text the text
075: * @param params the parameters
076: * @return the new text
077: */
078: public static String substitute(String text, String[] params) {
079: for (int i = 0; i < params.length; i++) {
080: String p = params[i];
081: if (p == null) {
082: p = "";
083: }
084: text = text.replaceAll("\\{" + i + "}",
085: safeRegexReplacement(p));
086: }
087: return text;
088: }
089:
090: /**
091: * Substitutes the named parameters. The passed keys and values must be
092: * Strings.
093: *
094: * @param text the text
095: * @param keys the parameter names
096: * @param params the parameter values
097: * @return the new text
098: */
099: public static String substitute(String text, String[] keys,
100: Map params) {
101: for (int i = 0; i < keys.length; i++) {
102: text = text.replaceAll("\\{" + keys[i] + "}",
103: safeRegexReplacement((String) params.get(keys[i])));
104: }
105: return text;
106: }
107:
108: /**
109: * Replace any \ or $ characters in the replacement string with an escaped \\
110: * or \$.
111: *
112: * @param replacement the regular expression replacement text
113: * @return null if replacement is null or the result of escaping all \ and $
114: * characters
115: */
116: private static String safeRegexReplacement(String replacement) {
117: if (replacement == null) {
118: return replacement;
119: }
120:
121: return replacement.replaceAll("\\\\", "\\\\\\\\").replaceAll(
122: "\\$", "\\\\\\$");
123: }
124: }
|