TextUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package gnu.xml;
  2. import gnu.mapping.*;
  3. import gnu.xml.*;
  4. import gnu.kawa.xml.KNode;
  5. import gnu.lists.*;
  6. import gnu.text.Char;
  7. import java.math.BigDecimal;
  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. public static String stringValue (Object node)
  21. {
  22. StringBuffer sbuf = new StringBuffer(100);
  23. if (node instanceof Values)
  24. {
  25. Values vals = (Values) node;
  26. int index = 0;
  27. for (int ipos = 0; (ipos = vals.nextPos(ipos)) != 0; )
  28. {
  29. stringValue(vals.getPosPrevious(ipos), sbuf);
  30. }
  31. }
  32. else
  33. stringValue(node, sbuf);
  34. return sbuf.toString();
  35. }
  36. public static void stringValue (Object node, StringBuffer sbuf) {
  37. if (node instanceof KNode) {
  38. KNode pos = (KNode) node;
  39. NodeTree tlist = (NodeTree) pos.sequence;
  40. tlist.stringValue(tlist.posToDataIndex(pos.ipos), sbuf);
  41. } else if (node instanceof Char) {
  42. Char.print(((Char) node).intValue(), sbuf);
  43. } else if (node instanceof Character) {
  44. sbuf.append(((Character) node).charValue());
  45. } else {
  46. if (node instanceof BigDecimal)
  47. node = XMLPrinter.formatDecimal((BigDecimal) node);
  48. else if (node instanceof Double || node instanceof gnu.math.DFloNum)
  49. node = XMLPrinter.formatDouble(((Number) node).doubleValue());
  50. else if (node instanceof Float)
  51. node = XMLPrinter.formatFloat(((Number) node).floatValue());
  52. if (node != null && node != Values.empty)
  53. sbuf.append(node);
  54. }
  55. }
  56. public static void textValue (Object arg, Consumer out)
  57. {
  58. if (arg == null || (arg instanceof Values && ((Values) arg).isEmpty()))
  59. return;
  60. String str;
  61. if (arg instanceof String)
  62. str = (String) arg;
  63. else
  64. {
  65. StringBuffer sbuf = new StringBuffer();
  66. if (arg instanceof Values)
  67. {
  68. Values vals = (Values) arg;
  69. int count = -1;
  70. for (int ipos = 0; (ipos = vals.nextPos(ipos)) != 0; )
  71. {
  72. if (++count > 0)
  73. sbuf.append(' ');
  74. stringValue(vals.getPosPrevious(ipos), sbuf);
  75. }
  76. }
  77. else
  78. stringValue(arg, sbuf);
  79. str = sbuf.toString();
  80. }
  81. out.write(str);
  82. }
  83. /** Create a normalized string.
  84. * @return the original string if it was normalized; otherwise a fresh one.
  85. * (XStringType.matcyhes assumes the above.)
  86. */
  87. public static String replaceWhitespace (String str, boolean collapse)
  88. {
  89. /* #ifdef JAVA5 */
  90. StringBuilder sbuf = null;
  91. /* #else */
  92. // StringBuffer sbuf = null;
  93. /* #endif */
  94. int len = str.length();
  95. // 1: previous was single space.
  96. // 2: previous was multiple spaces or other whitespace.
  97. int prevSpace = collapse ? 1 : 0;
  98. for (int i = 0; i < len; )
  99. {
  100. char ch = str.charAt(i++);
  101. int isSpace = ch == ' ' ? 1
  102. : ch == '\t' || ch == '\r' || ch == '\n' ? 2 : 0;
  103. if (sbuf == null
  104. && (isSpace == 2
  105. || (isSpace == 1 && prevSpace > 0 && collapse)
  106. || (isSpace == 1 && i == len && collapse)))
  107. {
  108. /* #ifdef JAVA5 */
  109. sbuf = new StringBuilder();
  110. /* #else */
  111. // sbuf = new StringBuffer();
  112. /* #endif */
  113. int k = prevSpace > 0 ? i - 2 : i - 1;
  114. for (int j = 0; j < k; j++)
  115. sbuf.append(str.charAt(j));
  116. ch = ' ';
  117. }
  118. if (collapse)
  119. {
  120. if (prevSpace > 0 && isSpace == 0)
  121. {
  122. if (sbuf != null && sbuf.length() > 0)
  123. sbuf.append(' ');
  124. prevSpace = 0;
  125. }
  126. else if (isSpace == 2 || (isSpace == 1 && prevSpace > 0))
  127. prevSpace = 2;
  128. else if (isSpace > 0)
  129. prevSpace = 1;
  130. else
  131. prevSpace = 0;
  132. if (prevSpace > 0)
  133. continue;
  134. }
  135. if (sbuf != null)
  136. sbuf.append(ch);
  137. }
  138. if (sbuf != null)
  139. return sbuf.toString();
  140. else
  141. return str;
  142. }
  143. }