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