1/*
2 *******************************************************************************
3 * Copyright (C) 2002-2012, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.util;
8
9import com.ibm.icu.impl.Utility;
10import com.ibm.icu.text.UTF16;
11
12public 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    public boolean isQuoting() {
22        return quoting;
23    }
24    public void clear() {
25        quoting = false;
26        output.setLength(0);
27    }
28    public int length() {
29        return output.length();
30    }
31    public Quoter append(String string) {
32        output.append(string);
33        return this;
34    }
35    public Quoter append(int codepoint) {
36        return append(UTF16.valueOf(codepoint));
37    }
38    // warning, allows access to internals
39    public String toString() {
40        setQuoting(false); // finish quoting
41        return output.toString();
42    }
43    /**
44     * Implements standard ICU rule quoting
45     */
46    public static class RuleQuoter extends Quoter {
47        private StringBuffer quoteBuffer = new StringBuffer();
48        public void setQuoting(boolean value) {
49            if (quoting == value) return;
50            if (quoting) { // stop quoting
51                Utility.appendToRule(output, (int)-1, true, false, quoteBuffer); // close previous quote
52            }
53            quoting = value;
54        }
55        public Quoter append(String s) {
56            if (DEBUG) System.out.println("\"" + s + "\"");
57            if (quoting) {
58                Utility.appendToRule(output, s, false, false, quoteBuffer);
59            } else {
60                output.append(s);
61            }
62            return this;
63        }
64    }
65}