From: <lan...@us...> - 2003-08-01 20:25:48
|
Update of /cvsroot/webmacro/wiki/src/org/opendoors/util In directory sc8-pr-cvs1:/tmp/cvs-serv4158/src/org/opendoors/util Modified Files: StringHelper.java Log Message: update version Index: StringHelper.java =================================================================== RCS file: /cvsroot/webmacro/wiki/src/org/opendoors/util/StringHelper.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** StringHelper.java 30 Sep 2001 08:28:26 -0000 1.1.1.1 --- StringHelper.java 1 Aug 2003 20:25:46 -0000 1.2 *************** *** 11,15 **** * (using JDK codes for the converter). * <p> ! * This class also has methods to take lists of supported locales and * character encoding names and their corresponding 'JDK codes'. Those codes * can be used for setting the encoding of widgets. There is a method --- 11,15 ---- * (using JDK codes for the converter). * <p> ! * This class also has methods to take lists of supported locales and * character encoding names and their corresponding 'JDK codes'. Those codes * can be used for setting the encoding of widgets. There is a method *************** *** 17,63 **** * and a method that returns the name for a given code. * - * @see #convertToDefault(String, String) - * @see #convertFromDefault(String, String) - * @see #getEncodingCode(String) - * @see #getEncodingName(String) - * @see #getLocaleNames(String) - * - * @author Klaas Waslander - * @author Alex Barilov - * @version 1.8, 12/18/97 */ ! public class StringHelper { ! final static boolean JDK10 = false; /** * Never instantiated. */ ! private StringHelper() { } /** * Splits a list separated by a delimiter into an array. */ ! public static String[] split(String list, String delimiter) { ! StringTokenizer token = new StringTokenizer(list, delimiter); ! int count = token.countTokens(); ! String[] fields = new String[count]; ! int index = 0; ! while (token.hasMoreTokens()) { ! fields[index++] = token.nextToken(); ! } ! return fields; ! } ! /** ! * Indexes a set of unique strings and returns it as a hash. ! */ ! public static Hashtable indexStrings(String[] string) { ! Hashtable hash = new Hashtable(); ! for (int index = 0; index < string.length; index++) ! hash.put(string[index], new Integer(index)); ! return hash; } /** * Determines if a string is a member of an array of strings. Case sensitive. --- 17,61 ---- * and a method that returns the name for a given code. * */ ! public class StringHelper ! { /** * Never instantiated. */ ! private StringHelper () ! { } + /** * Splits a list separated by a delimiter into an array. */ ! public static String[] split (String list, String delimiter) ! { ! StringTokenizer token = new StringTokenizer(list, delimiter); ! int count = token.countTokens(); ! String[] fields = new String[count]; ! int index = 0; ! while (token.hasMoreTokens()) ! { ! fields[index++] = token.nextToken(); ! } ! return fields; ! } ! ! /** ! * Indexes a set of unique strings and returns it as a hash. ! */ ! public static Hashtable indexStrings (String[] string) ! { ! Hashtable hash = new Hashtable(); ! for (int index = 0; index < string.length; index++) ! hash.put(string[index], new Integer(index)); ! return hash; } + /** * Determines if a string is a member of an array of strings. Case sensitive. *************** *** 65,76 **** * @param list The list possibly containing item. */ ! public static boolean isMember(String item, String[] list) { ! for (int index = 0; index < list.length; index++) { ! if (list[index].equals(item)) ! return true; ! } ! return false; } ! /** --- 63,76 ---- * @param list The list possibly containing item. */ ! public static boolean isMember (String item, String[] list) ! { ! for (int index = 0; index < list.length; index++) ! { ! if (list[index].equals(item)) ! return true; ! } ! return false; } ! /** *************** *** 79,103 **** * NOTE: this method does NOT use the JDK encoding mechanism! */ ! public static String bytesToString(byte[] bytes) { char value[] = new char[bytes.length]; ! for (int i = bytes.length; i-- > 0;) { ! value[i] = (char)(bytes[i] & 0xff); } return new String(value).trim(); } /** * Converts String to bytes, using one byte per character. * NOTE: this method does NOT use the JDK encoding mechanism! */ ! public static byte[] stringToBytes(String str) { char value[] = str.trim().toCharArray(); ! byte[] bytes = new byte[value.length]; ! for (int i = value.length; i-- > 0;) { ! bytes[i] = (byte)value[i]; } return bytes; } /** * Replace all occurences of string one with string two. --- 79,109 ---- * NOTE: this method does NOT use the JDK encoding mechanism! */ ! public static String bytesToString (byte[] bytes) ! { char value[] = new char[bytes.length]; ! for (int i = bytes.length; i-- > 0;) ! { ! value[i] = (char) (bytes[i] & 0xff); } return new String(value).trim(); } + /** * Converts String to bytes, using one byte per character. * NOTE: this method does NOT use the JDK encoding mechanism! */ ! public static byte[] stringToBytes (String str) ! { char value[] = str.trim().toCharArray(); ! byte[] bytes = new byte[value.length]; ! for (int i = value.length; i-- > 0;) ! { ! bytes[i] = (byte) value[i]; } return bytes; } + /** * Replace all occurences of string one with string two. *************** *** 109,124 **** * @return The newly constructed string. */ ! public static String stringReplace(String org, String str1, String str2) { ! StringBuffer sb = new StringBuffer(); ! int oldi = 0; ! int i = org.indexOf(str1); ! while (i >= 0) { sb.append(org.substring(oldi, i)); // check escape character ! if (i > 0 && org.charAt(i-1) == '\\') { // do not replace sb.append(str1); ! } else { // replace now! sb.append(str2); --- 115,135 ---- * @return The newly constructed string. */ ! public static String stringReplace (String org, String str1, String str2) ! { ! StringBuffer sb = new StringBuffer(); ! int oldi = 0; ! int i = org.indexOf(str1); ! while (i >= 0) ! { sb.append(org.substring(oldi, i)); // check escape character ! if (i > 0 && org.charAt(i - 1) == '\\') ! { // do not replace sb.append(str1); ! } ! else ! { // replace now! sb.append(str2); *************** *** 143,160 **** * @see #convertFromDefault(char, String) */ ! public static String convertFromDefault(String s, String encoding) { ! if (JDK10) { return s; - } else { - try { - return new String(s.getBytes(), encoding); - } catch (Throwable e) { - // unknown encoding - // System.out.println("Unknown encoding: " + encoding); - return s; - } } } /** * Convert a character in the default encoding to the given --- 154,172 ---- * @see #convertFromDefault(char, String) */ ! public static String convertFromDefault (String s, String encoding) ! { ! try ! { ! return new String(s.getBytes(), encoding); ! } ! catch (Throwable e) ! { ! // unknown encoding ! // System.out.println("Unknown encoding: " + encoding); return s; } } + /** * Convert a character in the default encoding to the given *************** *** 166,182 **** * @see #convertFromDefault(String, String) */ ! public static char convertFromDefault(char character, String encoding) { ! if (JDK10) { return character; - } else { - String str = convertFromDefault(""+character, encoding); - if (str.length() > 0) { - return str.charAt(0); - } else { - return character; - } } } /** * Converts a string with the given encoding to a string in the --- 178,195 ---- * @see #convertFromDefault(String, String) */ ! public static char convertFromDefault (char character, String encoding) ! { ! String str = convertFromDefault("" + character, encoding); ! if (str.length() > 0) ! { ! return str.charAt(0); ! } ! else ! { return character; } } + /** * Converts a string with the given encoding to a string in the *************** *** 190,207 **** * @see #convertToDefault(char, String) */ ! public static String convertToDefault(String s, String encoding) { ! if (JDK10) { return s; - } else { - try { - return new String(s.getBytes(encoding)); - } catch (Throwable e) { - // unknown encoding - // System.out.println("Unknown encoding: " + encoding); - return s; - } } } /** * Convert a character in the given encoding to the default --- 203,221 ---- * @see #convertToDefault(char, String) */ ! public static String convertToDefault (String s, String encoding) ! { ! try ! { ! return new String(s.getBytes(encoding)); ! } ! catch (Throwable e) ! { ! // unknown encoding ! // System.out.println("Unknown encoding: " + encoding); return s; } } + /** * Convert a character in the given encoding to the default *************** *** 213,242 **** * @see #convertToDefault(String, String) */ ! public static char convertToDefault(char character, String encoding) { ! if (JDK10) { return character; - } else { - String str = convertToDefault(""+character, encoding); - if (str.length() > 0) { - return str.charAt(0); - } else { - return character; - } } } ! /** ! * Compares one byte array to the other and returns true if they ! * are identical in length and value. ! */ ! public static boolean compare(byte[] a, byte[] b) { ! int length = a.length; ! if (length != b.length) ! return false; ! for (int index = 0; index < length; index++) { ! if (a[index] != b[index]) ! return false; ! } ! return true; } --- 227,259 ---- * @see #convertToDefault(String, String) */ ! public static char convertToDefault (char character, String encoding) ! { ! String str = convertToDefault("" + character, encoding); ! if (str.length() > 0) ! { ! return str.charAt(0); ! } ! else ! { return character; } } ! ! /** ! * Compares one byte array to the other and returns true if they ! * are identical in length and value. ! */ ! public static boolean compare (byte[] a, byte[] b) ! { ! int length = a.length; ! if (length != b.length) ! return false; ! for (int index = 0; index < length; index++) ! { ! if (a[index] != b[index]) ! return false; ! } ! return true; } *************** *** 256,267 **** * @see #getEncodingCode */ ! public static String getEncodingName(String code) { // get the names, go through them and if one returns the same // code, return that name as the resulting encoding name. ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) { ! String name = (String)names.nextElement(); ! String currentCode = (String)encodingTable.get(name); ! if (currentCode.equalsIgnoreCase(code)) { return name; } --- 273,287 ---- * @see #getEncodingCode */ ! public static String getEncodingName (String code) ! { // get the names, go through them and if one returns the same // code, return that name as the resulting encoding name. ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) ! { ! String name = (String) names.nextElement(); ! String currentCode = (String) encodingTable.get(name); ! if (currentCode.equalsIgnoreCase(code)) ! { return name; } *************** *** 270,273 **** --- 290,294 ---- } + /** * Get the code of the given encoding name. The case of the given *************** *** 279,292 **** * @see #getEncodingCodes */ ! public static String getEncodingCode(String name) { ! String code = (String)encodingTable.get(name); // try to find it ignoring the case ! if (code == null) { ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) { ! String currentName = (String)names.nextElement(); ! if (currentName.equalsIgnoreCase(name)) { ! code = (String)encodingTable.get(currentName); break; } --- 300,317 ---- * @see #getEncodingCodes */ ! public static String getEncodingCode (String name) ! { ! String code = (String) encodingTable.get(name); // try to find it ignoring the case ! if (code == null) ! { ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) ! { ! String currentName = (String) names.nextElement(); ! if (currentName.equalsIgnoreCase(name)) ! { ! code = (String) encodingTable.get(currentName); break; } *************** *** 296,299 **** --- 321,325 ---- } + /** * Get all the available encoding names. *************** *** 302,317 **** * @see #getEncodingName */ ! public static String[] getEncodingNames() { ! Vector result = new Vector(); ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) { ! String currentName = (String)names.nextElement(); result.addElement(currentName); } ! String[] array = new String[result.size()]; result.copyInto(array); return array; } /** * Get all the available encoding codes. --- 328,346 ---- * @see #getEncodingName */ ! public static String[] getEncodingNames () ! { ! Vector result = new Vector(); ! Enumeration names = encodingTable.keys(); ! while (names.hasMoreElements()) ! { ! String currentName = (String) names.nextElement(); result.addElement(currentName); } ! String[] array = new String[result.size()]; result.copyInto(array); return array; } + /** * Get all the available encoding codes. *************** *** 320,327 **** * @see #getEncodingCode */ ! public static String[] getEncodingCodes() { ! String[] names = getEncodingNames(); ! String[] result = new String[names.length]; ! for (int i = 0; i < names.length; i++) { result[i] = getEncodingCode(names[i]); } --- 349,358 ---- * @see #getEncodingCode */ ! public static String[] getEncodingCodes () ! { ! String[] names = getEncodingNames(); ! String[] result = new String[names.length]; ! for (int i = 0; i < names.length; i++) ! { result[i] = getEncodingCode(names[i]); } *************** *** 329,332 **** --- 360,364 ---- } + /** * Get all supported locale names. *************** *** 335,346 **** * @see #getEncodingNames */ ! public static String[] getLocaleNames() { ! Locale[] localeList = (new java.text.resources.LocaleData()).getAvailableLocales(null); String[] names = new String[localeList.length]; ! for (int i = 0; i < localeList.length; i++) { names[i] = localeList[i].getDisplayName() + "[" + localeList[i].toString() + "]"; } return names; ! } /** --- 367,381 ---- * @see #getEncodingNames */ ! public static String[] getLocaleNames () ! { ! Locale[] localeList = java.text.Collator.getAvailableLocales();//(new java.text.resources.LocaleData()).getAvailableLocales(null); String[] names = new String[localeList.length]; ! for (int i = 0; i < localeList.length; i++) ! { names[i] = localeList[i].getDisplayName() + "[" + localeList[i].toString() + "]"; } return names; ! } ! /** *************** *** 349,410 **** * for the JDK character encoding class. */ ! private static Hashtable encodingTable = new Hashtable(); ! static { ! putEncoding("ISO Latin-1", "8859_1"); ! putEncoding("ISO Latin-2", "8859_2"); ! putEncoding("ISO Latin-3", "8859_3"); ! putEncoding("ISO Latin-4", "8859_4"); ! putEncoding("ISO Latin/Cyrillic", "8859_5"); ! putEncoding("ISO Latin/Arabic", "8859_6"); ! putEncoding("ISO Latin/Greek", "8859_7"); ! putEncoding("ISO Latin/Hebrew", "8859_8"); ! putEncoding("ISO Latin-5", "8859_9"); ! putEncoding("Windows East Europe", "Cp1250"); ! putEncoding("Windows Cyrillic", "Cp1251"); ! putEncoding("Windows West Europe", "Cp1252"); ! putEncoding("Windows Greek", "Cp1253"); ! putEncoding("Windows Turkish", "Cp1254"); ! putEncoding("Windows Hebrew", "Cp1255"); ! putEncoding("Windows Arabic", "Cp1256"); ! putEncoding("Windows Baltic", "Cp1257"); ! putEncoding("Windows Vietnamese", "Cp1258"); ! putEncoding("PC Original", "Cp437"); ! putEncoding("PC Greek", "Cp737"); ! putEncoding("PC Baltic", "Cp775"); ! putEncoding("PC Latin-1", "Cp850"); ! putEncoding("PC Latin-2", "Cp852"); ! putEncoding("PC Cyrillic", "Cp855"); ! putEncoding("PC Turkish", "Cp857"); ! putEncoding("PC Portuguese", "Cp860"); ! putEncoding("PC Icelandic", "Cp861"); ! putEncoding("PC Hebrew", "Cp862"); ! putEncoding("PC Canadian French", "Cp863"); ! putEncoding("PC Arabic", "Cp864"); ! putEncoding("PC Nordic", "Cp865"); ! putEncoding("PC Russian", "Cp866"); ! putEncoding("PC Modern Greek", "Cp869"); ! putEncoding("Windows Thai", "Cp874"); ! putEncoding("Japanese EUC", "EUCJIS"); ! putEncoding("Macintosh Arabic", "MacArabic"); ! putEncoding("JIS", "JIS");//"LIS"); ! putEncoding("Macintosh Latin-2", "MacCentralEurope"); ! putEncoding("Macintosh Croation", "MacCroatian"); ! putEncoding("Macintosh Cyrillic", "MacCyrillic"); ! putEncoding("Macintosh Dingbat", "MacDingbat"); ! putEncoding("Macintosh Greek", "MacGreek"); ! putEncoding("Macintosh Hebrew", "MacHebrew"); ! putEncoding("Macintosh Iceland", "Iceland"); ! putEncoding("Macintosh Roman", "MacRoman"); ! putEncoding("Macintosh Romania", "MacRomania"); ! putEncoding("Macintosh Symbol", "MacSymbol"); ! putEncoding("Macintosh Thai", "MacThai"); ! putEncoding("Macintosh Turkish", "MacTurkish"); ! putEncoding("Macintosh Ukraine", "MacUkraine"); ! putEncoding("PC-Windows Japanese", "SJIS"); ! putEncoding("Standard UTF-8", "UTF8"); } /** Add an encoding name/value pair to the encoding hashtable. */ ! private static void putEncoding(String s1, String s2) { encodingTable.put(s1 + " [" + s2 + "]", s2); } --- 384,450 ---- * for the JDK character encoding class. */ ! private static Hashtable encodingTable = new Hashtable(); ! ! ! static ! { ! putEncoding("ISO Latin-1", "8859_1"); ! putEncoding("ISO Latin-2", "8859_2"); ! putEncoding("ISO Latin-3", "8859_3"); ! putEncoding("ISO Latin-4", "8859_4"); ! putEncoding("ISO Latin/Cyrillic", "8859_5"); ! putEncoding("ISO Latin/Arabic", "8859_6"); ! putEncoding("ISO Latin/Greek", "8859_7"); ! putEncoding("ISO Latin/Hebrew", "8859_8"); ! putEncoding("ISO Latin-5", "8859_9"); ! putEncoding("Windows East Europe", "Cp1250"); ! putEncoding("Windows Cyrillic", "Cp1251"); ! putEncoding("Windows West Europe", "Cp1252"); ! putEncoding("Windows Greek", "Cp1253"); ! putEncoding("Windows Turkish", "Cp1254"); ! putEncoding("Windows Hebrew", "Cp1255"); ! putEncoding("Windows Arabic", "Cp1256"); ! putEncoding("Windows Baltic", "Cp1257"); ! putEncoding("Windows Vietnamese", "Cp1258"); ! putEncoding("PC Original", "Cp437"); ! putEncoding("PC Greek", "Cp737"); ! putEncoding("PC Baltic", "Cp775"); ! putEncoding("PC Latin-1", "Cp850"); ! putEncoding("PC Latin-2", "Cp852"); ! putEncoding("PC Cyrillic", "Cp855"); ! putEncoding("PC Turkish", "Cp857"); ! putEncoding("PC Portuguese", "Cp860"); ! putEncoding("PC Icelandic", "Cp861"); ! putEncoding("PC Hebrew", "Cp862"); ! putEncoding("PC Canadian French", "Cp863"); ! putEncoding("PC Arabic", "Cp864"); ! putEncoding("PC Nordic", "Cp865"); ! putEncoding("PC Russian", "Cp866"); ! putEncoding("PC Modern Greek", "Cp869"); ! putEncoding("Windows Thai", "Cp874"); ! putEncoding("Japanese EUC", "EUCJIS"); ! putEncoding("Macintosh Arabic", "MacArabic"); ! putEncoding("JIS", "JIS");//"LIS"); ! putEncoding("Macintosh Latin-2", "MacCentralEurope"); ! putEncoding("Macintosh Croation", "MacCroatian"); ! putEncoding("Macintosh Cyrillic", "MacCyrillic"); ! putEncoding("Macintosh Dingbat", "MacDingbat"); ! putEncoding("Macintosh Greek", "MacGreek"); ! putEncoding("Macintosh Hebrew", "MacHebrew"); ! putEncoding("Macintosh Iceland", "Iceland"); ! putEncoding("Macintosh Roman", "MacRoman"); ! putEncoding("Macintosh Romania", "MacRomania"); ! putEncoding("Macintosh Symbol", "MacSymbol"); ! putEncoding("Macintosh Thai", "MacThai"); ! putEncoding("Macintosh Turkish", "MacTurkish"); ! putEncoding("Macintosh Ukraine", "MacUkraine"); ! putEncoding("PC-Windows Japanese", "SJIS"); ! putEncoding("Standard UTF-8", "UTF8"); } + /** Add an encoding name/value pair to the encoding hashtable. */ ! private static void putEncoding (String s1, String s2) ! { encodingTable.put(s1 + " [" + s2 + "]", s2); } |