001: // ***************************************************************
002: // * *
003: // * File: QuickMessageFormat.java *
004: // * *
005: // * Copyright (c) 2001 Sun Microsystems, Inc. *
006: // * All rights reserved. *
007: // * *
008: // * *
009: // * Date - Dec/11/2001 *
010: // * Author - alejandro.abdelnur@sun.com *
011: // * *
012: // ***************************************************************
013:
014: package com.sun.portal.common.text;
015:
016: import java.util.List;
017: import java.util.ArrayList;
018:
019: /**
020: * Fast version of the MessageFormat class.
021: * <P>
022: * It minimizes the Object creation to one StringBuffer and one String per
023: * invocation of the format methods.
024: * <P>
025: * It only supports String values.
026: * <P>
027: * It supports more than 9 arguments (the MessageFormat class does not).
028: * <P>
029: *
030: * @author <A HREF="mailto:alejandro.abdelnur@sun.com">Alejandro Abdelnur</A>
031: *
032: */
033: public class QuickMessageFormat {
034:
035: /*
036: * public static void main(String[] args) {
037: * QuickFormat f;
038: *
039: * f = new QuickFormat("Hello");
040: * System.out.println(f.format("hola"));
041: *
042: * f = new QuickFormat("{0}");
043: * System.out.println(f.format("hola"));
044: *
045: * f = new QuickFormat("{0} Hello");
046: * System.out.println(f.format("hola"));
047: *
048: * f = new QuickFormat("Hello {0}");
049: * System.out.println(f.format("hola"));
050: *
051: * f = new QuickFormat("Hello '{'0'}' Bye {0} \\''{0}'\\'{0}");
052: * System.out.println(f.format("hola"));
053: *
054: * f = new QuickFormat("Hello {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}");
055: * System.out.println(f.format(new String[]{"A","B","C","D","E","F","G","H","I","J","K"}));
056: * }
057: */
058:
059: private int estimatedLength;
060: private String[] chunk;
061: private int[] variable;
062:
063: /**
064: * Constructor QuickMessageFormat
065: *
066: *
067: * @param template
068: *
069: * @audience
070: */
071: public QuickMessageFormat(String template) {
072:
073: StringBuffer sb = new StringBuffer();
074: List chunkL = new ArrayList();
075: List variableL = new ArrayList();
076: int state = 0;
077:
078: for (int i = 0; i < template.length(); i++) {
079: char c = template.charAt(i);
080:
081: switch (state) {
082:
083: case 0: // processing regular characters
084: switch (c) {
085:
086: case '{': // finds begining of variable, stores chunk and changes to state 1
087: state = 1;
088:
089: chunkL.add(sb.toString());
090: sb.delete(0, sb.length());
091: break;
092:
093: case '\'': // finds begining of escaped chars, changes to 2
094: state = 2;
095: break;
096:
097: case '\\': // to escape single quote
098: state = 3;
099: break;
100:
101: default: // appends char to chunk
102: sb.append(c);
103: break;
104: }
105: break;
106:
107: case 1: // processing a variable number
108: switch (c) {
109:
110: case '}': // finds end of variable, gets number and changes to 0
111: state = 0;
112:
113: Integer v = new Integer(sb.toString());
114:
115: variableL.add(v);
116: sb.delete(0, sb.length());
117: break;
118:
119: default: // appends to chunk a digit from the variable number.
120: sb.append(c);
121: break;
122: }
123: break;
124:
125: case 2: // escaped chars, everything between single quotes goes literal, no variable processing
126: switch (c) {
127:
128: case '\'': // finds end of escaped sequence, changes state to 0
129: state = 0;
130: break;
131:
132: default: // appends char to chunk
133: sb.append(c);
134: break;
135: }
136: break;
137:
138: case 3:
139: sb.append(c);
140:
141: state = 0;
142: break;
143: }
144: }
145:
146: chunkL.add(sb.toString());
147:
148: // converts chunk and variable lists to arrays.
149: chunk = new String[chunkL.size()];
150:
151: chunkL.toArray(chunk);
152:
153: variable = new int[variableL.size()];
154:
155: for (int i = 0; i < variable.length; i++) {
156: variable[i] = ((Integer) variableL.get(i)).intValue();
157: }
158:
159: estimatedLength = template.length() + (variable.length * 15);
160: }
161:
162: /**
163: * Method format
164: *
165: *
166: * @param values
167: *
168: * @return
169: *
170: * @audience
171: */
172: public String format(String[] values) {
173:
174: StringBuffer sb = new StringBuffer(estimatedLength);
175:
176: sb.append(chunk[0]);
177:
178: for (int i = 0; i < variable.length; i++) {
179: sb.append(values[variable[i]]);
180: sb.append(chunk[i + 1]);
181: }
182:
183: return sb.toString();
184: }
185:
186: /**
187: * Method format
188: *
189: *
190: * @param value
191: *
192: * @return
193: *
194: * @audience
195: */
196: public String format(String value) {
197:
198: StringBuffer sb = new StringBuffer(estimatedLength);
199:
200: sb.append(chunk[0]);
201:
202: for (int i = 0; i < variable.length; i++) {
203: if (variable[i] == 0) {
204: sb.append(value);
205: }
206:
207: sb.append(chunk[i + 1]);
208: }
209:
210: return sb.toString();
211: }
212:
213: public static String format(String pattern, String value) {
214: QuickMessageFormat qmf = new QuickMessageFormat(pattern);
215: return qmf.format(value);
216: }
217:
218: public static String format(String pattern, String[] values) {
219: QuickMessageFormat qmf = new QuickMessageFormat(pattern);
220: return qmf.format(values);
221: }
222:
223: }
|