01: /*
02: *******************************************************************************
03: * Copyright (C) 2002-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *******************************************************************************
06: */
07: package com.ibm.icu.dev.tool.localeconverter;
08:
09: /**
10: * A CollationItem represents a single entry in a collation sequence.
11: */
12: public class CollationItem {
13: private static final char[] OP_CHARS = { '=', '<', ';', ',', '&' };
14: private static final char[] SPECIAL_CHARS = { '&', '@', '=', '<',
15: ';', ',' };
16: public static final int NONE = 0;
17: public static final int PRIMARY = 1;
18: public static final int SECONDARY = 2;
19: public static final int TERTIARY = 3;
20: private static final int FIRST = 4;
21: public int op;
22: public String item;
23: public String expansion;
24: public String comment;
25:
26: public static final CollationItem FORWARD = new CollationItem(NONE,
27: "") {
28: public String toString() {
29: return "";
30: }
31: };
32: public static final CollationItem BACKWARD = new CollationItem(
33: NONE, "") {
34: public String toString() {
35: return "@";
36: }
37: };
38:
39: public CollationItem(String item) {
40: this .op = FIRST;
41: this .item = cleanString(item);
42: this .expansion = "";
43: }
44:
45: public CollationItem(int op, String item) {
46: this (op, item, null);
47: }
48:
49: public CollationItem(int op, String item, String expansion) {
50: this .op = Math.abs(op);
51: if (this .op > TERTIARY)
52: this .op = TERTIARY;
53: this .item = cleanString(PosixCollationBuilder.unescape(item));
54: this .expansion = cleanString(PosixCollationBuilder
55: .unescape(expansion));
56: }
57:
58: public void setComment(String comment) {
59: this .comment = comment;
60: }
61:
62: public String toString() {
63: if (expansion.length() == 0) {
64: return "" + OP_CHARS[op] + item;
65: } else {
66: return "&" + expansion + OP_CHARS[op] + item;
67: }
68: }
69:
70: private String cleanString(String source) {
71: if (source == null)
72: return "";
73: String result = source;
74: for (int i = 0; i < result.length(); i++) {
75: final char c = result.charAt(i);
76: if ((c == '@') || (c == '\t') || (c == '\n') || (c == '\f')
77: || (c == '\r') || (c == '\013')
78: || ((c <= '\u002F') && (c >= '\u0020'))
79: || ((c <= '\u003F') && (c >= '\u003A'))
80: || ((c <= '\u0060') && (c >= '\u005B'))
81: || ((c <= '\u007E') && (c >= '\u007B'))) {
82: if (i < result.length() - 1) {
83: result = result.substring(0, i) + "\\" + c
84: + result.substring(i + 1);
85: } else {
86: result = result.substring(0, i) + "\\" + c;
87: }
88: i += 2; //skip the two characters we inserted
89: }
90: }
91: return result;
92: }
93: }
|