01: /*
02: * Copyright (C) 1996-2004, International Business Machines Corporation and
03: * others. All Rights Reserved.
04: */
05: package com.ibm.icu.text;
06:
07: import com.ibm.icu.lang.*;
08:
09: /**
10: * A transliterator that performs character to name mapping.
11: * It generates the Perl syntax \N{name}.
12: * @author Alan Liu
13: */
14: class UnicodeNameTransliterator extends Transliterator {
15:
16: static final String _ID = "Any-Name";
17:
18: static final String OPEN_DELIM = "\\N{";
19: static final char CLOSE_DELIM = '}';
20: static final int OPEN_DELIM_LEN = 3;
21:
22: /**
23: * System registration hook.
24: */
25: static void register() {
26: Transliterator.registerFactory(_ID,
27: new Transliterator.Factory() {
28: public Transliterator getInstance(String ID) {
29: return new UnicodeNameTransliterator(null);
30: }
31: });
32: }
33:
34: /**
35: * Constructs a transliterator.
36: */
37: public UnicodeNameTransliterator(UnicodeFilter filter) {
38: super (_ID, filter);
39: }
40:
41: /**
42: * Implements {@link Transliterator#handleTransliterate}.
43: */
44: protected void handleTransliterate(Replaceable text,
45: Position offsets, boolean isIncremental) {
46: int cursor = offsets.start;
47: int limit = offsets.limit;
48:
49: StringBuffer str = new StringBuffer();
50: str.append(OPEN_DELIM);
51: int len;
52: String name;
53:
54: while (cursor < limit) {
55: int c = text.char32At(cursor);
56: if ((name = UCharacter.getExtendedName(c)) != null) {
57:
58: str.setLength(OPEN_DELIM_LEN);
59: str.append(name).append(CLOSE_DELIM);
60:
61: int clen = UTF16.getCharCount(c);
62: text.replace(cursor, cursor + clen, str.toString());
63: len = str.length();
64: cursor += len; // advance cursor by 1 and adjust for new text
65: limit += len - clen; // change in length
66: } else {
67: ++cursor;
68: }
69: }
70:
71: offsets.contextLimit += limit - offsets.limit;
72: offsets.limit = limit;
73: offsets.start = cursor;
74: }
75: }
|