2 *******************************************************************************
3 * Copyright (C) 1996-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.text;
9 import com.ibm.icu.impl.UCaseProps;
10 import com.ibm.icu.lang.UCharacter;
11 import com.ibm.icu.util.ULocale;
14 * A transliterator that performs locale-sensitive toUpper()
17 class UppercaseTransliterator extends Transliterator {
20 * Package accessible ID.
22 static final String _ID = "Any-Upper";
23 // TODO: Add variants for tr, az, lt, default = default locale
26 * System registration hook.
28 static void register() {
29 Transliterator.registerFactory(_ID, new Transliterator.Factory() {
30 public Transliterator getInstance(String ID) {
31 return new UppercaseTransliterator(ULocale.US);
36 private ULocale locale;
38 private UCaseProps csp;
39 private ReplaceableContextIterator iter;
40 private StringBuilder result;
41 private int[] locCache;
44 * Constructs a transliterator.
46 public UppercaseTransliterator(ULocale loc) {
49 csp=UCaseProps.INSTANCE;
50 iter=new ReplaceableContextIterator();
51 result = new StringBuilder();
52 locCache = new int[1];
57 * Implements {@link Transliterator#handleTransliterate}.
59 protected synchronized void handleTransliterate(Replaceable text,
60 Position offsets, boolean isIncremental) {
65 if(offsets.start >= offsets.limit) {
73 // Walk through original string
74 // If there is a case change, modify corresponding position in replaceable
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);
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();
89 /* decode the result */
91 /* c mapped to itself, no change */
93 } else if(c<=UCaseProps.MAX_STRING_LENGTH) {
94 /* replace by the mapping string */
95 delta=iter.replace(result.toString());
98 /* replace by single-code point mapping */
99 delta=iter.replace(UTF16.valueOf(c));
103 offsets.limit += delta;
104 offsets.contextLimit += delta;
107 offsets.start = offsets.limit;
110 // NOTE: normally this would be static, but because the results vary by locale....
111 SourceTargetUtility sourceTargetUtility = null;
114 * @see com.ibm.icu.text.Transliterator#addSourceTargetSet(com.ibm.icu.text.UnicodeSet, com.ibm.icu.text.UnicodeSet, com.ibm.icu.text.UnicodeSet)
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);
127 sourceTargetUtility.addSourceTargetSet(this, inputFilter, sourceSet, targetSet);