1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.util;
25
26 import java.awt.*;
27 import java.util.*;
28 import java.io.*;
29
30 /***
31 * Misc is used to do "things" to messages
32 * Simon updated escape/unescape to allow serialised objects to be sent between agents
33 * since 1.2.1
34 */
35 public class Misc {
36 static final String DELIMITERS = "\n\r\t !%^&*()-+={[]}~,.:;#|//";
37 public static final String OPAQUE_CHAR = "#";
38 public static final String QUOTE = "\"";
39
40
41 public static final String literalToString(String s) {
42 if ( s.startsWith(QUOTE) && s.endsWith(QUOTE) )
43 return unescape(s.substring(1,s.length()-1));
44 else
45 return unescape(s);
46 }
47
48
49 public static final String opaqueToString(String s) {
50 return s.substring(1,s.length()-1);
51 }
52
53
54 public static final String escape(String str) {
55 String retval = new String();
56 char ch;
57 for (int i = 0; i < str.length(); i++) {
58
59 switch (str.charAt(i)) {
60
61 case 0 :
62 debug ("0 !");
63 retval+=((char) 0);
64 continue;
65 case '\b':
66 retval+=("//b");
67 continue;
68 case '\t':
69 retval+=("//t");
70 continue;
71 case '\n':
72 retval+=("//n");
73 continue;
74 case '\f':
75 retval+=("//f");
76 continue;
77 case '\r':
78 retval+=("//r");
79 continue;
80 case '\"':
81 retval+=("//\"");
82 continue;
83 case '\'':
84 retval+=("//\'");
85 continue;
86 case '//':
87 retval+=("////");
88 continue;
89
90
91
92
93 default:
94 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
95 String s = new String();
96 s+=("0000");
97 s+=Integer.toString(ch, 16);
98 retval+=("//u");
99 retval+=(s.substring(s.length() - 4, s.length()));
100 } else {
101 retval+=(ch);
102 }
103 continue;
104 }
105 }
106 return retval;
107 }
108
109 public static final String unescape(String str) {
110 String retval = new String();
111 char ch;
112 for(int i = 0; i < str.length(); ) {
113 ch = str.charAt(i++);
114 if ( ch != '//' )
115 retval+=(ch);
116 else {
117 ch = str.charAt(i++);
118 switch(ch) {
119 case 0 :
120 continue;
121 case 'b':
122 retval+=("\b");
123 continue;
124 case 't':
125 retval+=("\t");
126 continue;
127 case 'n':
128 retval+=("\n");
129 continue;
130 case 'f':
131 retval+=("\f");
132 continue;
133 case 'r':
134 retval+=("\r");
135 continue;
136 case '"':
137 retval+=("\"");
138 continue;
139 case '\'':
140 retval+=("\'");
141 continue;
142 case '//':
143 retval+=("//");
144 continue;
145 case ' ':
146 retval+= (" ");
147 continue;
148 case 'u' :
149 String thisChar = str.substring (i,i+4);
150 debug ("thisChar = " + thisChar);
151 i = i +4;
152 int val = Integer.valueOf(thisChar,16).intValue();
153 debug ("val = " + String.valueOf(val));
154 char appender = (char) val;
155 debug ("appender = " + appender);
156 retval += (appender);
157
158
159
160
161
162 continue;
163 default:
164
165 retval+=("//");
166 retval+=(ch);
167 continue;
168 }
169 }
170 }
171
172 return retval;
173
174 }
175
176
177 public static final String relativePath(String file) {
178 if ( file == null ) return null;
179 return relativePath(System.getProperty("user.dir"),file);
180 }
181
182
183 public static final String relativePath(String dir, String file) {
184 if ( file == null ) return null;
185 return relativePath(dir,new File(file));
186 }
187
188
189 public static final String relativePath(String dir, File file) {
190 if ( file == null ) return null;
191
192 if ( dir == null )
193 dir = System.getProperty("user.dir");
194
195 File d = new File(dir);
196 try {
197 dir = d.getCanonicalPath();
198 }
199 catch(IOException e) {
200 dir = d.getAbsolutePath();
201 }
202
203 String filename;
204 try {
205 filename = file.getCanonicalPath();
206 }
207 catch(IOException e) {
208 filename = file.getAbsolutePath();
209 }
210 File f;
211
212 int count = 0;
213 while( dir != null ) {
214 if ( filename.startsWith(dir) && !dir.equals(File.separator) ) {
215 String relpath = "";
216 for(int i = 0; i < count; i++ ) {
217 relpath += "..";
218 if ( i+1 < count )
219 relpath += File.separator;
220 }
221 relpath += filename.substring(dir.length());
222 if ( relpath.startsWith(File.separator) )
223 return "." + relpath;
224 else if ( relpath.equals("") )
225 return ".";
226 else
227 return relpath;
228 }
229 count++;
230 f = new File(dir);
231 dir = f.getParent();
232 }
233 if ( filename.equals("") )
234 return ".";
235 else
236 return filename;
237 }
238
239 public static final String spaces(int sp ) {
240 String tabs = "";
241 for( int i = 0; i < sp; i++ ) tabs += " ";
242 return tabs;
243 }
244
245
246 public static final String concat(String prefix, int[] data) {
247 for(int i = 0; data != null && i < data.length; i++ )
248 prefix += " " + data[i];
249 return prefix;
250 }
251
252
253 public static final String concat(int[] data) {
254 String prefix = "";
255 for( int i = 0; data != null && i < data.length; i++ )
256 prefix += data[i] + " ";
257 prefix = prefix.trim();
258 return prefix;
259 }
260
261
262 public static final String concat(String prefix, double[] data) {
263 for(int i = 0; data != null && i < data.length; i++ )
264 prefix += " " + data[i];
265 return prefix;
266 }
267
268
269 public static final String concat(double[] data) {
270 String prefix = "";
271 for( int i = 0; data != null && i < data.length; i++ )
272 prefix += data[i] + " ";
273 return prefix.trim();
274 }
275
276
277 public static final String concat(String prefix, Object[] data) {
278 for(int i = 0; data != null && i < data.length; i++ )
279 prefix += " " + data[i];
280 return prefix;
281 }
282
283
284 public static final String concat(Object[] data) {
285 String prefix = "";
286 for( int i = 0; data != null && i < data.length; i++ )
287 prefix += data[i] + " ";
288 prefix = prefix.trim();
289 return prefix;
290 }
291
292
293 public static final String concat(String prefix, Vector data) {
294 for(int i = 0; data != null && i < data.size(); i++ )
295 prefix += " " + data.elementAt(i);
296 return prefix;
297 }
298
299
300 public static final String concat(Vector data) {
301 String prefix = new String ();
302 for( int i = 0; data != null && i < data.size(); i++ )
303 prefix += data.elementAt(i) + " ";
304 prefix = prefix.trim();
305 return prefix;
306 }
307
308
309 public static final String concat(HSet data) {
310 String prefix = new String();
311 Enumeration enum = data.elements();
312 while( enum.hasMoreElements() )
313 prefix += enum.nextElement() + " ";
314 prefix= prefix.trim();
315 return prefix;
316 }
317
318 public static final String substitute(String in, String search, String replace) {
319 StringTokenizer st = new StringTokenizer(in,DELIMITERS,true);
320 String token;
321 String s = "";
322 while( st.hasMoreTokens() ) {
323 token = st.nextToken();
324 if ( token.equals(search) )
325 s += replace;
326 else
327 s += token;
328 }
329 return s;
330 }
331
332
333 public static final boolean member(int item, int[] List) {
334 if ( List == null ) return false;
335 for(int j = 0; j < List.length; j++ )
336 if ( item == List[j] ) return true;
337 return false;
338 }
339
340
341 public static final boolean member(String item, String[] List) {
342 if ( List == null ) return false;
343 for(int j = 0; j < List.length; j++ )
344 if ( item.equals(List[j]) ) return true;
345 return false;
346 }
347
348
349 public static final Vector intersection(Vector left, Vector right) {
350 Vector result = new Vector();
351 Object item;
352 for(int j = 0; j < left.size(); j++ ) {
353 item = left.elementAt(j);
354 if ( !result.contains(item) && right.contains(item) )
355 result.addElement(item);
356 }
357 return result;
358 }
359
360
361 public static final Vector union(Vector left, Vector right) {
362 Vector result = new Vector();
363 Object item;
364 for(int j = 0; j < left.size(); j++ ) {
365 item = left.elementAt(j);
366 if ( !result.contains(item) )
367 result.addElement(item);
368 }
369 for(int j = 0; j < right.size(); j++ ) {
370 item = right.elementAt(j);
371 if ( !result.contains(item) )
372 result.addElement(item);
373 }
374 return result;
375 }
376
377
378 public static final Vector difference(Vector left, Vector right) {
379 Vector result = new Vector();
380 Object item;
381 for(int j = 0; j < left.size(); j++ ) {
382 item = left.elementAt(j);
383 if ( !result.contains(item) && !right.contains(item) )
384 result.addElement(item);
385 }
386 return result;
387 }
388
389
390 public static final boolean isSubset(Vector subset, Vector superset) {
391 Object item;
392 for(int j = 0; j < subset.size(); j++ ) {
393 item = subset.elementAt(j);
394 if ( !superset.contains(item) )
395 return false;
396 }
397 return true;
398 }
399
400 public static final boolean sameVector(Vector left, Vector right) {
401 if ( left.size() != right.size() ) return false;
402
403 Vector diff = difference(left,right);
404 if ( !diff.isEmpty() ) return false;
405
406 diff = difference(right,left);
407 return diff.isEmpty();
408 }
409
410
411 public static final boolean isNumber(String s) {
412 return isLong(s) || isDouble(s);
413 }
414
415
416 public static final boolean isLong(String s) {
417 try {
418 long x = Long.parseLong(s);
419 return true;
420 }
421 catch(NumberFormatException e) {
422 return false;
423 }
424 }
425
426
427 public static final boolean isDouble(String s) {
428 try {
429 double x = Double.parseDouble(s);
430 return true;
431 }
432 catch(NumberFormatException e) {
433 return false;
434 }
435 }
436
437
438 public static final void sort(String[] data) {
439 if ( data == null ) return;
440 boolean swapped = true;
441 while( swapped ) {
442 swapped = false;
443 for( int i = 0; i < data.length-1; i++ )
444 if ( data[i].compareTo(data[i+1]) > 0 ) {
445 String tmp = data[i];
446 data[i] = data[i+1];
447 data[i+1] = tmp;
448 swapped = true;
449 }
450 }
451 }
452
453
454 public static final void sort(Vector data) {
455 if ( data == null ) return;
456 if ( data.isEmpty() ) return;
457 if ( !( data.elementAt(0) instanceof String) ) return;
458
459 boolean swapped = true;
460 while( swapped ) {
461 swapped = false;
462 for( int i = 0; i < data.size()-1; i++ )
463 if ( ((String)data.elementAt(i)).compareTo(
464 (String)data.elementAt(i+1)) > 0 ) {
465 Object tmp = data.elementAt(i);
466 data.setElementAt(data.elementAt(i+1),i);
467 data.setElementAt(tmp,i+1);
468 swapped = true;
469 }
470 }
471 }
472
473
474 public static final int whichPosition(String data, String[] items) {
475 if ( data != null && items != null )
476 for( int i = 0; i < items.length; i++ ) {
477 if ( data.equals(items[i]) ) return i;
478 }
479 return -1;
480 }
481
482
483 public static final int whichPosition(String data, Vector items) {
484 if ( data != null && items != null )
485 for( int i = 0; i < items.size(); i++ ) {
486 if ( data.equals((String)items.elementAt(i)) ) return i;
487 }
488 return -1;
489 }
490
491
492 public static final Vector flatten(Vector data) {
493 Vector result = new Vector();
494 if ( data == null || data.isEmpty() ) return result;
495 Object obj;
496 for( int i = 0; i < data.size(); i++ ) {
497 obj = data.elementAt(i);
498 if ( obj instanceof Vector ) {
499 Vector v = flatten((Vector)obj);
500 for( int j = 0; j < v.size(); j++ )
501 result.addElement(v.elementAt(j));
502 }
503 else
504 result.addElement(obj);
505 }
506 return result;
507 }
508
509
510 public static final Vector copyVector(Vector data) {
511 if ( data == null ) return null;
512 Vector result = new Vector();
513 if ( data.isEmpty() ) return result;
514 Object obj;
515 for( int i = 0; i < data.size(); i++ ) {
516 obj = data.elementAt(i);
517 if ( obj instanceof Vector ) {
518 Vector v = copyVector((Vector)obj);
519 result.addElement(v);
520 }
521 else
522 result.addElement(obj);
523 }
524 return result;
525 }
526
527
528 public static final String[] stringArray(String s) {
529 StringTokenizer st = new StringTokenizer(s);
530 Vector data = new Vector();
531 while( st.hasMoreTokens() )
532 data.addElement(st.nextToken());
533 return stringArray(data);
534 }
535
536
537 public static final String[] stringArray(Object[] data) {
538 if ( data == null ) return new String[0];
539 String[] result = new String[data.length];
540 for( int i = 0; i < data.length; i++ )
541 result[i] = data[i].toString();
542 return result;
543 }
544
545
546 public static final String[] stringArray(Vector data) {
547 if ( data == null ) return new String[0];
548 String[] result = new String[data.size()];
549 for( int i = 0; i < data.size(); i++ )
550 result[i] = (String)data.elementAt(i);
551 return result;
552 }
553
554
555 public static final Vector stringVector(String[] data) {
556 Vector result = new Vector();
557 if ( data != null )
558 for( int i = 0; i < data.length; i++ )
559 result.addElement(data[i]);
560 return result;
561 }
562
563
564 public static final String decimalPlaces(double x, int num) {
565 return decimalPlaces(Double.toString(x),num);
566 }
567
568
569 public static final String decimalPlaces(String str,int num) {
570 String exp = "";
571 int index = str.indexOf("E");
572 if ( index != -1 ) {
573 exp = str.substring(index);
574 }
575 index = str.indexOf(".");
576 if ( index == -1 ) {
577 String cstr = str + ".";
578 for( int i = 0; i < num; i++ )
579 cstr += "0";
580 return cstr+exp;
581 }
582 else {
583 int len = str.length();
584 if ( len > index + num )
585 return str.substring(0,index+num+1)+exp;
586 else {
587 String cstr = str;
588 for( int i = 0; i < num+index+1-len; i++ )
589 cstr += "0";
590 return cstr+exp;
591 }
592 }
593 }
594
595
596 /***
597 * Serialize object to a String.
598 * This can be used to transform an object into a string that can then be sent
599 * as content in a performative message. Note: the string must be turned back into
600 * an object using zeus.util.Misc.contentToObject(String)
601 * modified by Simon. Static since 1.2.2
602 * @author John Shepherdson
603 * @date 04/05/2001.
604 * @param The object to be serialised (must me serialisable!)
605 * @return String containing the agent.
606 * @exception IOException
607 * @see java.io.ByteArrayOutputStream, java.io.ObjectOutputStream
608 * @since 1.2.1
609 */
610 public static final String objectToContent (Serializable serialised) throws java.io.IOException {
611 ByteArrayOutputStream out = new ByteArrayOutputStream();
612 ObjectOutputStream stream = new ObjectOutputStream(out);
613 stream.writeObject(serialised);
614 stream.flush();
615 stream.close ();
616 String retVal = out.toString();
617 retVal = escape(retVal);
618 return retVal;
619 }
620
621
622
623 /***
624 * De-serialize from a String that contains an escaped serialised object to a reference to
625 * the object. Note, you must have a copy of the class that this is an instance of in
626 * your path, or you will get an exception.
627 * Modified by Simon to return an Object
628 *
629 * @author John Shepherdson
630 * @date 04/05/2001.
631 * @param String containing the agent.
632 * @return Instance of Object
633 * @exception ClassNotFoundException, StreamCorruptedException, IOException
634 * @see java.io.ByteArrayInputStream, java.io.ObjectInputStream
635 * @since 1.2.1
636 */
637 public static final Object contentToObject (String contentString)
638 throws java.lang.ClassNotFoundException,
639 java.io.StreamCorruptedException, java.io.IOException
640 {
641 contentString = unescape(contentString);
642 byte [] buf = contentString.getBytes();
643 ByteArrayInputStream in = new ByteArrayInputStream(buf);
644 ObjectInputStream stream = new ObjectInputStream(in);
645 Object agent = stream.readObject();
646 return agent;
647 }
648
649
650 public static final void debug (String str) {
651
652 }
653
654
655 public static final boolean isZero(double x) {
656 return Math.abs(x) < 1.0E-12;
657 }
658 }