KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > Utilities


1 /*
2  * Utilities.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: Utilities.java,v 1.8 2004/01/26 00:30:12 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 public final class Utilities extends Lisp
28 {
29     private static final boolean isPlatformUnix;
30     private static final boolean isPlatformWindows;
31
32     static {
33         String JavaDoc osName = System.getProperty("os.name");
34         isPlatformUnix = osName.startsWith("Linux") ||
35             osName.startsWith("Mac OS X") || osName.startsWith("Solaris") ||
36             osName.startsWith("SunOS") || osName.startsWith("AIX");
37         isPlatformWindows = osName.startsWith("Windows");
38     }
39
40     public static boolean isPlatformUnix()
41     {
42         return isPlatformUnix;
43     }
44
45     public static boolean isPlatformWindows()
46     {
47         return isPlatformWindows;
48     }
49
50     public static boolean isFilenameAbsolute(String JavaDoc filename)
51     {
52         final int length = filename.length();
53         if (length > 0) {
54             char c0 = filename.charAt(0);
55             if (c0 == '\\' || c0 == '/')
56                 return true;
57             if (length > 2) {
58                 if (isPlatformWindows()) {
59                     // Check for drive letter.
60
char c1 = filename.charAt(1);
61                     if (c1 == ':') {
62                         if (c0 >= 'a' && c0 <= 'z')
63                             return true;
64                         if (c0 >= 'A' && c0 <= 'Z')
65                             return true;
66                     }
67                 } else {
68                     // Unix.
69
if (filename.equals("~") || filename.startsWith("~/"))
70                         return true;
71                 }
72             }
73         }
74         return false;
75     }
76
77     public static File JavaDoc getFile(Pathname pathname) throws ConditionThrowable
78     {
79         Pathname defaultPathname =
80             Pathname.coerceToPathname(_DEFAULT_PATHNAME_DEFAULTS_.symbolValue());
81         Pathname merged =
82             Pathname.mergePathnames(pathname, defaultPathname, NIL);
83         String JavaDoc namestring = merged.getNamestring();
84         if (namestring != null)
85             return new File JavaDoc(namestring);
86         signal(new SimpleError("Pathname has no namestring: " + merged));
87         // Not reached.
88
return null;
89     }
90
91     public static Pathname getDirectoryPathname(File JavaDoc file)
92         throws ConditionThrowable
93     {
94         try {
95             String JavaDoc namestring = file.getCanonicalPath();
96             if (namestring != null && namestring.length() > 0) {
97                 if (namestring.charAt(namestring.length() - 1) != File.separatorChar)
98                     namestring = namestring.concat(File.separator);
99             }
100             return new Pathname(namestring);
101         }
102         catch (IOException JavaDoc e) {
103             signal(new LispError(e.getMessage()));
104             // Not reached.
105
return null;
106         }
107     }
108
109     public static final char toUpperCase(char c)
110     {
111         if (c < 128)
112             return UPPER_CASE_CHARS[c];
113         return Character.toUpperCase(c);
114     }
115
116     private static final char[] UPPER_CASE_CHARS = new char[128];
117
118     static {
119         for (int i = UPPER_CASE_CHARS.length; i-- > 0;)
120             UPPER_CASE_CHARS[i] = Character.toUpperCase((char)i);
121     }
122
123     public static final char toLowerCase(char c)
124     {
125         if (c < 128)
126             return LOWER_CASE_CHARS[c];
127         return Character.toLowerCase(c);
128     }
129
130     private static final char[] LOWER_CASE_CHARS = new char[128];
131
132     static {
133         for (int i = LOWER_CASE_CHARS.length; i-- > 0;)
134             LOWER_CASE_CHARS[i] = Character.toLowerCase((char)i);
135     }
136 }
137
Popular Tags