1 package gnu.xml; 2 import gnu.mapping.*; 3 import gnu.xml.*; 4 import gnu.kawa.xml.KNode; 5 import gnu.lists.*; 6 import java.math.BigDecimal ; 7 8 public class TextUtils 9 { 10 public static String asString (Object node) 11 { 12 if (node == Values.empty || node == null) 13 return ""; 14 else if (node instanceof Values) 15 throw new ClassCastException (); 16 StringBuffer sbuf = new StringBuffer (100); 17 stringValue(node, sbuf); 18 return sbuf.toString(); 19 } 20 21 public static String stringValue (Object node) 22 { 23 StringBuffer sbuf = new StringBuffer (100); 24 if (node instanceof Values) 25 { 26 TreeList tlist = (TreeList) node; 27 int index = 0; 28 for (;;) 29 { 30 int kind = tlist.getNextKind(index); 31 if (kind == Sequence.EOF_VALUE) 32 break; 33 if (kind == Sequence.OBJECT_VALUE) 34 stringValue(tlist.getPosNext(index), sbuf); 35 else 36 tlist.stringValue(tlist.posToDataIndex(index), sbuf); 37 index = tlist.nextPos(index); 38 } 39 } 40 else 41 stringValue(node, sbuf); 42 return sbuf.toString(); 43 } 44 45 public static void stringValue (Object node, StringBuffer sbuf) 46 { 47 if (node instanceof KNode) 48 { 49 KNode pos = (KNode) node; 50 NodeTree tlist = (NodeTree) pos.sequence; 51 tlist.stringValue(tlist.posToDataIndex(pos.ipos), sbuf); 52 return; 53 } 54 if (node instanceof BigDecimal ) 55 node = XMLPrinter.formatDecimal((BigDecimal ) node); 56 else if (node instanceof Double || node instanceof gnu.math.DFloNum) 57 node = XMLPrinter.formatDouble(((Number ) node).doubleValue()); 58 else if (node instanceof Float ) 59 node = XMLPrinter.formatFloat(((Number ) node).floatValue()); 60 if (node != null && node != Values.empty) 61 sbuf.append(node); 62 } 63 64 public static void textValue (Object arg, Consumer out) 65 { 66 if (arg == null || (arg instanceof Values && ((Values) arg).isEmpty())) 67 return; 68 String str; 69 if (arg instanceof String ) 70 str = (String ) arg; 71 else 72 { 73 StringBuffer sbuf = new StringBuffer (); 74 if (arg instanceof Values) 75 { 76 Object [] vals = ((Values) arg).getValues(); 77 for (int i = 0; i < vals.length; i++) 78 { 79 if (i > 0) 80 sbuf.append(' '); 81 stringValue(vals[i], sbuf); 82 } 83 } 84 else 85 stringValue(arg, sbuf); 86 str = sbuf.toString(); 87 } 88 out.write(str); 89 } 90 91 public static String replaceWhitespace (String str, boolean collapse) 92 { 93 94 96 StringBuffer sbuf = null; 97 98 int len = str.length(); 99 int prevSpace = collapse ? 1 : 0; 102 for (int i = 0; i < len; ) 103 { 104 char ch = str.charAt(i++); 105 int isSpace = ch == ' ' ? 1 106 : ch == '\t' || ch == '\r' || ch == '\n' ? 2 : 0; 107 if (sbuf == null 108 && (isSpace == 2 109 || (isSpace == 1 && prevSpace > 0 && collapse) 110 || (isSpace == 1 && i == len && collapse))) 111 { 112 113 115 sbuf = new StringBuffer (); 116 117 int k = prevSpace > 0 ? i - 2 : i - 1; 118 for (int j = 0; j < k; j++) 119 sbuf.append(str.charAt(j)); 120 ch = ' '; 121 } 122 if (collapse) 123 { 124 if (prevSpace > 0 && isSpace == 0) 125 { 126 if (sbuf != null && sbuf.length() > 0) 127 sbuf.append(' '); 128 prevSpace = 0; 129 } 130 else if (isSpace == 2 || (isSpace == 1 && prevSpace > 0)) 131 prevSpace = 2; 132 else if (isSpace > 0) 133 prevSpace = 1; 134 else 135 prevSpace = 0; 136 if (prevSpace > 0) 137 continue; 138 } 139 if (sbuf != null) 140 sbuf.append(ch); 141 } 142 if (sbuf != null) 143 return sbuf.toString(); 144 else 145 return str; 146 147 } 148 } 149 | Popular Tags |