001: /**************************************************************************/
002: /* N I C E */
003: /* A simple imperative object-oriented research language */
004: /* (c) Daniel Bonniot 1999 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.util;
012:
013: import java.io.File;
014: import java.util.jar.JarFile;
015:
016: import java.util.Date;
017: import java.text.DateFormat;
018:
019: import java.util.List;
020: import java.util.ArrayList;
021:
022: /**
023: Communication with the system environment.
024:
025: @version $Date: 2004/02/28 14:23:42 $
026: @author Daniel Bonniot
027: */
028:
029: public class System {
030: /**
031: Return a string to nicely display the file.
032:
033: If the file lies in the user home directory
034: (as indicated by the <tt>user.home</tt> system property),
035: replace this prefix with "~".
036:
037: Eg:
038: /udir/bonniot/Nice/stdlib/nice/lang/package.nicei
039: is pretty printed as:
040: ~/Nice/stdlib/nice/lang/package.nicei
041: */
042: public static String prettyPrint(File f) {
043: return prettyPrintFile(f.toString());
044: }
045:
046: /** @see #prettyPrint(java.io.File) */
047: public static String prettyPrint(JarFile f) {
048: return prettyPrintFile(f.getName());
049: }
050:
051: public static String prettyPrintFile(String name) {
052: if (name != null && name.startsWith(home))
053: return "~" + name.substring(homeLength);
054: else
055: return name;
056: }
057:
058: /**
059: Return a file from a string describing it.
060: Performs ~ expansion (~ -> user.home property).
061: */
062: public static File getFile(String file) {
063: if (file.charAt(0) == '~')
064: file = home + file.substring(1);
065:
066: return new File(file);
067: }
068:
069: private static final String home = java.lang.System
070: .getProperty("user.home");
071: private static final int homeLength = home.length();
072:
073: /**
074: Format dates.
075: */
076:
077: public static String date(long date) {
078: return longDate.format(new Date(date));
079: }
080:
081: private static final DateFormat longDate = DateFormat
082: .getDateTimeInstance();
083:
084: /**
085: String operations.
086: */
087:
088: public static String[] split(String str, char separator) {
089: List res = new ArrayList();
090: int ntx = 0;
091: int pos = 0;
092: while (ntx > -1) {
093: ntx = str.indexOf(separator, pos);
094: if (ntx > -1) {
095: if (ntx > 0) {
096: res.add(str.substring(pos, ntx));
097: }
098: pos = ntx + 1;
099: } else {
100: res.add(pos == 0 ? str : str.substring(pos));
101: }
102: }
103:
104: return (String[]) res.toArray(new String[res.size()]);
105: }
106: }
|