]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/RBNFChinesePostProcessor.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / text / RBNFChinesePostProcessor.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2004-2009, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.text;
9
10 /**
11  * A post-processor for Chinese text.
12  */
13 final class RBNFChinesePostProcessor implements RBNFPostProcessor {
14     //private NFRuleSet lastRuleSet;
15     private boolean longForm;
16     private int format;
17
18     private static final String[] rulesetNames = {
19         "%traditional", "%simplified", "%accounting", "%time"
20     };
21
22     /**
23      * Initialization routine for this instance, called once
24      * immediately after first construction and never again.  
25      */
26     public void init(RuleBasedNumberFormat formatter, String rules) {
27     }
28
29     /**
30      * Work routine.  Post process the output, which was generated by the
31      * ruleset with the given name.
32      */
33     public void process(StringBuffer buf, NFRuleSet ruleSet) {
34         // markers depend on what rule set we are using
35
36         // Commented by johnvu on the if statement since lastRuleSet is never initialized
37         //if (ruleSet != lastRuleSet) {
38             String name = ruleSet.getName();
39             for (int i = 0; i < rulesetNames.length; ++i) {
40                 if (rulesetNames[i].equals(name)) {
41                     format = i;
42                     longForm = i == 1 || i == 3;
43                     break;
44                 }
45             }
46         //}
47
48         if (longForm) {
49             for (int i = buf.indexOf("*"); i != -1; i = buf.indexOf("*", i)) {
50                 buf.delete(i, i+1);
51             }
52             return;
53         }
54
55         final String DIAN = "\u9ede"; // decimal point
56
57         final String[][] markers = {
58             { "\u842c", "\u5104", "\u5146", "\u3007" }, // marker chars, last char is the 'zero'
59             { "\u4e07", "\u4ebf", "\u5146", "\u3007" },
60             { "\u842c", "\u5104", "\u5146", "\u96f6" }
61             // need markers for time?
62         };
63
64         // remove unwanted lings
65         // a '0' (ling) with * might be removed
66         // mark off 10,000 'chunks', markers are Z, Y, W (zhao, yii, and wan)
67         // already, we avoid two lings in the same chunk -- ling without * wins
68         // now, just need  to avoid optional lings in adjacent chunks
69         // process right to left
70
71         // decision matrix:
72         // state, situation
73         //     state         none       opt.          req.
74         //     -----         ----       ----          ----
75         // none to right     none       opt.          req.  
76         // opt. to right     none   clear, none  clear right, req.
77         // req. to right     none   clear, none       req.
78
79         // mark chunks with '|' for convenience
80         {
81             String[] m = markers[format];
82             for (int i = 0; i < m.length-1; ++i) {
83                 int n = buf.indexOf(m[i]);
84                 if (n != -1) {
85                     buf.insert(n+m[i].length(), '|');
86                 }
87             }
88         }
89
90         int x = buf.indexOf(DIAN);
91         if (x == -1) {
92             x = buf.length();
93         }
94         int s = 0; // 0 = none to right, 1 = opt. to right, 2 = req. to right
95         int n = -1; // previous optional ling
96         String ling = markers[format][3];
97         while (x >= 0) {
98             int m = buf.lastIndexOf("|", x);
99             int nn = buf.lastIndexOf(ling, x);
100             int ns = 0;
101             if (nn > m) {
102                 ns = (nn > 0 && buf.charAt(nn-1) != '*') ? 2 : 1;
103             }
104             x = m - 1;
105
106             // actually much simpler, but leave this verbose for now so it's easier to follow
107             switch (s*3+ns) {
108             case 0: /* none, none */
109                 s = ns; // redundant
110                 n = -1;
111                 break;
112             case 1: /* none, opt. */
113                 s = ns;
114                 n = nn; // remember optional ling to right
115                 break;
116             case 2: /* none, req. */
117                 s = ns;
118                 n = -1;
119                 break;
120             case 3: /* opt., none */
121                 s = ns;
122                 n = -1;
123                 break;
124             case 4: /* opt., opt. */
125                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
126                 s = 0;
127                 n = -1;
128                 break;
129             case 5: /* opt., req. */
130                 buf.delete(n-1, n+ling.length()); // delete previous optional ling
131                 s = ns;
132                 n = -1;
133                 break;
134             case 6: /* req., none */
135                 s = ns;
136                 n = -1;
137                 break;
138             case 7: /* req., opt. */
139                 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
140                 s = 0;
141                 n = -1;
142                 break;
143             case 8: /* req., req. */
144                 s = ns;
145                 n = -1;
146                 break;
147             default:
148                 throw new IllegalStateException();
149             }
150         }
151
152         for (int i = buf.length(); --i >= 0;) {
153             char c = buf.charAt(i);
154             if (c == '*' || c == '|') {
155                 buf.delete(i, i+1);
156             }
157         }
158     }
159 }