KickJava   Java API By Example, From Geeks To Geeks.

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


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 toLower()
19  * case mapping.
20  */

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

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

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

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

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

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

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