[LIB-9] Separate chesshog-graphics module
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / FilenameUtils.java
diff --git a/chesshog-graphics/src/main/java/org/hedgecode/chess/img/FilenameUtils.java b/chesshog-graphics/src/main/java/org/hedgecode/chess/img/FilenameUtils.java
new file mode 100644 (file)
index 0000000..4b6b773
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2018-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.img;
+
+/**
+ * Utilities for path, file name and extension manipulation.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class FilenameUtils {
+
+    private static final String EMPTY_STRING = "";
+
+    private static final char EXTENSION_SEPARATOR = '.';
+
+    private static final char UNIX_SEPARATOR = '/';
+    private static final char WINDOWS_SEPARATOR = '\\';
+
+    private static final String PATH_COLON = ":";
+
+    private static final String JAR_SEPARATOR = ".jar!";
+
+    private static final String OS = System.getProperty("os.name").toLowerCase();
+
+    public static String getName(final String fileName) {
+        if (fileName == null) {
+            return null;
+        }
+        final int endPathIndex = Math.max(
+                fileName.lastIndexOf(UNIX_SEPARATOR),
+                fileName.lastIndexOf(WINDOWS_SEPARATOR)
+        );
+        return fileName.substring(endPathIndex + 1);
+    }
+
+    public static String getBaseName(final String fileName) {
+        return removeExtension(
+                getName(fileName)
+        );
+    }
+
+    public static String getExtension(final String fileName) {
+        if (fileName == null) {
+            return null;
+        }
+        final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
+        return extensionPos < 0
+                ? EMPTY_STRING
+                : fileName.substring(extensionPos + 1);
+    }
+
+    public static String getFullPath(final String parentDir, final String... childDirs) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(parentDir);
+        if (childDirs.length > 0) {
+            for (String childDir : childDirs) {
+                sb.append(isWindows() ? WINDOWS_SEPARATOR : UNIX_SEPARATOR).append(childDir);
+            }
+        }
+        return sb.toString();
+    }
+
+    public static String fixPath(final String path) {
+        if (isWindows()) {
+            return path.startsWith(String.valueOf(UNIX_SEPARATOR))
+                    && path.contains(PATH_COLON)
+                    ? path.substring(1)
+                    : path;
+        }
+        return path;
+    }
+
+    public static String getPathFromJar(final String jarPath) {
+        final int jarEndPos = jarPath.lastIndexOf(JAR_SEPARATOR);
+        return jarEndPos < 0
+                ? EMPTY_STRING
+                : jarPath.substring(jarEndPos + JAR_SEPARATOR.length());
+    }
+
+    private static String removeExtension(final String fileName) {
+        if (fileName == null) {
+            return null;
+        }
+        final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
+        return extensionPos < 0
+                ? fileName
+                : fileName.substring(0, extensionPos);
+    }
+
+    private static boolean isWindows() {
+        return (OS.contains("win"));
+    }
+
+    private FilenameUtils() {
+        throw new AssertionError(
+                "No org.hedgecode.chess.img.FilenameUtils instances!"
+        );
+    }
+
+}