]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/translit/src/com/ibm/icu/text/UppercaseTransliterator.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / translit / src / com / ibm / icu / text / UppercaseTransliterator.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2011, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.text;
8
9 import com.ibm.icu.impl.UCaseProps;
10 import com.ibm.icu.lang.UCharacter;
11 import com.ibm.icu.util.ULocale;
12
13 /**
14  * A transliterator that performs locale-sensitive toUpper()
15  * case mapping.
16  */
17 class UppercaseTransliterator extends Transliterator {
18
19     /**
20      * Package accessible ID.
21      */
22     static final String _ID = "Any-Upper";
23     // TODO: Add variants for tr, az, lt, default = default locale
24
25     /**
26      * System registration hook.
27      */
28     static void register() {
29         Transliterator.registerFactory(_ID, new Transliterator.Factory() {
30             public Transliterator getInstance(String ID) {
31                 return new UppercaseTransliterator(ULocale.US);
32             }
33         });
34     }
35
36     private ULocale locale;
37
38     private UCaseProps csp;
39     private ReplaceableContextIterator iter;
40     private StringBuilder result;
41     private int[] locCache;
42
43     /**
44      * Constructs a transliterator.
45      */
46     public UppercaseTransliterator(ULocale loc) {
47         super(_ID, null);
48         locale = loc;
49         csp=UCaseProps.INSTANCE;
50         iter=new ReplaceableContextIterator();
51         result = new StringBuilder();
52         locCache = new int[1];
53         locCache[0]=0;
54     }
55
56     /**
57      * Implements {@link Transliterator#handleTransliterate}.
58      */
59     protected synchronized void handleTransliterate(Replaceable text,
60                                        Position offsets, boolean isIncremental) {
61     if(csp==null) {
62         return;
63     }
64
65     if(offsets.start >= offsets.limit) {
66         return;
67     } 
68
69     iter.setText(text);
70     result.setLength(0);
71     int c, delta;
72
73     // Walk through original string
74     // If there is a case change, modify corresponding position in replaceable
75
76     iter.setIndex(offsets.start);
77     iter.setLimit(offsets.limit);
78     iter.setContextLimits(offsets.contextStart, offsets.contextLimit);
79     while((c=iter.nextCaseMapCP())>=0) {
80         c=csp.toFullUpper(c, iter, result, locale, locCache);
81
82         if(iter.didReachLimit() && isIncremental) {
83             // the case mapping function tried to look beyond the context limit
84             // wait for more input
85             offsets.start=iter.getCaseMapCPStart();
86             return;
87         }
88
89         /* decode the result */
90         if(c<0) {
91             /* c mapped to itself, no change */
92             continue;
93         } else if(c<=UCaseProps.MAX_STRING_LENGTH) {
94             /* replace by the mapping string */
95             delta=iter.replace(result.toString());
96             result.setLength(0);
97         } else {
98             /* replace by single-code point mapping */
99                 delta=iter.replace(UTF16.valueOf(c));
100             }
101
102             if(delta!=0) {
103                 offsets.limit += delta;
104                 offsets.contextLimit += delta;
105             }
106         }
107         offsets.start = offsets.limit;
108     }
109
110     // NOTE: normally this would be static, but because the results vary by locale....
111     SourceTargetUtility sourceTargetUtility = null;
112     
113     /* (non-Javadoc)
114      * @see com.ibm.icu.text.Transliterator#addSourceTargetSet(com.ibm.icu.text.UnicodeSet, com.ibm.icu.text.UnicodeSet, com.ibm.icu.text.UnicodeSet)
115      */
116     @Override
117     public void addSourceTargetSet(UnicodeSet inputFilter, UnicodeSet sourceSet, UnicodeSet targetSet) {
118         synchronized (this) {
119             if (sourceTargetUtility == null) {
120                 sourceTargetUtility = new SourceTargetUtility(new Transform<String,String>() {
121                     public String transform(String source) {
122                         return UCharacter.toUpperCase(locale, source);
123                     }
124                 });
125             }
126         }
127         sourceTargetUtility.addSourceTargetSet(this, inputFilter, sourceSet, targetSet);
128     }
129 }