KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > Strings


1 /*
2  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
3  * Copyright (C) 1996 - 2000 BULL
4  * Copyright (C) 1996 - 2000 INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Dyade
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package fr.dyade.aaa.util;
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import java.util.*;
29 import java.io.*;
30
31
32 /**
33  * This class provides a set of static functions for building string
34  * representations of complex structures.
35  */

36 public class Strings {
37   /**
38    * Provides a string representation of an object. Checks if there exists
39    * in this class a specialized <code>toString</code> function for the object
40    * class, or calls the <code>toString</code> function of the object.
41    *
42    * @param output a buffer to print the object into
43    * @param obj the object to print
44    */

45   public static final void toString(StringBuffer JavaDoc output, Object JavaDoc obj) {
46     if (obj == null) {
47       output.append("null");
48       return;
49     }
50
51     if (obj instanceof String JavaDoc) {
52       toString(output, (String JavaDoc) obj);
53       return;
54     }
55     if (obj instanceof List) {
56       toString(output, (List) obj);
57       return;
58     }
59     if (obj instanceof Collection) {
60       toString(output, (Collection) obj);
61       return;
62     }
63     if (obj instanceof Map) {
64       toString(output, (Map) obj);
65       return;
66     }
67     if (obj instanceof Map.Entry) {
68       toString(output, (Map.Entry) obj);
69       return;
70     }
71
72     Class JavaDoc type = obj.getClass();
73     if (type.isArray()) {
74       toString(output, obj, type.getComponentType());
75       return;
76     }
77
78     try {
79       Class JavaDoc[] argstype = new Class JavaDoc[1];
80       argstype[0] = Class.forName("java.io.StringBuffer");
81       Method JavaDoc method = type.getMethod("toString", argstype);
82       Object JavaDoc[] args = new Object JavaDoc[1];
83       args[0] = output;
84       method.invoke(obj, args);
85       return;
86     } catch (Exception JavaDoc exc) {}
87
88     output.append(obj.toString());
89   }
90
91   /**
92    * Provides a string representation of an object.
93    * Calls <code>toString(StringBuffer)</code>.
94    *
95    * @param obj the object to print
96    * @return a string representation of the object
97    */

98   public static final String JavaDoc toString(Object JavaDoc obj) {
99     if (obj == null)
100       return "null";
101
102     if (obj instanceof String JavaDoc) {
103       // this avoids creating a useless StringBuffer
104
return toString((String JavaDoc) obj);
105     }
106
107     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
108     toString(output, obj);
109     return output.toString();
110   }
111
112   /**
113    * Provides a Java string literal representing the parameter string.
114    * This includes surrounding double quotes, and quoted special characters,
115    * including UTF escape sequences when necessary.
116    * <p>
117    * This function works only for ASCII character encoding, and assumes
118    * this is the default encoding.
119    *
120    * @param output a byte buffer to print the object into
121    * @param str the string to print
122    */

123   public static final void toByteArray(ByteArrayOutputStream output,
124                        String JavaDoc str) {
125     if (str == null) {
126       return;
127     }
128
129     output.write(34); // '"'
130

131     int max = str.length();
132     for (int i = 0; i < max; i ++) {
133       // gets the numeric value of the unicode character
134
int b = (int) str.charAt(i);
135       if ((b >= 32) && (b <= 126)) {
136     // ASCII printable character, includes '"' and '\\'
137
switch (b) {
138     case 34: // '"'
139
case 92: // '\\'
140
output.write(92); // '\\'
141
break;
142     }
143     output.write(b);
144       } else {
145     output.write(92); // '\\'
146
switch (b) {
147     case 8: // backspace
148
output.write(98); // 'b'
149
break;
150     case 9: // horizontal tab
151
output.write(116); // 't'
152
break;
153     case 10: // linefeed
154
output.write(110); // 'n'
155
break;
156     case 12: // form feed
157
output.write(102); // 'f'
158
break;
159     case 13: // carriage return
160
output.write(114); // 'r'
161
break;
162     default:
163       // UTF escape sequence
164
output.write(117); // 'u'
165
int b3 = b >> 4;
166       int b4 = b & 0xf;
167       if (b4 < 10)
168         b4 += 48; // '0'
169
else
170         b4 += 87; // 'a' - 10
171
int b2 = b3 >> 4;
172       b3 &= 0xf;
173       if (b3 < 10)
174         b3 += 48; // '0'
175
else
176         b3 += 87; // 'a' - 10
177
int b1 = b2 >> 4;
178       b2 &= 0xf;
179       if (b2 < 10)
180         b2 += 48; // '0'
181
else
182         b2 += 87; // 'a' - 10
183
if (b1 < 10)
184         b1 += 48; // '0'
185
else
186         b1 += 87; // 'a' - 10
187
output.write(b1);
188       output.write(b2);
189       output.write(b3);
190       output.write(b4);
191       break;
192     }
193       }
194     }
195
196     output.write(34); // '"'
197
}
198
199   /**
200    * Provides a Java string literal representing the parameter string.
201    * This includes surrounding double quotes, and quoted special characters,
202    * including UTF escape sequences when necessary.
203    * <p>
204    * This function works only for ASCII character encoding, and assumes
205    * this is the default encoding.
206    *
207    * @param output a string buffer to print the object into
208    * @param str the string to print
209    */

210   public static final void toString(StringBuffer JavaDoc output, String JavaDoc str) {
211     if (str == null) {
212       output.append("null");
213       return;
214     }
215
216     output.append(toString(str));
217   }
218
219   /**
220    * Provides a Java string literal representing the parameter string.
221    * This includes surrounding double quotes, and quoted special characters,
222    * including UTF escape sequences when necessary.
223    * <p>
224    * This function works only for ASCII character encoding, and assumes
225    * this is the default encoding.
226    *
227    * @param str the string to print
228    * @return a Java string literal representation of the string
229    */

230   public static final String JavaDoc toString(String JavaDoc str) {
231     if (str == null)
232       return "null";
233
234     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
235     toByteArray(buffer, str);
236     return buffer.toString();
237   }
238
239   /**
240    * Controls the formatting of lists of objects. Lists with a number of
241    * elements up to <code>listMax</code> are entirely printed. A value of
242    * <code>-1</code> leads to complete printing of the list, whatever its size.
243    * <p>
244    * This variable, when used in an agent server, may be set by the debug
245    * variable <code>Debug.var.fr.dyade.aaa.util.listMax</code>. Its default value is
246    * <code>10</code>.
247    */

248   public static int listMax = 10;
249
250   /**
251    * Controls the formatting of lists of objects. Lists with a number of
252    * elements greater than <code>listMax</code> are partially printed, with
253    * the <code>listBorder</code> leading and trailing elements.
254    * <p>
255    * This variable, when used in an agent server, may be set by the debug
256    * variable <code>Debug.var.fr.dyade.aaa.util.listBorder</code>. Its default value is
257    * <code>3</code>.
258    */

259   public static int listBorder = 3;
260
261   /**
262    * Provides a string representation of an array.
263    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
264    *
265    * @param output a buffer to print the object into
266    * @param obj the array to print
267    * @param type the type of the array components
268    */

269   public static final void toString(StringBuffer JavaDoc output,
270                     Object JavaDoc obj, Class JavaDoc type) {
271     if (obj == null) {
272       output.append("null");
273       return;
274     }
275
276     if (type.isPrimitive()) {
277       if (type == Boolean.TYPE)
278     toString(output, (boolean[]) obj);
279       else if (type == Character.TYPE)
280     toString(output, (char[]) obj);
281       else if (type == Byte.TYPE)
282     toString(output, (byte[]) obj);
283       else if (type == Short.TYPE)
284     toString(output, (short[]) obj);
285       else if (type == Integer.TYPE)
286     toString(output, (int[]) obj);
287       else if (type == Long.TYPE)
288     toString(output, (long[]) obj);
289       else if (type == Float.TYPE)
290     toString(output, (float[]) obj);
291       else if (type == Double.TYPE)
292     toString(output, (double[]) obj);
293       return;
294     }
295
296     Object JavaDoc[] tab = (Object JavaDoc[]) obj;
297
298     output.append("(");
299     int size = tab.length;
300     output.append(size);
301     if (listMax == -1 || size <= listMax) {
302       for (int i = 0; i < size; i ++) {
303     output.append(",");
304     toString(output, tab[i]);
305       }
306     } else {
307       int border = size / 2;
308       if (listBorder < border)
309     border = listBorder;
310       for (int i = 0; i < border; i ++) {
311     output.append(",");
312     toString(output, tab[i]);
313       }
314       output.append(",...");
315       for (int i = border; i > 0; i --) {
316     output.append(",");
317     toString(output, tab[size - i]);
318       }
319     }
320     output.append(")");
321   }
322
323   /**
324    * Provides a string representation of an array.
325    * Calls <code>toString(StringBuffer, Object, Class)</code>.
326    *
327    * @param output a buffer to print the object into
328    * @param tab the array to print
329    * @param type the type of the array components
330    */

331   public static final void toStringArray(StringBuffer JavaDoc output, Object JavaDoc tab) {
332     if (tab == null) {
333       output.append("null");
334       return;
335     }
336
337     Class JavaDoc type = tab.getClass();
338     if (! type.isArray()) {
339       toString(output, tab);
340       return;
341     }
342
343     toString(output, tab, type.getComponentType());
344   }
345
346   /**
347    * Provides a string representation of an array.
348    * Calls <code>toString(StringBuffer, Object, Class)</code>.
349    *
350    * @param tab the array to print
351    * @return a string representation of the array
352    */

353   public static final String JavaDoc toStringArray(Object JavaDoc tab) {
354     if (tab == null)
355       return "null";
356
357     Class JavaDoc type = tab.getClass();
358     if (! type.isArray())
359       return toString(tab);
360
361     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
362     toString(output, tab, type.getComponentType());
363     return output.toString();
364   }
365
366   /**
367    * Provides a string representation of an array of booleans.
368    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
369    *
370    * @param output a buffer to print the object into
371    * @param tab the array to print
372    */

373   public static final void toString(StringBuffer JavaDoc output, boolean[] tab) {
374     if (tab == null) {
375       output.append("null");
376       return;
377     }
378
379     output.append("(");
380     int size = tab.length;
381     output.append(size);
382     if (listMax == -1 || size <= listMax) {
383       for (int i = 0; i < size; i ++) {
384     output.append(",");
385     output.append(tab[i]);
386       }
387     } else {
388       int border = size / 2;
389       if (listBorder < border)
390     border = listBorder;
391       for (int i = 0; i < border; i ++) {
392     output.append(",");
393     output.append(tab[i]);
394       }
395       output.append(",...");
396       for (int i = border; i > 0; i --) {
397     output.append(",");
398     output.append(tab[size - i]);
399       }
400     }
401     output.append(")");
402   }
403
404   /**
405    * Provides a string representation of an array of bytes.
406    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
407    *
408    * @param output a buffer to print the object into
409    * @param tab the array to print
410    */

411   public static final void toString(StringBuffer JavaDoc output, byte[] tab) {
412     if (tab == null) {
413       output.append("null");
414       return;
415     }
416
417     output.append("(");
418     int size = tab.length;
419     output.append(size);
420     if (listMax == -1 || size <= listMax) {
421       for (int i = 0; i < size; i ++) {
422     output.append(",");
423     output.append(tab[i]);
424       }
425     } else {
426       int border = size / 2;
427       if (listBorder < border)
428     border = listBorder;
429       for (int i = 0; i < border; i ++) {
430     output.append(",");
431     output.append(tab[i]);
432       }
433       output.append(",...");
434       for (int i = border; i > 0; i --) {
435     output.append(",");
436     output.append(tab[size - i]);
437       }
438     }
439     output.append(")");
440   }
441
442   /**
443    * Provides a string representation of an array of chars.
444    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
445    *
446    * @param output a buffer to print the object into
447    * @param tab the array to print
448    */

449   public static final void toString(StringBuffer JavaDoc output, char[] tab) {
450     if (tab == null) {
451       output.append("null");
452       return;
453     }
454
455     output.append("(");
456     int size = tab.length;
457     output.append(size);
458     if (listMax == -1 || size <= listMax) {
459       for (int i = 0; i < size; i ++) {
460     output.append(",");
461     output.append(tab[i]);
462       }
463     } else {
464       int border = size / 2;
465       if (listBorder < border)
466     border = listBorder;
467       for (int i = 0; i < border; i ++) {
468     output.append(",");
469     output.append(tab[i]);
470       }
471       output.append(",...");
472       for (int i = border; i > 0; i --) {
473     output.append(",");
474     output.append(tab[size - i]);
475       }
476     }
477     output.append(")");
478   }
479
480   /**
481    * Provides a string representation of an array of shorts.
482    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
483    *
484    * @param output a buffer to print the object into
485    * @param tab the array to print
486    */

487   public static final void toString(StringBuffer JavaDoc output, short[] tab) {
488     if (tab == null) {
489       output.append("null");
490       return;
491     }
492
493     output.append("(");
494     int size = tab.length;
495     output.append(size);
496     if (listMax == -1 || size <= listMax) {
497       for (int i = 0; i < size; i ++) {
498     output.append(",");
499     output.append(tab[i]);
500       }
501     } else {
502       int border = size / 2;
503       if (listBorder < border)
504     border = listBorder;
505       for (int i = 0; i < border; i ++) {
506     output.append(",");
507     output.append(tab[i]);
508       }
509       output.append(",...");
510       for (int i = border; i > 0; i --) {
511     output.append(",");
512     output.append(tab[size - i]);
513       }
514     }
515     output.append(")");
516   }
517
518   /**
519    * Provides a string representation of an array of ints.
520    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
521    *
522    * @param output a buffer to print the object into
523    * @param tab the array to print
524    */

525   public static final void toString(StringBuffer JavaDoc output, int[] tab) {
526     if (tab == null) {
527       output.append("null");
528       return;
529     }
530
531     output.append("(");
532     int size = tab.length;
533     output.append(size);
534     if (listMax == -1 || size <= listMax) {
535       for (int i = 0; i < size; i ++) {
536     output.append(",");
537     output.append(tab[i]);
538       }
539     } else {
540       int border = size / 2;
541       if (listBorder < border)
542     border = listBorder;
543       for (int i = 0; i < border; i ++) {
544     output.append(",");
545     output.append(tab[i]);
546       }
547       output.append(",...");
548       for (int i = border; i > 0; i --) {
549     output.append(",");
550     output.append(tab[size - i]);
551       }
552     }
553     output.append(")");
554   }
555
556   /**
557    * Provides a string representation of an array of longs.
558    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
559    *
560    * @param output a buffer to print the object into
561    * @param tab the array to print
562    */

563   public static final void toString(StringBuffer JavaDoc output, long[] tab) {
564     if (tab == null) {
565       output.append("null");
566       return;
567     }
568
569     output.append("(");
570     int size = tab.length;
571     output.append(size);
572     if (listMax == -1 || size <= listMax) {
573       for (int i = 0; i < size; i ++) {
574     output.append(",");
575     output.append(tab[i]);
576       }
577     } else {
578       int border = size / 2;
579       if (listBorder < border)
580     border = listBorder;
581       for (int i = 0; i < border; i ++) {
582     output.append(",");
583     output.append(tab[i]);
584       }
585       output.append(",...");
586       for (int i = border; i > 0; i --) {
587     output.append(",");
588     output.append(tab[size - i]);
589       }
590     }
591     output.append(")");
592   }
593
594   /**
595    * Provides a string representation of an array of floats.
596    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
597    *
598    * @param output a buffer to print the object into
599    * @param tab the array to print
600    */

601   public static final void toString(StringBuffer JavaDoc output, float[] tab) {
602     if (tab == null) {
603       output.append("null");
604       return;
605     }
606
607     output.append("(");
608     int size = tab.length;
609     output.append(size);
610     if (listMax == -1 || size <= listMax) {
611       for (int i = 0; i < size; i ++) {
612     output.append(",");
613     output.append(tab[i]);
614       }
615     } else {
616       int border = size / 2;
617       if (listBorder < border)
618     border = listBorder;
619       for (int i = 0; i < border; i ++) {
620     output.append(",");
621     output.append(tab[i]);
622       }
623       output.append(",...");
624       for (int i = border; i > 0; i --) {
625     output.append(",");
626     output.append(tab[size - i]);
627       }
628     }
629     output.append(")");
630   }
631
632   /**
633    * Provides a string representation of an array of doubles.
634    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
635    *
636    * @param output a buffer to print the object into
637    * @param tab the array to print
638    */

639   public static final void toString(StringBuffer JavaDoc output, double[] tab) {
640     if (tab == null) {
641       output.append("null");
642       return;
643     }
644
645     output.append("(");
646     int size = tab.length;
647     output.append(size);
648     if (listMax == -1 || size <= listMax) {
649       for (int i = 0; i < size; i ++) {
650     output.append(",");
651     output.append(tab[i]);
652       }
653     } else {
654       int border = size / 2;
655       if (listBorder < border)
656     border = listBorder;
657       for (int i = 0; i < border; i ++) {
658     output.append(",");
659     output.append(tab[i]);
660       }
661       output.append(",...");
662       for (int i = border; i > 0; i --) {
663     output.append(",");
664     output.append(tab[size - i]);
665       }
666     }
667     output.append(")");
668   }
669
670   /**
671    * Provides a string representation of a list of objects.
672    * This includes Vectors.
673    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
674    *
675    * @param output a buffer to print the object into
676    * @param list the list of <code>Object</code> objects to print
677    */

678   public static final void toString(StringBuffer JavaDoc output, List list) {
679     if (list == null) {
680       output.append("null");
681       return;
682     }
683
684     output.append("(");
685     int size = list.size();
686     output.append(size);
687     if (listMax == -1 || size <= listMax || size <= (listBorder*2)) {
688       for (Iterator it = list.iterator(); it.hasNext();) {
689     output.append(",");
690     toString(output, it.next());
691       }
692     } else {
693       int border = size / 2;
694       if (listBorder < border)
695     border = listBorder;
696       for (int i = 0; i < border; i ++) {
697     output.append(",");
698     toString(output, list.get(i));
699       }
700       output.append(",...");
701       for (int i = border; i > 0; i --) {
702     output.append(",");
703     toString(output, list.get(size - i));
704       }
705     }
706     output.append(")");
707   }
708
709   /**
710    * Provides a string representation of a list of objects.
711    * Calls <code>toString(StringBuffer, ...)</code>.
712    *
713    * @param list the list of <code>Object</code> objects to print
714    * @return a string representation of the list
715    */

716   public static final String JavaDoc toString(List list) {
717     if (list == null)
718       return "null";
719
720     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
721     toString(output, list);
722     return output.toString();
723   }
724
725   /**
726    * Provides a string representation of an unordered Collection of objects.
727    * This includes HashSets.
728    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
729    *
730    * @param output a buffer to print the object into
731    * @param set the collection to print
732    */

733   public static final void toString(StringBuffer JavaDoc output, Collection set) {
734     if (set == null) {
735       output.append("null");
736       return;
737     }
738
739     output.append("(");
740     int size = set.size();
741     output.append(size);
742     if (listMax != -1 && size > listMax)
743       size = listBorder;
744     for (Iterator it = set.iterator(); size > 0; size --) {
745       output.append(",");
746       toString(output, it.next());
747     }
748     output.append(")");
749   }
750
751   /**
752    * Provides a string representation of an unordered Collection of objects.
753    * Calls <code>toString(StringBuffer, ...)</code>.
754    *
755    * @param list the collection to print
756    * @return a string representation of the list
757    */

758   public static final String JavaDoc toString(Collection set) {
759     if (set == null)
760       return "null";
761
762     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
763     toString(output, set);
764     return output.toString();
765   }
766
767   /**
768    * Provides a string representation of a Map.
769    * This includes HashTables.
770    * Uses the <code>listMax</code> and <code>listBorder</code> variables.
771    *
772    * @param output a buffer to print the object into
773    * @param map the map to print
774    */

775   public static final void toString(StringBuffer JavaDoc output, Map map) {
776     if (map == null) {
777       output.append("null");
778       return;
779     }
780
781     toString(output, map.values());
782   }
783
784   /**
785    * Provides a string representation of a Map.
786    * Calls <code>toString(StringBuffer, ...)</code>.
787    *
788    * @param map the map to print
789    * @return a string representation of the map
790    */

791   public static final String JavaDoc toString(Map map) {
792     if (map == null)
793       return "null";
794
795     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
796     toString(output, map);
797     return output.toString();
798   }
799
800   /**
801    * Provides a string representation of a Map entry.
802    *
803    * @param output a buffer to print the object into
804    * @param entry the map entry to print
805    */

806   public static final void toString(StringBuffer JavaDoc output, Map.Entry entry) {
807     if (entry == null) {
808       output.append("null");
809       return;
810     }
811     output.append("(");
812     output.append(entry.getKey());
813     output.append(",");
814     output.append(entry.getValue());
815     output.append(")");
816   }
817
818 }
819
Popular Tags