2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.img;
20 * Utilities for path, file name and extension manipulation.
22 * @author Dmitry Samoshin aka gotty
24 public final class FilenameUtils {
26 private static final String EMPTY_STRING = "";
28 private static final char EXTENSION_SEPARATOR = '.';
30 private static final char UNIX_SEPARATOR = '/';
31 private static final char WINDOWS_SEPARATOR = '\\';
33 private static final String PATH_COLON = ":";
35 private static final String JAR_SEPARATOR = ".jar!";
37 private static final String OS = System.getProperty("os.name").toLowerCase();
39 public static String getName(final String fileName) {
40 if (fileName == null) {
43 final int endPathIndex = Math.max(
44 fileName.lastIndexOf(UNIX_SEPARATOR),
45 fileName.lastIndexOf(WINDOWS_SEPARATOR)
47 return fileName.substring(endPathIndex + 1);
50 public static String getBaseName(final String fileName) {
51 return removeExtension(
56 public static String getExtension(final String fileName) {
57 if (fileName == null) {
60 final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
61 return extensionPos < 0
63 : fileName.substring(extensionPos + 1);
66 public static String getFullPath(final String parentDir, final String... childDirs) {
67 StringBuilder sb = new StringBuilder();
69 if (childDirs.length > 0) {
70 for (String childDir : childDirs) {
71 sb.append(isWindows() ? WINDOWS_SEPARATOR : UNIX_SEPARATOR).append(childDir);
77 public static String fixPath(final String path) {
79 return path.startsWith(String.valueOf(UNIX_SEPARATOR))
80 && path.contains(PATH_COLON)
87 public static String getPathFromJar(final String jarPath) {
88 final int jarEndPos = jarPath.lastIndexOf(JAR_SEPARATOR);
91 : jarPath.substring(jarEndPos + JAR_SEPARATOR.length());
94 private static String removeExtension(final String fileName) {
95 if (fileName == null) {
98 final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
99 return extensionPos < 0
101 : fileName.substring(0, extensionPos);
104 private static boolean isWindows() {
105 return (OS.contains("win"));
108 private FilenameUtils() {
109 throw new AssertionError(
110 "No org.hedgecode.chess.img.FilenameUtils instances!"