KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xml > TextUtils


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 JavaDoc;
7
8 public class TextUtils
9 {
10   public static String JavaDoc asString (Object JavaDoc node)
11   {
12     if (node == Values.empty || node == null)
13       return "";
14     else if (node instanceof Values)
15       throw new ClassCastException JavaDoc();
16     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
17     stringValue(node, sbuf);
18     return sbuf.toString();
19   }
20
21   public static String JavaDoc stringValue (Object JavaDoc node)
22   {
23     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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 JavaDoc node, StringBuffer JavaDoc 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 JavaDoc)
55       node = XMLPrinter.formatDecimal((BigDecimal JavaDoc) node);
56     else if (node instanceof Double JavaDoc || node instanceof gnu.math.DFloNum)
57       node = XMLPrinter.formatDouble(((Number JavaDoc) node).doubleValue());
58     else if (node instanceof Float JavaDoc)
59       node = XMLPrinter.formatFloat(((Number JavaDoc) node).floatValue());
60     if (node != null && node != Values.empty)
61       sbuf.append(node);
62   }
63
64   public static void textValue (Object JavaDoc arg, Consumer out)
65   {
66     if (arg == null || (arg instanceof Values && ((Values) arg).isEmpty()))
67       return;
68     String JavaDoc str;
69     if (arg instanceof String JavaDoc)
70       str = (String JavaDoc) arg;
71     else
72       {
73         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
74         if (arg instanceof Values)
75           {
76             Object JavaDoc[] 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 JavaDoc replaceWhitespace (String JavaDoc str, boolean collapse)
92   {
93     /* #ifdef JAVA5 */
94     // StringBuilder sbuf = null;
95
/* #else */
96     StringBuffer JavaDoc sbuf = null;
97     /* #endif */
98     int len = str.length();
99     // 1: previous was single space.
100
// 2: previous was multiple spaces or other whitespace.
101
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             /* #ifdef JAVA5 */
113             // sbuf = new StringBuilder();
114
/* #else */
115             sbuf = new StringBuffer JavaDoc();
116             /* #endif */
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