2 *******************************************************************************
3 * Copyright (C) 2004-2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.text;
11 * A post-processor for Chinese text.
13 final class RBNFChinesePostProcessor implements RBNFPostProcessor {
14 //private NFRuleSet lastRuleSet;
15 private boolean longForm;
18 private static final String[] rulesetNames = {
19 "%traditional", "%simplified", "%accounting", "%time"
23 * Initialization routine for this instance, called once
24 * immediately after first construction and never again.
26 public void init(RuleBasedNumberFormat formatter, String rules) {
30 * Work routine. Post process the output, which was generated by the
31 * ruleset with the given name.
33 public void process(StringBuffer buf, NFRuleSet ruleSet) {
34 // markers depend on what rule set we are using
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)) {
42 longForm = i == 1 || i == 3;
49 for (int i = buf.indexOf("*"); i != -1; i = buf.indexOf("*", i)) {
55 final String DIAN = "\u9ede"; // decimal point
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?
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
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.
79 // mark chunks with '|' for convenience
81 String[] m = markers[format];
82 for (int i = 0; i < m.length-1; ++i) {
83 int n = buf.indexOf(m[i]);
85 buf.insert(n+m[i].length(), '|');
90 int x = buf.indexOf(DIAN);
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];
98 int m = buf.lastIndexOf("|", x);
99 int nn = buf.lastIndexOf(ling, x);
102 ns = (nn > 0 && buf.charAt(nn-1) != '*') ? 2 : 1;
106 // actually much simpler, but leave this verbose for now so it's easier to follow
108 case 0: /* none, none */
112 case 1: /* none, opt. */
114 n = nn; // remember optional ling to right
116 case 2: /* none, req. */
120 case 3: /* opt., none */
124 case 4: /* opt., opt. */
125 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
129 case 5: /* opt., req. */
130 buf.delete(n-1, n+ling.length()); // delete previous optional ling
134 case 6: /* req., none */
138 case 7: /* req., opt. */
139 buf.delete(nn-1, nn+ling.length()); // delete current optional ling
143 case 8: /* req., req. */
148 throw new IllegalStateException();
152 for (int i = buf.length(); --i >= 0;) {
153 char c = buf.charAt(i);
154 if (c == '*' || c == '|') {