KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > UppercaseTransliterator


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2005, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.text;
8
9 import java.io.IOException JavaDoc;
10
11 import com.ibm.icu.impl.UCaseProps;
12
13 import com.ibm.icu.util.ULocale;
14
15 import com.ibm.icu.text.ReplaceableContextIterator;
16
17 /**
18  * A transliterator that performs locale-sensitive toUpper()
19  * case mapping.
20  */

21 class UppercaseTransliterator extends Transliterator {
22
23     /**
24      * Package accessible ID.
25      */

26     static final String JavaDoc _ID = "Any-Upper";
27     // TODO: Add variants for tr, az, lt, default = default locale
28

29     /**
30      * System registration hook.
31      */

32     static void register() {
33         Transliterator.registerFactory(_ID, new Transliterator.Factory() {
34             public Transliterator getInstance(String JavaDoc ID) {
35                 return new UppercaseTransliterator(ULocale.US);
36             }
37         });
38     }
39
40     private ULocale locale;
41
42     private UCaseProps csp;
43     private ReplaceableContextIterator iter;
44     private StringBuffer JavaDoc result;
45     private int[] locCache;
46
47     /**
48      * Constructs a transliterator.
49      */

50     public UppercaseTransliterator(ULocale loc) {
51         super(_ID, null);
52         locale = loc;
53         try {
54             csp=UCaseProps.getSingleton();
55         } catch (IOException JavaDoc e) {
56             csp=null;
57         }
58         iter=new ReplaceableContextIterator();
59         result = new StringBuffer JavaDoc();
60         int[] locCache = new int[1];
61         locCache[0]=0;
62     }
63
64     /**
65      * Implements {@link Transliterator#handleTransliterate}.
66      */

67     protected void handleTransliterate(Replaceable text,
68                                        Position offsets, boolean isIncremental) {
69     if(csp==null) {
70         return;
71     }
72
73     if(offsets.start >= offsets.limit) {
74         return;
75     }
76
77     iter.setText(text);
78     result.setLength(0);
79     int c, delta;
80
81     // Walk through original string
82
// If there is a case change, modify corresponding position in replaceable
83

84     iter.setIndex(offsets.start);
85     iter.setLimit(offsets.limit);
86     iter.setContextLimits(offsets.contextStart, offsets.contextLimit);
87     while((c=iter.nextCaseMapCP())>=0) {
88         c=csp.toFullUpper(c, iter, result, locale, locCache);
89
90         if(iter.didReachLimit() && isIncremental) {
91             // the case mapping function tried to look beyond the context limit
92
// wait for more input
93
offsets.start=iter.getCaseMapCPStart();
94             return;
95         }
96
97         /* decode the result */
98         if(c<0) {
99             /* c mapped to itself, no change */
100             continue;
101         } else if(c<=UCaseProps.MAX_STRING_LENGTH) {
102             /* replace by the mapping string */
103             delta=iter.replace(result.toString());
104             result.setLength(0);
105         } else {
106             /* replace by single-code point mapping */
107                 delta=iter.replace(UTF16.valueOf(c));
108             }
109
110             if(delta!=0) {
111                 offsets.limit += delta;
112                 offsets.contextLimit += delta;
113             }
114         }
115         offsets.start = offsets.limit;
116     }
117 }
118
Popular Tags