2 *******************************************************************************
3 * Copyright (C) 2002-2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.util;
9 import java.io.BufferedReader;
10 import java.io.IOException;
12 import com.ibm.icu.text.Transliterator;
14 public class TransliteratorUtilities {
15 public static boolean DEBUG = false;
17 public static void registerTransliteratorFromFile(String dir, String id) {
19 String filename = id.replace('-', '_') + ".txt";
20 String rules = getFileContents(dir, filename);
22 int pos = id.indexOf('-');
28 rid = id.substring(pos+1) + "-" + id.substring(0, pos);
30 t = Transliterator.createFromRules(id, rules, Transliterator.FORWARD);
31 Transliterator.unregister(id);
32 Transliterator.registerInstance(t);
34 /*String test = "\u049A\u0430\u0437\u0430\u049B";
35 System.out.println(t.transliterate(test));
36 t = Transliterator.getInstance(id);
37 System.out.println(t.transliterate(test));
40 t = Transliterator.createFromRules(rid, rules, Transliterator.REVERSE);
41 Transliterator.unregister(rid);
42 Transliterator.registerInstance(t);
43 if (DEBUG) System.out.println("Registered new Transliterator: " + id + ", " + rid);
44 } catch (IOException e) {
45 //#if defined(FOUNDATION10) || defined(J2SE13)
46 //## throw (IllegalArgumentException) new IllegalArgumentException("Can't open " + dir + ", " + id+" "+ e.getMessage());
48 throw (IllegalArgumentException) new IllegalArgumentException("Can't open " + dir + ", " + id).initCause(e);
56 public static String getFileContents(String dir, String filename) throws IOException {
57 //#if defined(FOUNDATION10) || defined(J2SE13)
58 //## BufferedReader br = TestUtil.openUTF8Reader(dir, filename);
60 BufferedReader br = BagFormatter.openUTF8Reader(dir, filename);
62 StringBuffer buffer = new StringBuffer();
64 String line = br.readLine();
65 if (line == null) break;
66 if (line.length() > 0 && line.charAt(0) == '\uFEFF') line = line.substring(1);
67 buffer.append(line).append("\r\n");
70 return buffer.toString();
74 private static final String BASE_RULES =
76 ":: (hex-any/xml10);" +
78 "'<' < '&'[lL][Tt]';' ;" +
80 "'&' < '&'[aA][mM][pP]';' ;" +
81 "'>' < '&'[gG][tT]';' ;" +
82 "'\"' < '&'[qQ][uU][oO][tT]';' ; " +
83 "'' < '&'[aA][pP][oO][sS]';' ; ";
85 private static final String CONTENT_RULES =
88 private static final String HTML_RULES = BASE_RULES + CONTENT_RULES +
91 private static final String HTML_RULES_CONTROLS = HTML_RULES +
92 ":: [[:C:][:Z:][:whitespace:][:Default_Ignorable_Code_Point:]] hex/unicode ; ";
94 private static final String HTML_RULES_ASCII = HTML_RULES +
95 ":: [[:C:][:^ASCII:]] any-hex/xml ; ";
97 private static final String XML_RULES = HTML_RULES +
102 The ampersand character (&) and the left angle bracket (<) MUST NOT appear
104 in their literal form, except when used as markup delimiters, or within a
106 comment, a processing instruction, or a CDATA section. If they are needed
108 elsewhere, they MUST be escaped using either numeric character references or
110 the strings "&" and "<" respectively. The right angle bracket (>) MAY
112 be represented using the string ">", and MUST, for compatibility, be
114 escaped using either ">" or a character reference when it appears in the string
116 "]]>" in content, when that string is not marking the end of a CDATA section.
118 In the content of elements, character data is any string of characters which does
120 not contain the start-delimiter of any markup and does not include the
122 CDATA-section-close delimiter, "]]>". In a CDATA section, character data is
124 any string of characters not including the CDATA-section-close delimiter,
128 To allow attribute values to contain both single and double quotes, the
130 apostrophe or single-quote character (') MAY be represented as "'", and
132 the double-quote character (") as """.
137 public static final Transliterator toXML = Transliterator.createFromRules(
138 "any-xml", XML_RULES, Transliterator.FORWARD);
139 public static final Transliterator fromXML = Transliterator.createFromRules(
140 "xml-any", XML_RULES, Transliterator.REVERSE);
141 public static final Transliterator toHTML = Transliterator.createFromRules(
142 "any-html", HTML_RULES, Transliterator.FORWARD);
143 public static final Transliterator toHTMLControl = Transliterator.createFromRules(
144 "any-html", HTML_RULES_CONTROLS, Transliterator.FORWARD);
145 public static final Transliterator toHTMLAscii = Transliterator.createFromRules(
146 "any-html", HTML_RULES_ASCII, Transliterator.FORWARD);
147 public static final Transliterator fromHTML = Transliterator.createFromRules(
148 "html-any", HTML_RULES, Transliterator.REVERSE);