2 *******************************************************************************
\r
3 * Copyright (C) 2004-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.text;
\r
11 * A post-processor for Chinese text.
\r
13 final class RBNFChinesePostProcessor implements RBNFPostProcessor {
\r
14 //private NFRuleSet lastRuleSet;
\r
15 private boolean longForm;
\r
18 private static final String[] rulesetNames = {
\r
19 "%traditional", "%simplified", "%accounting", "%time"
\r
23 * Initialization routine for this instance, called once
\r
24 * immediately after first construction and never again.
\r
26 public void init(RuleBasedNumberFormat formatter, String rules) {
\r
30 * Work routine. Post process the output, which was generated by the
\r
31 * ruleset with the given name.
\r
33 public void process(StringBuffer buf, NFRuleSet ruleSet) {
\r
34 // markers depend on what rule set we are using
\r
36 // Commented by johnvu on the if statement since lastRuleSet is never initialized
\r
37 //if (ruleSet != lastRuleSet) {
\r
38 String name = ruleSet.getName();
\r
39 for (int i = 0; i < rulesetNames.length; ++i) {
\r
40 if (rulesetNames[i].equals(name)) {
\r
42 longForm = i == 1 || i == 3;
\r
49 for (int i = buf.indexOf("*"); i != -1; i = buf.indexOf("*", i)) {
\r
55 final String DIAN = "\u9ede"; // decimal point
\r
57 final String[][] markers = {
\r
58 { "\u842c", "\u5104", "\u5146", "\u3007" }, // marker chars, last char is the 'zero'
\r
59 { "\u4e07", "\u4ebf", "\u5146", "\u3007" },
\r
60 { "\u842c", "\u5104", "\u5146", "\u96f6" }
\r
61 // need markers for time?
\r
64 // remove unwanted lings
\r
65 // a '0' (ling) with * might be removed
\r
66 // mark off 10,000 'chunks', markers are Z, Y, W (zhao, yii, and wan)
\r
67 // already, we avoid two lings in the same chunk -- ling without * wins
\r
68 // now, just need to avoid optional lings in adjacent chunks
\r
69 // process right to left
\r
73 // state none opt. req.
\r
74 // ----- ---- ---- ----
\r
75 // none to right none opt. req.
\r
76 // opt. to right none clear, none clear right, req.
\r
77 // req. to right none clear, none req.
\r
79 // mark chunks with '|' for convenience
\r
81 String[] m = markers[format];
\r
82 for (int i = 0; i < m.length-1; ++i) {
\r
83 int n = buf.indexOf(m[i]);
\r
85 buf.insert(n+m[i].length(), '|');
\r
90 int x = buf.indexOf(DIAN);
\r
94 int s = 0; // 0 = none to right, 1 = opt. to right, 2 = req. to right
\r
95 int n = -1; // previous optional ling
\r
96 String ling = markers[format][3];
\r
98 int m = buf.lastIndexOf("|", x);
\r
99 int nn = buf.lastIndexOf(ling, x);
\r
102 ns = (nn > 0 && buf.charAt(nn-1) != '*') ? 2 : 1;
\r
106 // actually much simpler, but leave this verbose for now so it's easier to follow
\r
108 case 0: /* none, none */
\r
109 s = ns; // redundant
\r
112 case 1: /* none, opt. */
\r
114 n = nn; // remember optional ling to right
\r
116 case 2: /* none, req. */
\r
120 case 3: /* opt., none */
\r
124 case 4: /* opt., opt. */
\r
125 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
\r
129 case 5: /* opt., req. */
\r
130 buf.delete(n-1, n+ling.length()); // delete previous optional ling
\r
134 case 6: /* req., none */
\r
138 case 7: /* req., opt. */
\r
139 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
\r
143 case 8: /* req., req. */
\r
148 throw new IllegalStateException();
\r
152 for (int i = buf.length(); --i >= 0;) {
\r
153 char c = buf.charAt(i);
\r
154 if (c == '*' || c == '|') {
\r
155 buf.delete(i, i+1);
\r