%@ page language="java"
import="java.util.*,java.text.*,com.ibm.icu.util.*"
contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
Format Tester
<%!
static final String STR = "str";
static final String NUM = "num";
static final String DATE = "date";
static final String CHAR = "char";
static final String NUM_FMT = "0.#";
static final DecimalFormat decFmt = new DecimalFormat(NUM_FMT);
static final String DATE_FMT = "yyyy-MM-dd HH:mm:ss";
static final SimpleDateFormat dateFmt = new SimpleDateFormat(DATE_FMT);
static final Object convertToObject(String argType, String arg) {
try {
Object result = null;
if (argType.equals(STR)) {
return arg;
}
else if (argType.equals(NUM)) {
result = decFmt.parse(arg, new ParsePosition(0));
}
else if (argType.equals(DATE)) {
result = dateFmt.parse(arg, new ParsePosition(0));
}
else if (argType.equals(CHAR)) {
result = new Character(arg.charAt(0));
}
else {
return "Invalid Type " + argType;
}
if (result == null) {
return "null";
}
else {
return result;
}
}
catch (Exception e) {
return "[Can not parse \"" + arg + "\"]";
}
}
static final Object[] convertToObjects(String[] argTypes, String[] args) {
int argsLength = (args.length < argTypes.length ? args.length : argTypes.length);
Object[] objs = new Object[argsLength];
for (int argIdx = 0; argIdx < argsLength; argIdx++) {
objs[argIdx] = convertToObject(argTypes[argIdx],args[argIdx]);
}
return objs;
}
static final String escapeString(String arg) {
return arg.replaceAll("&", "&").replaceAll("\"", """).replaceAll("<", "<").replaceAll(">", ">");
}
static final String[] escapeStrings(String[] args) {
for (int argIdx = 0; argIdx < args.length; argIdx++) {
args[argIdx] = escapeString(args[argIdx]);
}
return args;
}
/**
* If we get something like 3.8.1.0_11, it becomes just "3.8.1".
* If we get something like 4.0.0.0, it becomes just "4.0".
*/
static final String trimVersion(String ver) {
int endIdx = ver.lastIndexOf('_') - 1;
if (endIdx < 0) {
endIdx = ver.length() - 1;
}
for (; endIdx >= 0; endIdx--) {
char currChar = ver.charAt(endIdx);
if (currChar != '0' && currChar != '.') {
endIdx++;
break;
}
}
if (endIdx == 1) {
int minorEndIdx = ver.indexOf('.', endIdx + 1);
if (minorEndIdx > endIdx) {
endIdx = minorEndIdx;
}
}
return ver.substring(0, endIdx);
}
%><%
String selectedLocale = request.getParameter("locale");
if (selectedLocale == null) {
selectedLocale = request.getLocale().toString();
}
String msgType = request.getParameter("msgType");
if (msgType== null) {
msgType="java";
}
String msgFmtStr = request.getParameter("msg");
if (msgFmtStr == null) {
msgFmtStr = "Hello {0}!";
}
msgFmtStr = escapeString(msgFmtStr);
String args[] = request.getParameterValues("arg");
if (args == null) {
args = new String[]{"world","",""};
}
args = escapeStrings(args);
String argTypes[] = request.getParameterValues("argType");
if (argTypes == null) {
argTypes = new String[]{"str","str","str"};
}
Object argObjs[] = convertToObjects(argTypes, args);
%>
Format Tester
Result
<%
try {
ULocale currULoc = new ULocale(selectedLocale);
if (msgType.equals("icu4j")) {
out.print((new com.ibm.icu.text.MessageFormat(msgFmtStr,currULoc)).format(argObjs));
}
else if (msgType.equals("printf")) {
out.print((new Formatter(currULoc.toLocale())).format(currULoc.toLocale(), msgFmtStr, argObjs));
}
else {
out.print((new java.text.MessageFormat(msgFmtStr,currULoc.toLocale())).format(argObjs));
}
}
catch (Exception e) {
out.print(""+e.getMessage()+"");
}
%>
Read the following for more information on the syntax you can use.
Powered by
ICU <%= trimVersion(com.ibm.icu.util.VersionInfo.ICU_VERSION.toString()) %> and
<%= System.getProperty("java.vendor") %> Java <%= trimVersion(System.getProperty("java.version")) %>