]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/framework/src/com/ibm/icu/dev/util/UnicodePropertySource.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / framework / src / com / ibm / icu / dev / util / UnicodePropertySource.java
1
2 /*
3  *******************************************************************************
4  * Copyright (C) 2002-2012, International Business Machines Corporation and    *
5  * others. All Rights Reserved.                                                *
6  *******************************************************************************
7  */
8 package com.ibm.icu.dev.util;
9
10 import java.util.Arrays;
11 import java.util.List;
12 import java.util.Locale;
13 import java.util.Map;
14 import java.util.Set;
15
16 import com.ibm.icu.lang.UCharacter;
17 import com.ibm.icu.lang.UProperty;
18 import com.ibm.icu.text.Normalizer;
19 import com.ibm.icu.text.UTF16;
20 import com.ibm.icu.text.UnicodeSet;
21 import com.ibm.icu.text.UnicodeSetIterator;
22
23
24 /**
25  * Provides a general interface for Unicode Properties, and
26  * extracting sets based on those values.
27  * @author Davis
28  */
29 public abstract class UnicodePropertySource implements Cloneable {
30     
31     protected String propertyAlias;
32     protected int m_nameChoice = UProperty.NameChoice.LONG;
33     protected StringFilter filter = new StringFilter();
34     protected UnicodeSetIterator matchIterator = new UnicodeSetIterator(new UnicodeSet(0,0x10FFFF));
35     
36     abstract public String getPropertyValue(int codepoint);
37     abstract public Set getAvailablePropertyAliases(Set result);
38     abstract public Set getAvailablePropertyValueAliases(Set result);
39
40     abstract public String getPropertyAlias(int nameChoice);
41     abstract public String getPropertyValueAlias(String valueAlias, int nameChoice);
42     
43     /**
44      * Subclasses should override
45      */
46     public Object clone() {
47         try {
48             UnicodePropertySource result = (UnicodePropertySource)super.clone();
49             result.filter = (StringFilter)filter.clone();
50             return result;             
51         } catch (CloneNotSupportedException e) {
52             throw new IllegalStateException("Should never happen.");
53         }
54     }
55     
56     public UnicodePropertySource setPropertyAlias(String propertyAlias) {
57         this.propertyAlias = propertyAlias;
58         return this;
59     }
60     
61     public String getPropertyAlias() {
62         return propertyAlias;
63     }
64     
65     public static final boolean equals(int codepoint, String other) {
66         if (other.length() == 1) {
67             return codepoint == other.charAt(0);
68         }
69         return other.equals(UTF16.valueOf(codepoint));
70     }
71     
72     public UnicodeSet getPropertySet(boolean charEqualsValue, UnicodeSet result){
73         if (result == null) result = new UnicodeSet();
74         matchIterator.reset();
75         while (matchIterator.next()) {
76             String value = filter.remap(getPropertyValue(matchIterator.codepoint));
77             if (equals(matchIterator.codepoint, value) == charEqualsValue) {
78                 result.add(matchIterator.codepoint);
79             }
80         }
81         return result;
82     }
83
84     public UnicodeSet getPropertySet(String propertyValue, UnicodeSet result){
85         if (result == null) result = new UnicodeSet();
86         matchIterator.reset();
87         while (matchIterator.next()) {
88             String value = filter.remap(getPropertyValue(matchIterator.codepoint));
89             if (propertyValue.equals(value)) {
90                 result.add(matchIterator.codepoint);
91             } 
92         }
93         return result;
94     }
95
96     public UnicodeSet getPropertySet(Matcher matcher, UnicodeSet result) {
97         if (result == null) result = new UnicodeSet();
98         matchIterator.reset();
99         while (matchIterator.next()) {
100             String value = filter.remap(getPropertyValue(matchIterator.codepoint));
101             if (value == null)
102                 continue;
103             if (matcher.matches(value)) {
104                 result.add(matchIterator.codepoint);
105             }
106         }
107         return result;
108     }
109     
110     public interface Matcher {
111         public boolean matches(String value);
112     }
113     
114     public int getNameChoice() {
115         return m_nameChoice;
116     }
117
118     public UnicodePropertySource setNameChoice(int choice) {
119         m_nameChoice = choice;
120         return this;
121     }
122     
123     public static class StringFilter implements Cloneable {
124         public String remap(String original) {
125             return original;
126         }
127         public Object clone() {
128             try {
129                 return super.clone();
130             } catch (CloneNotSupportedException e) {
131                 throw new IllegalStateException("Should never happen.");
132             }
133         }
134     }
135     
136     public static class MapFilter extends StringFilter {
137         Map valueMap;
138         public String remap(String original) {
139             Object changed = valueMap.get(original);
140             return changed == null ? original : (String) changed;
141         }
142         public Map getMap() {
143             return valueMap;
144         }
145
146         public MapFilter setMap(Map map) {
147             valueMap = map;
148             return this;
149         }
150     }
151
152     static public class ICU extends UnicodePropertySource {
153         protected int propEnum = Integer.MIN_VALUE;
154         {
155             matchIterator = new UnicodeSetIterator(
156                 new UnicodeSet("[^[:Cn:]-[:Default_Ignorable_Code_Point:]]"));
157         }
158         
159         public UnicodePropertySource setPropertyAlias(String propertyAlias) {
160             super.setPropertyAlias(propertyAlias);
161             int extraPosition = Extras.indexOf(propertyAlias);
162             if (extraPosition >= 0) {
163                 propEnum = EXTRA_START + extraPosition;
164             } else {
165                 propEnum = UCharacter.getPropertyEnum(propertyAlias);
166             }
167             return this;
168         }
169
170         public String getPropertyValue(int codePoint) {
171             if (propEnum < UProperty.INT_LIMIT) {
172                 int enumValue = UCharacter.getIntPropertyValue(codePoint, propEnum);
173                 return UCharacter.getPropertyValueName(propEnum,enumValue, (int)m_nameChoice);
174             } else if (propEnum < UProperty.DOUBLE_LIMIT) {
175                 return Double.toString(UCharacter.getUnicodeNumericValue(codePoint));
176                 // TODO: Fix HACK -- API deficient
177             } else switch(propEnum) {
178                 case UProperty.AGE: return UCharacter.getAge(codePoint).toString();
179                 case UProperty.BIDI_MIRRORING_GLYPH: return UTF16.valueOf(UCharacter.getMirror(codePoint));
180                 case UProperty.CASE_FOLDING: return UCharacter.foldCase(UTF16.valueOf(codePoint),true);
181                 case UProperty.ISO_COMMENT: return UCharacter.getISOComment(codePoint);
182                 case UProperty.LOWERCASE_MAPPING: return UCharacter.toLowerCase(Locale.ENGLISH,UTF16.valueOf(codePoint));
183                 case UProperty.NAME: return UCharacter.getName(codePoint);
184                 case UProperty.SIMPLE_CASE_FOLDING: return UTF16.valueOf(UCharacter.foldCase(codePoint,true));
185                 case UProperty.SIMPLE_LOWERCASE_MAPPING: return UTF16.valueOf(UCharacter.toLowerCase(codePoint));
186                 case UProperty.SIMPLE_TITLECASE_MAPPING: return UTF16.valueOf(UCharacter.toTitleCase(codePoint));
187                 case UProperty.SIMPLE_UPPERCASE_MAPPING: return UTF16.valueOf(UCharacter.toUpperCase(codePoint));
188                 case UProperty.TITLECASE_MAPPING: return UCharacter.toTitleCase(Locale.ENGLISH,UTF16.valueOf(codePoint),null);
189                 case UProperty.UNICODE_1_NAME: return UCharacter.getName1_0(codePoint);
190                 case UProperty.UPPERCASE_MAPPING: return UCharacter.toUpperCase(Locale.ENGLISH,UTF16.valueOf(codePoint));
191                 case NFC: return Normalizer.normalize(codePoint, Normalizer.NFC);
192                 case NFD: return Normalizer.normalize(codePoint, Normalizer.NFD);
193                 case NFKC: return Normalizer.normalize(codePoint, Normalizer.NFKC);
194                 case NFKD: return Normalizer.normalize(codePoint, Normalizer.NFKD);
195             }
196             return null;
197         }
198         
199         static final List Extras = Arrays.asList(new String[] {
200             "NFC", "NFD", "NFKC", "NKFD"
201         });
202         
203         static final int 
204             NFC  = 0x8000,
205             NFD  = 0x8001,
206             NFKC = 0x8002,
207             NFKD = 0x8003,
208             EXTRA_START = NFC,
209             EXTRA_LIMIT = NFKD+1;
210
211         static final int[][] ranges = {
212             {UProperty.BINARY_START,    UProperty.BINARY_LIMIT},
213             {UProperty.INT_START,       UProperty.INT_LIMIT},
214             {UProperty.DOUBLE_START,    UProperty.DOUBLE_LIMIT},
215             {UProperty.STRING_START,    UProperty.STRING_LIMIT},
216         };
217
218         public Set getAvailablePropertyAliases(Set result) {
219             for (int i = 0; i < ranges.length; ++i) {
220                 for (int j = ranges[i][0]; j < ranges[i][1]; ++j) {
221                     result.add(UCharacter.getPropertyName(j, m_nameChoice));
222                 }
223             }
224             result.addAll(Extras);
225             return result;
226         }
227
228         public Set getAvailablePropertyValueAliases(Set result) {
229             if (propEnum < UProperty.INT_LIMIT) {
230                 int start = UCharacter.getIntPropertyMinValue(propEnum);
231                 int end = UCharacter.getIntPropertyMaxValue(propEnum);
232                 for (int i = start; i <= end; ++i) {
233                     result.add(getFixedValueAlias(null, i,m_nameChoice));
234                 }
235             } else {
236                 result.add(getFixedValueAlias(null, -1,m_nameChoice));
237             }
238             return result;
239         }
240         
241         /**
242          * @param valueAlias null if unused.
243          * @param valueEnum -1 if unused
244          * @param nameChoice
245          * @return the alias
246          */
247         private String getFixedValueAlias(String valueAlias, int valueEnum, int nameChoice) {
248             if (propEnum >= UProperty.STRING_START) {
249                 return "<string>";
250             } else if (propEnum >= UProperty.DOUBLE_START) {
251                 return "<double>";
252             }
253             if (valueAlias != null && !valueAlias.equals("<integer>")) {
254                 valueEnum = UCharacter.getPropertyValueEnum(propEnum,valueAlias);
255             }
256             String result = fixedGetPropertyValueName(propEnum, valueEnum, nameChoice);
257             if (result != null) return result;
258             // try other namechoice
259             result = fixedGetPropertyValueName(propEnum,valueEnum,
260                 nameChoice == UProperty.NameChoice.LONG ? UProperty.NameChoice.SHORT : UProperty.NameChoice.LONG);
261             if (result != null) return result;
262             return "<integer>";
263         }
264
265         private static String fixedGetPropertyValueName(int propEnum, int valueEnum, int nameChoice) {
266             try {
267                 return UCharacter.getPropertyValueName(propEnum,valueEnum,nameChoice);
268             } catch (Exception e) {
269                 return null;
270             }
271         }
272
273         public String getPropertyAlias(int nameChoice) {
274             if (propEnum < EXTRA_START) {
275                 return UCharacter.getPropertyName(propEnum, nameChoice);
276             }
277             return (String)Extras.get(propEnum-EXTRA_START);
278         }
279
280         public String getPropertyValueAlias(String valueAlias, int nameChoice) {
281             return getFixedValueAlias(valueAlias, -1, nameChoice);
282         }
283     }
284     // TODO file bug on getPropertyValueName for Canonical_Combining_Class
285
286     public StringFilter getFilter() {
287         return filter;
288     }
289
290
291     public UnicodePropertySource setFilter(StringFilter filter) {
292         this.filter = filter;
293         return this;
294     }
295
296     /**
297      */
298     static public void addAll(UnicodeSetIterator source, UnicodeSet result) {
299         while (source.nextRange()) {
300             if (source.codepoint == UnicodeSetIterator.IS_STRING) {
301                 result.add(source.string);
302             } else {
303                 result.add(source.codepoint, source.codepointEnd);
304             }
305         }
306     }
307     
308     public UnicodeSet getMatchSet(UnicodeSet result) {
309         if (result == null) result = new UnicodeSet();
310         addAll(matchIterator, result);
311         return result;
312     }
313
314     /**
315      * @param set
316      */
317     public void setMatchSet(UnicodeSet set) {
318         matchIterator = new UnicodeSetIterator(set);
319     }
320
321 }