KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.ibm.icu.text;
6 import com.ibm.icu.lang.*;
7 import com.ibm.icu.impl.Utility;
8 import com.ibm.icu.impl.UCharacterProperty;
9 import com.ibm.icu.impl.UCharacterName;
10
11 /**
12  * A transliterator that performs name to character mapping.
13  * @author Alan Liu
14  */

15 class NameUnicodeTransliterator extends Transliterator {
16
17     char openDelimiter;
18     char closeDelimiter;
19
20     static final String JavaDoc _ID = "Name-Any";
21
22     static final String JavaDoc OPEN_PAT = "\\N~{~";
23     static final char OPEN_DELIM = '\\'; // first char of OPEN_PAT
24
static final char CLOSE_DELIM = '}';
25     static final char SPACE = ' ';
26
27
28     /**
29      * System registration hook.
30      */

31     static void register() {
32         Transliterator.registerFactory(_ID, new Transliterator.Factory() {
33             public Transliterator getInstance(String JavaDoc ID) {
34                 return new NameUnicodeTransliterator(null);
35             }
36         });
37     }
38
39     /**
40      * Constructs a transliterator.
41      */

42     public NameUnicodeTransliterator(UnicodeFilter filter) {
43         super(_ID, filter);
44     }
45
46     /**
47      * Implements {@link Transliterator#handleTransliterate}.
48      */

49     protected void handleTransliterate(Replaceable text,
50                                        Position offsets, boolean isIncremental) {
51
52         int maxLen = UCharacterName.getInstance().getMaxCharNameLength() + 1; // allow for temporary trailing space
53

54         StringBuffer JavaDoc name = new StringBuffer JavaDoc(maxLen);
55
56         // Get the legal character set
57
UnicodeSet legal = new UnicodeSet();
58         UCharacterName.getInstance().getCharNameCharacters(legal);
59
60         int cursor = offsets.start;
61         int limit = offsets.limit;
62
63         // Modes:
64
// 0 - looking for open delimiter
65
// 1 - after open delimiter
66
int mode = 0;
67         int openPos = -1; // open delim candidate pos
68

69         int c;
70         while (cursor < limit) {
71             c = text.char32At(cursor);
72
73             switch (mode) {
74             case 0: // looking for open delimiter
75
if (c == OPEN_DELIM) { // quick check first
76
openPos = cursor;
77                     int i = Utility.parsePattern(OPEN_PAT, text, cursor, limit);
78                     if (i >= 0 && i < limit) {
79                         mode = 1;
80                         name.setLength(0);
81                         cursor = i;
82                         continue; // *** reprocess char32At(cursor)
83
}
84                 }
85                 break;
86
87             case 1: // after open delimiter
88
// Look for legal chars. If \s+ is found, convert it
89
// to a single space. If closeDelimiter is found, exit
90
// the loop. If any other character is found, exit the
91
// loop. If the limit is reached, exit the loop.
92

93                 // Convert \s+ => SPACE. This assumes there are no
94
// runs of >1 space characters in names.
95
if (UCharacterProperty.isRuleWhiteSpace(c)) {
96                     // Ignore leading whitespace
97
if (name.length() > 0 &&
98                         name.charAt(name.length()-1) != SPACE) {
99                         name.append(SPACE);
100                         // If we are too long then abort. maxLen includes
101
// temporary trailing space, so use '>'.
102
if (name.length() > maxLen) {
103                             mode = 0;
104                         }
105                     }
106                     break;
107                 }
108
109                 if (c == CLOSE_DELIM) {
110
111                     int len = name.length();
112                     
113                     // Delete trailing space, if any
114
if (len > 0 &&
115                         name.charAt(len-1) == SPACE) {
116                         name.setLength(--len);
117                     }
118
119                     c = UCharacter.getCharFromExtendedName(name.toString());
120                     if (c != -1) {
121                         // Lookup succeeded
122

123                         // assert(UTF16.getCharCount(CLOSE_DELIM) == 1);
124
cursor++; // advance over CLOSE_DELIM
125

126                         String JavaDoc str = UTF16.valueOf(c);
127                         text.replace(openPos, cursor, str);
128
129                         // Adjust indices for the change in the length of
130
// the string. Do not assume that str.length() ==
131
// 1, in case of surrogates.
132
int delta = cursor - openPos - str.length();
133                         cursor -= delta;
134                         limit -= delta;
135                         // assert(cursor == openPos + str.length());
136
}
137                     // If the lookup failed, we leave things as-is and
138
// still switch to mode 0 and continue.
139
mode = 0;
140                     openPos = -1; // close off candidate
141
continue; // *** reprocess char32At(cursor)
142
}
143
144                 if (legal.contains(c)) {
145                     UTF16.append(name, c);
146                     // If we go past the longest possible name then abort.
147
// maxLen includes temporary trailing space, so use '>='.
148
if (name.length() >= maxLen) {
149                         mode = 0;
150                     }
151                 }
152
153                 // Invalid character
154
else {
155                     --cursor; // Backup and reprocess this character
156
mode = 0;
157                 }
158
159                 break;
160             }
161
162             cursor += UTF16.getCharCount(c);
163         }
164
165         offsets.contextLimit += limit - offsets.limit;
166         offsets.limit = limit;
167         // In incremental mode, only advance the cursor up to the last
168
// open delimiter candidate.
169
offsets.start = (isIncremental && openPos >= 0) ? openPos : cursor;
170     }
171 }
172
Popular Tags