KickJava   Java API By Example, From Geeks To Geeks.

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


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

7
8 package com.ibm.icu.text;
9
10 import com.ibm.icu.impl.Utility;
11
12 /**
13  * A post-processor for Chinese text.
14  */

15 final class RBNFChinesePostProcessor extends RBNFPostProcessor {
16     private NFRuleSet lastRuleSet;
17     private boolean longForm;
18     private int format;
19
20     private static final String JavaDoc[] rulesetNames = {
21         "%traditional", "%simplified", "%accounting", "%time"
22     };
23
24     /**
25      * Initialization routine for this instance, called once
26      * immediately after first construction and never again.
27      */

28     void init(RuleBasedNumberFormat formatter, String JavaDoc rules) {
29     }
30
31     /**
32      * Work routine. Post process the output, which was generated by the
33      * ruleset with the given name.
34      */

35     void process(StringBuffer JavaDoc buf, NFRuleSet ruleSet) {
36         // markers depend on what rule set we are using
37

38         if (ruleSet != lastRuleSet) {
39             String JavaDoc name = ruleSet.getName();
40             for (int i = 0; i < rulesetNames.length; ++i) {
41                 if (rulesetNames[i].equals(name)) {
42                     format = i;
43                     longForm = i == 1 || i == 3;
44                     break;
45                 }
46             }
47         }
48
49         if (longForm) {
50             for (int i = Utility.indexOf(buf,"*"); i != -1; i = Utility.indexOf(buf, "*", i)) {
51                 buf.delete(i, i+1);
52             }
53             return;
54         }
55
56         final String JavaDoc DIAN = "\u9ede"; // decimal point
57

58         final String JavaDoc[][] markers = {
59             { "\u842c", "\u5104", "\u5146", "\u3007" }, // marker chars, last char is the 'zero'
60
{ "\u4e07", "\u4ebf", "\u5146", "\u3007" },
61             { "\u842c", "\u5104", "\u5146", "\u96f6" }
62             // need markers for time?
63
};
64
65         // remove unwanted lings
66
// a '0' (ling) with * might be removed
67
// mark off 10,000 'chunks', markers are Z, Y, W (zhao, yii, and wan)
68
// already, we avoid two lings in the same chunk -- ling without * wins
69
// now, just need to avoid optional lings in adjacent chunks
70
// process right to left
71

72         // decision matrix:
73
// state, situation
74
// state none opt. req.
75
// ----- ---- ---- ----
76
// none to right none opt. req.
77
// opt. to right none clear, none clear right, req.
78
// req. to right none clear, none req.
79

80         // mark chunks with '|' for convenience
81
{
82             String JavaDoc[] m = markers[format];
83             for (int i = 0; i < m.length-1; ++i) {
84                 int n = Utility.indexOf(buf, m[i]);
85                 if (n != -1) {
86                     buf.insert(n+m[i].length(), '|');
87                 }
88             }
89         }
90
91         int x = Utility.indexOf(buf, DIAN);
92         if (x == -1) {
93             x = buf.length();
94         }
95         int s = 0; // 0 = none to right, 1 = opt. to right, 2 = req. to right
96
int n = -1; // previous optional ling
97
String JavaDoc ling = markers[format][3];
98         while (x >= 0) {
99             int m = Utility.lastIndexOf(buf, "|", x);
100             int nn = Utility.lastIndexOf(buf, ling, x);
101             int ns = 0;
102             if (nn > m) {
103                 ns = (nn > 0 && buf.charAt(nn-1) != '*') ? 2 : 1;
104             }
105             x = m - 1;
106
107             // actually much simpler, but leave this verbose for now so it's easier to follow
108
switch (s*3+ns) {
109             case 0: /* none, none */
110                 s = ns; // redundant
111
n = -1;
112                 break;
113             case 1: /* none, opt. */
114                 s = ns;
115                 n = nn; // remember optional ling to right
116
break;
117             case 2: /* none, req. */
118                 s = ns;
119                 n = -1;
120                 break;
121             case 3: /* opt., none */
122                 s = ns;
123                 n = -1;
124                 break;
125             case 4: /* opt., opt. */
126                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
127
s = 0;
128                 n = -1;
129                 break;
130             case 5: /* opt., req. */
131                 buf.delete(n-1, n+ling.length()); // delete previous optional ling
132
s = ns;
133                 n = -1;
134                 break;
135             case 6: /* req., none */
136                 s = ns;
137                 n = -1;
138                 break;
139             case 7: /* req., opt. */
140                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
141
s = 0;
142                 n = -1;
143                 break;
144             case 8: /* req., req. */
145                 s = ns;
146                 n = -1;
147                 break;
148             default:
149                 throw new IllegalStateException JavaDoc();
150             }
151         }
152
153         for (int i = buf.length(); --i >= 0;) {
154             char c = buf.charAt(i);
155             if (c == '*' || c == '|') {
156                 buf.delete(i, i+1);
157             }
158         }
159     }
160 }
161
Popular Tags