2 * Copyright (C) 1996-2010, International Business Machines Corporation and
\r
3 * others. All Rights Reserved.
\r
6 package com.ibm.icu.text;
\r
8 import java.io.IOException;
\r
10 import com.ibm.icu.impl.UCaseProps;
\r
11 import com.ibm.icu.util.ULocale;
\r
14 * A transliterator that converts all letters (as defined by
\r
15 * <code>UCharacter.isLetter()</code>) to lower case, except for those
\r
16 * letters preceded by non-letters. The latter are converted to title
\r
17 * case using <code>UCharacter.toTitleCase()</code>.
\r
20 class TitlecaseTransliterator extends Transliterator {
\r
22 static final String _ID = "Any-Title";
\r
25 * System registration hook.
\r
27 static void register() {
\r
28 Transliterator.registerFactory(_ID, new Transliterator.Factory() {
\r
29 public Transliterator getInstance(String ID) {
\r
30 return new TitlecaseTransliterator(ULocale.US);
\r
34 registerSpecialInverse("Title", "Lower", false);
\r
37 private ULocale locale;
\r
39 private UCaseProps csp;
\r
40 private ReplaceableContextIterator iter;
\r
41 private StringBuffer result;
\r
42 private int[] locCache;
\r
45 * Constructs a transliterator.
\r
47 public TitlecaseTransliterator(ULocale loc) {
\r
50 // Need to look back 2 characters in the case of "can't"
\r
51 setMaximumContextLength(2);
\r
53 csp=UCaseProps.getSingleton();
\r
54 } catch (IOException e) {
\r
57 iter=new ReplaceableContextIterator();
\r
58 result = new StringBuffer();
\r
59 locCache = new int[1];
\r
64 * Implements {@link Transliterator#handleTransliterate}.
\r
66 protected void handleTransliterate(Replaceable text,
\r
67 Position offsets, boolean isIncremental) {
\r
68 // TODO reimplement, see ustrcase.c
\r
69 // using a real word break iterator
\r
70 // instead of just looking for a transition between cased and uncased characters
\r
71 // call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
\r
72 // needs to take isIncremental into account because case mappings are context-sensitive
\r
73 // also detect when lowercasing function did not finish because of context
\r
75 if (offsets.start >= offsets.limit) {
\r
79 // case type: >0 cased (UCaseProps.LOWER etc.) ==0 uncased <0 case-ignorable
\r
82 // Our mode; we are either converting letter toTitle or
\r
84 boolean doTitle = true;
\r
86 // Determine if there is a preceding context of cased case-ignorable*,
\r
87 // in which case we want to start in toLower mode. If the
\r
88 // prior context is anything else (including empty) then start
\r
91 for (start = offsets.start - 1; start >= offsets.contextStart; start -= UTF16.getCharCount(c)) {
\r
92 c = text.char32At(start);
\r
93 type=csp.getTypeOrIgnorable(c);
\r
94 if(type>0) { // cased
\r
97 } else if(type==0) { // uncased but not ignorable
\r
100 // else (type<0) case-ignorable: continue
\r
103 // Convert things after a cased character toLower; things
\r
104 // after a uncased, non-case-ignorable character toTitle. Case-ignorable
\r
105 // characters are copied directly and do not change the mode.
\r
107 iter.setText(text);
\r
108 iter.setIndex(offsets.start);
\r
109 iter.setLimit(offsets.limit);
\r
110 iter.setContextLimits(offsets.contextStart, offsets.contextLimit);
\r
112 result.setLength(0);
\r
114 // Walk through original string
\r
115 // If there is a case change, modify corresponding position in replaceable
\r
118 while((c=iter.nextCaseMapCP())>=0) {
\r
119 type=csp.getTypeOrIgnorable(c);
\r
120 if(type>=0) { // not case-ignorable
\r
122 c=csp.toFullTitle(c, iter, result, locale, locCache);
\r
124 c=csp.toFullLower(c, iter, result, locale, locCache);
\r
126 doTitle = type==0; // doTitle=isUncased
\r
128 if(iter.didReachLimit() && isIncremental) {
\r
129 // the case mapping function tried to look beyond the context limit
\r
130 // wait for more input
\r
131 offsets.start=iter.getCaseMapCPStart();
\r
135 /* decode the result */
\r
137 /* c mapped to itself, no change */
\r
139 } else if(c<=UCaseProps.MAX_STRING_LENGTH) {
\r
140 /* replace by the mapping string */
\r
141 delta=iter.replace(result.toString());
\r
142 result.setLength(0);
\r
144 /* replace by single-code point mapping */
\r
145 delta=iter.replace(UTF16.valueOf(c));
\r
149 offsets.limit += delta;
\r
150 offsets.contextLimit += delta;
\r
154 offsets.start = offsets.limit;
\r