001: package gnu.xml;
002:
003: import gnu.mapping.*;
004: import gnu.xml.*;
005: import gnu.kawa.xml.KNode;
006: import gnu.lists.*;
007: import java.math.BigDecimal;
008:
009: public class TextUtils {
010: public static String asString(Object node) {
011: if (node == Values.empty || node == null)
012: return "";
013: else if (node instanceof Values)
014: throw new ClassCastException();
015: StringBuffer sbuf = new StringBuffer(100);
016: stringValue(node, sbuf);
017: return sbuf.toString();
018: }
019:
020: public static String stringValue(Object node) {
021: StringBuffer sbuf = new StringBuffer(100);
022: if (node instanceof Values) {
023: TreeList tlist = (TreeList) node;
024: int index = 0;
025: for (;;) {
026: int kind = tlist.getNextKind(index);
027: if (kind == Sequence.EOF_VALUE)
028: break;
029: if (kind == Sequence.OBJECT_VALUE)
030: stringValue(tlist.getPosNext(index), sbuf);
031: else
032: tlist
033: .stringValue(tlist.posToDataIndex(index),
034: sbuf);
035: index = tlist.nextPos(index);
036: }
037: } else
038: stringValue(node, sbuf);
039: return sbuf.toString();
040: }
041:
042: public static void stringValue(Object node, StringBuffer sbuf) {
043: if (node instanceof KNode) {
044: KNode pos = (KNode) node;
045: NodeTree tlist = (NodeTree) pos.sequence;
046: tlist.stringValue(tlist.posToDataIndex(pos.ipos), sbuf);
047: return;
048: }
049: if (node instanceof BigDecimal)
050: node = XMLPrinter.formatDecimal((BigDecimal) node);
051: else if (node instanceof Double
052: || node instanceof gnu.math.DFloNum)
053: node = XMLPrinter.formatDouble(((Number) node)
054: .doubleValue());
055: else if (node instanceof Float)
056: node = XMLPrinter.formatFloat(((Number) node).floatValue());
057: if (node != null && node != Values.empty)
058: sbuf.append(node);
059: }
060:
061: public static void textValue(Object arg, Consumer out) {
062: if (arg == null
063: || (arg instanceof Values && ((Values) arg).isEmpty()))
064: return;
065: String str;
066: if (arg instanceof String)
067: str = (String) arg;
068: else {
069: StringBuffer sbuf = new StringBuffer();
070: if (arg instanceof Values) {
071: Object[] vals = ((Values) arg).getValues();
072: for (int i = 0; i < vals.length; i++) {
073: if (i > 0)
074: sbuf.append(' ');
075: stringValue(vals[i], sbuf);
076: }
077: } else
078: stringValue(arg, sbuf);
079: str = sbuf.toString();
080: }
081: out.write(str);
082: }
083:
084: /** Create a normalized string.
085: * @return the original string if it was normalized; otherwise a fresh one.
086: * (XStringType.matcyhes assumes the above.)
087: */
088: public static String replaceWhitespace(String str, boolean collapse) {
089: /* #ifdef JAVA5 */
090: // StringBuilder sbuf = null;
091: /* #else */
092: StringBuffer sbuf = null;
093: /* #endif */
094: int len = str.length();
095: // 1: previous was single space.
096: // 2: previous was multiple spaces or other whitespace.
097: int prevSpace = collapse ? 1 : 0;
098: for (int i = 0; i < len;) {
099: char ch = str.charAt(i++);
100: int isSpace = ch == ' ' ? 1 : ch == '\t' || ch == '\r'
101: || ch == '\n' ? 2 : 0;
102: if (sbuf == null
103: && (isSpace == 2
104: || (isSpace == 1 && prevSpace > 0 && collapse) || (isSpace == 1
105: && i == len && collapse))) {
106: /* #ifdef JAVA5 */
107: // sbuf = new StringBuilder();
108: /* #else */
109: sbuf = new StringBuffer();
110: /* #endif */
111: int k = prevSpace > 0 ? i - 2 : i - 1;
112: for (int j = 0; j < k; j++)
113: sbuf.append(str.charAt(j));
114: ch = ' ';
115: }
116: if (collapse) {
117: if (prevSpace > 0 && isSpace == 0) {
118: if (sbuf != null && sbuf.length() > 0)
119: sbuf.append(' ');
120: prevSpace = 0;
121: } else if (isSpace == 2
122: || (isSpace == 1 && prevSpace > 0))
123: prevSpace = 2;
124: else if (isSpace > 0)
125: prevSpace = 1;
126: else
127: prevSpace = 0;
128: if (prevSpace > 0)
129: continue;
130: }
131: if (sbuf != null)
132: sbuf.append(ch);
133: }
134: if (sbuf != null)
135: return sbuf.toString();
136: else
137: return str;
138:
139: }
140: }
|