1 /* 2 ******************************************************************************* 3 * Copyright (C) 2002-2012, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package org.unicode.cldr.util; 8 9 import com.ibm.icu.impl.Utility; 10 import com.ibm.icu.text.UTF16; 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 setQuoting(boolean value)18 public void setQuoting(boolean value) { 19 quoting = value; 20 } 21 isQuoting()22 public boolean isQuoting() { 23 return quoting; 24 } 25 clear()26 public void clear() { 27 quoting = false; 28 output.setLength(0); 29 } 30 length()31 public int length() { 32 return output.length(); 33 } 34 append(String string)35 public Quoter append(String string) { 36 output.append(string); 37 return this; 38 } 39 append(int codepoint)40 public Quoter append(int codepoint) { 41 return append(UTF16.valueOf(codepoint)); 42 } 43 44 // warning, allows access to internals toString()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 setQuoting(boolean value)56 public void setQuoting(boolean value) { 57 if (quoting == value) return; 58 if (quoting) { // stop quoting 59 Utility.appendToRule(output, (int) -1, true, false, quoteBuffer); // close previous quote 60 } 61 quoting = value; 62 } 63 append(String s)64 public Quoter append(String s) { 65 if (DEBUG) System.out.println("\"" + s + "\""); 66 if (quoting) { 67 Utility.appendToRule(output, s, false, false, quoteBuffer); 68 } else { 69 output.append(s); 70 } 71 return this; 72 } 73 } 74 }