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