]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/TransliteratorUtilities.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / TransliteratorUtilities.java
1 //##header\r
2 /*\r
3  *******************************************************************************\r
4  * Copyright (C) 2002-2009, International Business Machines Corporation and    *\r
5  * others. All Rights Reserved.                                                *\r
6  *******************************************************************************\r
7  */\r
8 package com.ibm.icu.dev.test.util;\r
9 \r
10 import java.io.BufferedReader;\r
11 import java.io.IOException;\r
12 \r
13 import com.ibm.icu.text.Transliterator;\r
14 //#if defined(FOUNDATION10) || defined(J2SE13)\r
15 //##import com.ibm.icu.dev.test.TestUtil;\r
16 //#endif\r
17 \r
18 public class TransliteratorUtilities {\r
19     public static boolean DEBUG = false;\r
20 \r
21     public static void registerTransliteratorFromFile(String dir, String id) {\r
22         try {\r
23             String filename = id.replace('-', '_') +  ".txt";\r
24             String rules = getFileContents(dir, filename);\r
25             Transliterator t;\r
26             int pos = id.indexOf('-');\r
27             String rid;\r
28             if (pos < 0) {\r
29                 rid = id + "-Any";\r
30                 id = "Any-" + id;\r
31             } else {\r
32                 rid = id.substring(pos+1) + "-" + id.substring(0, pos);\r
33             }\r
34             t = Transliterator.createFromRules(id, rules, Transliterator.FORWARD);\r
35             Transliterator.unregister(id);\r
36             Transliterator.registerInstance(t);\r
37 \r
38             /*String test = "\u049A\u0430\u0437\u0430\u049B";\r
39             System.out.println(t.transliterate(test));\r
40             t = Transliterator.getInstance(id);\r
41             System.out.println(t.transliterate(test));\r
42             */\r
43 \r
44             t = Transliterator.createFromRules(rid, rules, Transliterator.REVERSE);\r
45             Transliterator.unregister(rid);\r
46             Transliterator.registerInstance(t);\r
47             if (DEBUG) System.out.println("Registered new Transliterator: " + id + ", " + rid);\r
48         } catch (IOException e) {\r
49 //#if defined(FOUNDATION10) || defined(J2SE13)\r
50 //##        throw (IllegalArgumentException) new IllegalArgumentException("Can't open " + dir + ", " + id+" "+ e.getMessage());\r
51 //#else\r
52             throw (IllegalArgumentException) new IllegalArgumentException("Can't open " + dir + ", " + id).initCause(e);\r
53 //#endif\r
54         }\r
55     }\r
56 \r
57     /**\r
58      * \r
59      */\r
60     public static String getFileContents(String dir, String filename) throws IOException {\r
61 //#if defined(FOUNDATION10) || defined(J2SE13)\r
62 //##        BufferedReader br = TestUtil.openUTF8Reader(dir, filename);\r
63 //#else\r
64         BufferedReader br = BagFormatter.openUTF8Reader(dir, filename);\r
65 //#endif \r
66         StringBuffer buffer = new StringBuffer();\r
67         while (true) {\r
68             String line = br.readLine();\r
69             if (line == null) break;\r
70             if (line.length() > 0 && line.charAt(0) == '\uFEFF') line = line.substring(1);\r
71             buffer.append(line).append("\r\n");\r
72         }\r
73         br.close();\r
74         return buffer.toString();\r
75          \r
76     }\r
77 \r
78     private static final String BASE_RULES =\r
79         ":: (hex-any/xml);" +\r
80         ":: (hex-any/xml10);" + \r
81         "'<' > '&lt;' ;" +\r
82         "'<' < '&'[lL][Tt]';' ;" +\r
83         "'&' > '&amp;' ;" +\r
84         "'&' < '&'[aA][mM][pP]';' ;" +\r
85         "'>' < '&'[gG][tT]';' ;" +\r
86         "'\"' < '&'[qQ][uU][oO][tT]';' ; " +\r
87         "'' < '&'[aA][pP][oO][sS]';' ; ";\r
88 \r
89     private static final String CONTENT_RULES =\r
90         "'>' > '&gt;' ;";\r
91 \r
92     private static final String HTML_RULES = BASE_RULES + CONTENT_RULES + \r
93         "'\"' > '&quot;' ; ";\r
94 \r
95     private static final String HTML_RULES_CONTROLS = HTML_RULES + \r
96         ":: [[:C:][:Z:][:whitespace:][:Default_Ignorable_Code_Point:]] hex/unicode ; ";\r
97 \r
98     private static final String HTML_RULES_ASCII = HTML_RULES + \r
99         ":: [[:C:][:^ASCII:]] any-hex/xml ; ";\r
100 \r
101     private static final String XML_RULES = HTML_RULES +\r
102         "'' > '&apos;' ; "\r
103 ;\r
104     \r
105     /*\r
106 The ampersand character (&) and the left angle bracket (<) MUST NOT appear \r
107 \r
108 in their literal form, except when used as markup delimiters, or within a \r
109 \r
110 comment, a processing instruction, or a CDATA section. If they are needed \r
111 \r
112 elsewhere, they MUST be escaped using either numeric character references or \r
113 \r
114 the strings "&amp;" and "&lt;" respectively. The right angle bracket (>) MAY \r
115 \r
116 be represented using the string "&gt;", and MUST, for compatibility, be \r
117 \r
118 escaped using either "&gt;" or a character reference when it appears in the string \r
119 \r
120 "]]>" in content, when that string is not marking the end of a CDATA section.\r
121 \r
122 In the content of elements, character data is any string of characters which does \r
123 \r
124 not contain the start-delimiter of any markup and does not include the \r
125 \r
126 CDATA-section-close delimiter, "]]>". In a CDATA section, character data is \r
127 \r
128 any string of characters not including the CDATA-section-close delimiter, \r
129 \r
130 "]]>".\r
131 \r
132 To allow attribute values to contain both single and double quotes, the \r
133 \r
134 apostrophe or single-quote character (') MAY be represented as "&apos;", and \r
135 \r
136 the double-quote character (") as "&quot;".\r
137 \r
138 \r
139      */\r
140     \r
141     public static final Transliterator toXML = Transliterator.createFromRules(\r
142             "any-xml", XML_RULES, Transliterator.FORWARD);\r
143     public static final Transliterator fromXML = Transliterator.createFromRules(\r
144             "xml-any", XML_RULES, Transliterator.REVERSE);\r
145     public static final Transliterator toHTML = Transliterator.createFromRules(\r
146             "any-html", HTML_RULES, Transliterator.FORWARD);\r
147     public static final Transliterator toHTMLControl = Transliterator.createFromRules(\r
148             "any-html", HTML_RULES_CONTROLS, Transliterator.FORWARD);\r
149     public static final Transliterator toHTMLAscii = Transliterator.createFromRules(\r
150             "any-html", HTML_RULES_ASCII, Transliterator.FORWARD);\r
151     public static final Transliterator fromHTML = Transliterator.createFromRules(\r
152             "html-any", HTML_RULES, Transliterator.REVERSE);\r
153 }\r