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.test.util;
08:
09: import com.ibm.icu.text.UTF16;
10: import com.ibm.icu.impl.Utility;
11:
12: public abstract class Quoter {
13: private static boolean DEBUG = false;
14:
15: protected boolean quoting = false;
16: protected StringBuffer output = new StringBuffer();
17:
18: public void setQuoting(boolean value) {
19: quoting = value;
20: }
21:
22: public boolean isQuoting() {
23: return quoting;
24: }
25:
26: public void clear() {
27: quoting = false;
28: output.setLength(0);
29: }
30:
31: public int length() {
32: return output.length();
33: }
34:
35: public Quoter append(String string) {
36: output.append(string);
37: return this ;
38: }
39:
40: public Quoter append(int codepoint) {
41: return append(UTF16.valueOf(codepoint));
42: }
43:
44: // warning, allows access to internals
45: public String toString() {
46: setQuoting(false); // finish quoting
47: return output.toString();
48: }
49:
50: /**
51: * Implements standard ICU rule quoting
52: */
53: public static class RuleQuoter extends Quoter {
54: private StringBuffer quoteBuffer = new StringBuffer();
55:
56: public void setQuoting(boolean value) {
57: if (quoting == value)
58: return;
59: if (quoting) { // stop quoting
60: Utility.appendToRule(output, (int) -1, true, false,
61: quoteBuffer); // close previous quote
62: }
63: quoting = value;
64: }
65:
66: public Quoter append(String s) {
67: if (DEBUG)
68: System.out.println("\"" + s + "\"");
69: if (quoting) {
70: Utility.appendToRule(output, s, false, false,
71: quoteBuffer);
72: } else {
73: output.append(s);
74: }
75: return this;
76: }
77: }
78: }
|