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 private static final String WIN_OS_TEMPLATE = "win";
41 public static String getName(final String fileName) {
42 if (fileName == null) {
45 final int endPathIndex = Math.max(
46 fileName.lastIndexOf(UNIX_SEPARATOR),
47 fileName.lastIndexOf(WINDOWS_SEPARATOR)
49 return fileName.substring(endPathIndex + 1);
52 public static String getBaseName(final String fileName) {
53 return removeExtension(
58 public static String getExtension(final String fileName) {
59 if (fileName == null) {
62 final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
63 return extensionPos < 0
65 : fileName.substring(extensionPos + 1);
68 public static String getFullPath(final String parentDir, final String... childDirs) {
69 StringBuilder sb = new StringBuilder();
71 if (childDirs.length > 0) {
72 for (String childDir : childDirs) {
73 sb.append(isWindows() ? WINDOWS_SEPARATOR : UNIX_SEPARATOR).append(childDir);
79 public static String fixPath(final String path) {
81 return path.startsWith(String.valueOf(UNIX_SEPARATOR))
82 && path.contains(PATH_COLON)
89 public static String getPathFromJar(final String jarPath) {
90 final int jarEndPos = jarPath.lastIndexOf(JAR_SEPARATOR);
93 : jarPath.substring(jarEndPos + JAR_SEPARATOR.length());
96 private static String removeExtension(final String fileName) {
97 if (fileName == null) {
100 final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
101 return extensionPos < 0
103 : fileName.substring(0, extensionPos);
106 private static boolean isWindows() {
107 return (OS.contains(WIN_OS_TEMPLATE));
110 private FilenameUtils() {
111 throw new AssertionError(
112 "No org.hedgecode.chess.img.FilenameUtils instances!"