KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > FileUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal;
19
20 import java.io.File JavaDoc;
21
22 public class FileUtils
23 {
24     private static final boolean OS_CASE_SENSITIVE = ! new File JavaDoc( "x" ).equals( new File JavaDoc( "X" ) );
25
26
27     /**
28      * Tell whether a given URI is absolute, i.e., whether it contains a scheme-part (e.g., "http:").
29      *
30      * @param uri the URI to test.
31      * @return <code>true</code> if the given URI is absolute.
32      */

33     public static boolean isAbsoluteURI( String JavaDoc uri )
34     {
35         //
36
// This method needs to be fast, so it can't use java.net.URI.
37
//
38
if ( uri.length() == 0 || uri.charAt( 0 ) == '/' ) return false;
39         
40         for ( int i = 0, len = uri.length(); i < len; ++i )
41         {
42             char c = uri.charAt( i );
43             
44             if ( c == ':' )
45             {
46                 return true;
47             }
48             else if ( c == '/' )
49             {
50                 return false;
51             }
52         }
53         
54         return false;
55     }
56     
57     /**
58      * Tell whether a URI ends in a given String.
59      */

60     public static boolean uriEndsWith( String JavaDoc uri, String JavaDoc ending )
61     {
62         int queryStart = uri.indexOf( '?' );
63         
64         if ( queryStart == -1 )
65         {
66             return uri.endsWith( ending );
67         }
68         else
69         {
70             return uri.length() - queryStart >= ending.length()
71                    && uri.substring( queryStart - ending.length(), queryStart ).equals( ending );
72         }
73     }
74     
75     /**
76      * Get the file extension from a file name.
77      *
78      * @param filename the file name.
79      * @return the file extension (everything after the last '.'), or the empty string if there is no
80      * file extension.
81      */

82     public static String JavaDoc getFileExtension( String JavaDoc filename )
83     {
84         int lastDot = filename.lastIndexOf( '.' );
85         return lastDot != -1 ? filename.substring( lastDot + 1 ) : "";
86     }
87     
88     public static String JavaDoc stripFileExtension( String JavaDoc filename )
89     {
90         int lastDot = filename.lastIndexOf( '.' );
91         return lastDot != -1 ? filename.substring( 0, lastDot ) : filename;
92     }
93
94     /**
95      * Tell whether the current operating system is case-sensitive with regard to file names.
96      */

97     public static boolean isOSCaseSensitive()
98     {
99         return OS_CASE_SENSITIVE;
100     }
101     
102     /**
103      * Compare two strings, with case sensitivity determined by the operating system.
104      *
105      * @param s1 the first String to compare.
106      * @param s2 the second String to compare.
107      * @return <code>true</code> when:
108      * <ul>
109      * <li>the strings match exactly (including case), or,</li>
110      * <li>the operating system is not case-sensitive with regard to file names, and the strings match,
111      * ignoring case.</li>
112      * </ul>
113      * @see #isOSCaseSensitive()
114      */

115     public static boolean osSensitiveEquals( String JavaDoc s1, String JavaDoc s2 )
116     {
117         if ( OS_CASE_SENSITIVE )
118         {
119             return s1.equals( s2 );
120         }
121         else
122         {
123             return s1.equalsIgnoreCase( s2 );
124         }
125     }
126     
127     /**
128      * Tell whether a string ends with a particular suffix, with case sensitivity determined by the operating system.
129      *
130      * @param str the String to test.
131      * @param suffix the suffix to look for.
132      * @return <code>true</code> when:
133      * <ul>
134      * <li><code>str</code> ends with <code>suffix</code>, or,</li>
135      * <li>the operating system is not case-sensitive with regard to file names, and <code>str</code> ends with
136      * <code>suffix</code>, ignoring case.</li>
137      * </ul>
138      * @see #isOSCaseSensitive()
139      */

140     public static boolean osSensitiveEndsWith( String JavaDoc str, String JavaDoc suffix )
141     {
142         if ( OS_CASE_SENSITIVE )
143         {
144             return str.endsWith( suffix );
145         }
146         else
147         {
148             int strLen = str.length();
149             int suffixLen = suffix.length();
150             
151             if ( strLen < suffixLen )
152             {
153                 return false;
154             }
155             
156             return ( str.substring( strLen - suffixLen ).equalsIgnoreCase( suffix ) );
157         }
158     }
159 }
160
161
Popular Tags