001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util.internal;
020:
021: import java.io.File;
022:
023: public class FileUtils {
024: private static final boolean OS_CASE_SENSITIVE = !new File("x")
025: .equals(new File("X"));
026:
027: /**
028: * Tell whether a given URI is absolute, i.e., whether it contains a scheme-part (e.g., "http:").
029: *
030: * @param uri the URI to test.
031: * @return <code>true</code> if the given URI is absolute.
032: */
033: public static boolean isAbsoluteURI(String uri) {
034: //
035: // This method needs to be fast, so it can't use java.net.URI.
036: //
037: if (uri.length() == 0 || uri.charAt(0) == '/')
038: return false;
039:
040: for (int i = 0, len = uri.length(); i < len; ++i) {
041: char c = uri.charAt(i);
042:
043: if (c == ':') {
044: return true;
045: } else if (c == '/') {
046: return false;
047: }
048: }
049:
050: return false;
051: }
052:
053: /**
054: * Tell whether a URI ends in a given String.
055: */
056: public static boolean uriEndsWith(String uri, String ending) {
057: int queryStart = uri.indexOf('?');
058:
059: if (queryStart == -1) {
060: return uri.endsWith(ending);
061: } else {
062: return uri.length() - queryStart >= ending.length()
063: && uri.substring(queryStart - ending.length(),
064: queryStart).equals(ending);
065: }
066: }
067:
068: /**
069: * Get the file extension from a file name.
070: *
071: * @param filename the file name.
072: * @return the file extension (everything after the last '.'), or the empty string if there is no
073: * file extension.
074: */
075: public static String getFileExtension(String filename) {
076: int lastDot = filename.lastIndexOf('.');
077: return lastDot != -1 ? filename.substring(lastDot + 1) : "";
078: }
079:
080: public static String stripFileExtension(String filename) {
081: int lastDot = filename.lastIndexOf('.');
082: return lastDot != -1 ? filename.substring(0, lastDot)
083: : filename;
084: }
085:
086: /**
087: * Tell whether the current operating system is case-sensitive with regard to file names.
088: */
089: public static boolean isOSCaseSensitive() {
090: return OS_CASE_SENSITIVE;
091: }
092:
093: /**
094: * Compare two strings, with case sensitivity determined by the operating system.
095: *
096: * @param s1 the first String to compare.
097: * @param s2 the second String to compare.
098: * @return <code>true</code> when:
099: * <ul>
100: * <li>the strings match exactly (including case), or,</li>
101: * <li>the operating system is not case-sensitive with regard to file names, and the strings match,
102: * ignoring case.</li>
103: * </ul>
104: * @see #isOSCaseSensitive()
105: */
106: public static boolean osSensitiveEquals(String s1, String s2) {
107: if (OS_CASE_SENSITIVE) {
108: return s1.equals(s2);
109: } else {
110: return s1.equalsIgnoreCase(s2);
111: }
112: }
113:
114: /**
115: * Tell whether a string ends with a particular suffix, with case sensitivity determined by the operating system.
116: *
117: * @param str the String to test.
118: * @param suffix the suffix to look for.
119: * @return <code>true</code> when:
120: * <ul>
121: * <li><code>str</code> ends with <code>suffix</code>, or,</li>
122: * <li>the operating system is not case-sensitive with regard to file names, and <code>str</code> ends with
123: * <code>suffix</code>, ignoring case.</li>
124: * </ul>
125: * @see #isOSCaseSensitive()
126: */
127: public static boolean osSensitiveEndsWith(String str, String suffix) {
128: if (OS_CASE_SENSITIVE) {
129: return str.endsWith(suffix);
130: } else {
131: int strLen = str.length();
132: int suffixLen = suffix.length();
133:
134: if (strLen < suffixLen) {
135: return false;
136: }
137:
138: return (str.substring(strLen - suffixLen)
139: .equalsIgnoreCase(suffix));
140: }
141: }
142: }
|