001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.impl;
008:
009: import com.ibm.icu.text.Replaceable;
010: import com.ibm.icu.text.ReplaceableString;
011: import com.ibm.icu.text.UnicodeMatcher;
012: import com.ibm.icu.text.Transliterator;
013:
014: /**
015: * @author Ram
016: */
017: //This class contains utility functions so testing not needed
018: ///CLOVER:OFF
019: public class UtilityExtensions {
020: /**
021: * Append the given string to the rule. Calls the single-character
022: * version of appendToRule for each character.
023: */
024: public static void appendToRule(StringBuffer rule, String text,
025: boolean isLiteral, boolean escapeUnprintable,
026: StringBuffer quoteBuf) {
027: for (int i = 0; i < text.length(); ++i) {
028: // Okay to process in 16-bit code units here
029: Utility.appendToRule(rule, text.charAt(i), isLiteral,
030: escapeUnprintable, quoteBuf);
031: }
032: }
033:
034: /**
035: * Given a matcher reference, which may be null, append its
036: * pattern as a literal to the given rule.
037: */
038: public static void appendToRule(StringBuffer rule,
039: UnicodeMatcher matcher, boolean escapeUnprintable,
040: StringBuffer quoteBuf) {
041: if (matcher != null) {
042: appendToRule(rule, matcher.toPattern(escapeUnprintable),
043: true, escapeUnprintable, quoteBuf);
044: }
045: }
046:
047: /**
048: * For debugging purposes; format the given text in the form
049: * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start
050: * and limit, and the || indicate the start and limit.
051: */
052: public static String formatInput(ReplaceableString input,
053: Transliterator.Position pos) {
054: StringBuffer appendTo = new StringBuffer();
055: formatInput(appendTo, input, pos);
056: return com.ibm.icu.impl.Utility.escape(appendTo.toString());
057: }
058:
059: /**
060: * For debugging purposes; format the given text in the form
061: * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start
062: * and limit, and the || indicate the start and limit.
063: */
064: public static StringBuffer formatInput(StringBuffer appendTo,
065: ReplaceableString input, Transliterator.Position pos) {
066: if (0 <= pos.contextStart && pos.contextStart <= pos.start
067: && pos.start <= pos.limit
068: && pos.limit <= pos.contextLimit
069: && pos.contextLimit <= input.length()) {
070:
071: String b, c, d;
072: //a = input.substring(0, pos.contextStart);
073: b = input.substring(pos.contextStart, pos.start);
074: c = input.substring(pos.start, pos.limit);
075: d = input.substring(pos.limit, pos.contextLimit);
076: //e = input.substring(pos.contextLimit, input.length());
077: appendTo.//append(a).
078: append('{').append(b).append('|').append(c).append(
079: '|').append(d).append('}')
080: //.append(e)
081: ;
082: } else {
083: appendTo.append("INVALID Position {cs=" + pos.contextStart
084: + ", s=" + pos.start + ", l=" + pos.limit + ", cl="
085: + pos.contextLimit + "} on " + input);
086: }
087: return appendTo;
088: }
089:
090: /**
091: * Convenience method.
092: */
093: public static String formatInput(Replaceable input,
094: Transliterator.Position pos) {
095: return formatInput((ReplaceableString) input, pos);
096: }
097:
098: /**
099: * Convenience method.
100: */
101: public static StringBuffer formatInput(StringBuffer appendTo,
102: Replaceable input, Transliterator.Position pos) {
103: return formatInput(appendTo, (ReplaceableString) input, pos);
104: }
105:
106: }
107: //CLOVER:ON
|