001: // Copyright 03/16/00 Sun Microsystems, Inc. All Rights Reserved.
002: // "@(#)I18n.java 1.5 00/03/16 Sun Microsystems"
003:
004: package com.sun.portal.desktop.util;
005:
006: import java.lang.*;
007: import java.util.*;
008: import java.text.*;
009: import java.io.*;
010: import javax.servlet.*;
011: import javax.servlet.http.*;
012:
013: public class I18n {
014: public static final String DEFAULT_CHARSET = "UTF-8";
015: public static final String ASCII_CHARSET = "ISO-8859-1";
016:
017: static public String print(String s) {
018: if (s == null) {
019: return null;
020: }
021:
022: StringBuffer buf = new StringBuffer();
023: for (int i = 0; i < s.length(); i++) {
024: int c = (int) s.charAt(i);
025: if (c >= 32 && c < 127)
026: buf.append(s.charAt(i));
027: else {
028: buf.append("\\u");
029: String n = "";
030: for (int j = 0; j < 4; j++) {
031: n = "" + Character.forDigit(c & 0xf, 16) + n;
032: c = c >> 4;
033: }
034: buf.append(n);
035: }
036: }
037: return buf.toString();
038: }
039:
040: static public String decodeCharset(String s, String charset) {
041: if (s == null) {
042: return null;
043: }
044:
045: try {
046: byte buf[] = s.getBytes(ASCII_CHARSET);
047: return new String(buf, 0, buf.length, charset);
048: } catch (UnsupportedEncodingException uee) {
049: return s;
050: }
051: }
052:
053: static public String encodeCharset(String s, String charset) {
054: if (s == null) {
055: return null;
056: }
057:
058: try {
059: byte buf[] = s.getBytes(charset);
060: return new String(buf, 0, buf.length, ASCII_CHARSET);
061: } catch (UnsupportedEncodingException uee) {
062: return s;
063: }
064: }
065:
066: static public boolean isAscii(String s) {
067: // perform i18n check - the equals test returns false if there are
068: // any chars with value > 256, i.e., multi-byte characters.
069:
070: if (s == null) {
071: return true;
072: }
073:
074: try {
075: if (!s.equals(new String(s.getBytes(ASCII_CHARSET),
076: ASCII_CHARSET))) {
077: // the name does contain non-latin chars
078: return false;
079: }
080: } catch (java.io.UnsupportedEncodingException uee) {
081: // we should never reach this.
082: return false;
083: }
084:
085: return true;
086: }
087:
088: static private String format(MessageFormat mf, Object o) {
089: String msg = mf.format(new Object[] { o }, new StringBuffer(),
090: null).toString();
091:
092: return msg;
093: }
094:
095: static private String format(MessageFormat mf, Object[] array) {
096: String msg = mf.format(array, new StringBuffer(), null)
097: .toString();
098:
099: return msg;
100: }
101:
102: static public String format(String pattern, Long j, Locale l) {
103:
104: MessageFormat mf = new MessageFormat("");
105: mf.setLocale(l);
106: mf.applyPattern(pattern);
107: String msg = format(mf, j);
108:
109: return msg;
110: }
111:
112: static public String format(String pattern, Integer i, Locale l) {
113:
114: MessageFormat mf = new MessageFormat("");
115: mf.setLocale(l);
116: mf.applyPattern(pattern);
117: String msg = format(mf, i);
118:
119: return msg;
120: }
121:
122: static public String format(String pattern, Date d, TimeZone tz,
123: Locale l) {
124:
125: MessageFormat mf = new MessageFormat("");
126: mf.setLocale(l);
127: mf.applyPattern(pattern);
128: ((DateFormat) mf.getFormats()[0]).setTimeZone(tz);
129:
130: //
131: // possibly two formats, one for date and one for time
132: // depends on pattern, which is in the
133: //
134: DateFormat df1 = ((DateFormat) mf.getFormats()[0]);
135: if (df1 != null) {
136: df1.setTimeZone(tz);
137: }
138:
139: DateFormat df2 = ((DateFormat) mf.getFormats()[1]);
140: if (df2 != null) {
141: df2.setTimeZone(tz);
142: }
143:
144: return format(mf, d);
145: }
146:
147: static public String format(String pattern, TimeZone tz, Locale l) {
148:
149: MessageFormat mf = new MessageFormat("");
150: mf.setLocale(l);
151: mf.applyPattern(pattern);
152:
153: Object[] arguments = {
154: new String(tz.getID()),
155: new String(tz.getDisplayName(false, TimeZone.SHORT, l)),
156: new String(tz.getDisplayName(false, TimeZone.LONG, l)) };
157:
158: return format(mf, arguments);
159: }
160:
161: /**
162: * Replacement URLEncoder.encode() method for i18n purposes
163: * Uses our replacement IURLEncoder class due to bug in java URLEncoder class
164: * encodes twice because the PAPI decodes once automatically (via the Servlet API)
165: * and then automatically does charset decoding with possibly the wrong charset
166: * so we need to hide behind an extra layer of URLEncoding
167: * @param enc string to encode, converts to UTF8 first
168: **/
169: static public String IURLEncode(String enc) {
170: try {
171: return IURLEncoder.encode(IURLEncoder.encode(new String(enc
172: .getBytes(DEFAULT_CHARSET), ASCII_CHARSET)));
173: } catch (Exception e) {
174: return enc;
175: }
176: }
177:
178: /**
179: * Replacement URLDecoder.decode() method for i18n purposes
180: * Uses our replacement IURLDecoder class due to bug in java URLDecoder class
181: * @param enc string to decode, converts from UTF8 first
182: **/
183: static public String IURLDecode(String enc) {
184: try {
185: String dc = new String(IURLDecoder.decode(enc));
186: return new String(dc.getBytes(ASCII_CHARSET),
187: DEFAULT_CHARSET);
188: } catch (Exception e) {
189: return enc;
190: }
191: }
192:
193: }
|