[LIB-9] Separate chesshog-graphics module
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / FilenameUtils.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
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
17 package org.hedgecode.chess.img;
18
19 /**
20  * Utilities for path, file name and extension manipulation.
21  *
22  * @author Dmitry Samoshin aka gotty
23  */
24 public final class FilenameUtils {
25
26     private static final String EMPTY_STRING = "";
27
28     private static final char EXTENSION_SEPARATOR = '.';
29
30     private static final char UNIX_SEPARATOR = '/';
31     private static final char WINDOWS_SEPARATOR = '\\';
32
33     private static final String PATH_COLON = ":";
34
35     private static final String JAR_SEPARATOR = ".jar!";
36
37     private static final String OS = System.getProperty("os.name").toLowerCase();
38
39     public static String getName(final String fileName) {
40         if (fileName == null) {
41             return null;
42         }
43         final int endPathIndex = Math.max(
44                 fileName.lastIndexOf(UNIX_SEPARATOR),
45                 fileName.lastIndexOf(WINDOWS_SEPARATOR)
46         );
47         return fileName.substring(endPathIndex + 1);
48     }
49
50     public static String getBaseName(final String fileName) {
51         return removeExtension(
52                 getName(fileName)
53         );
54     }
55
56     public static String getExtension(final String fileName) {
57         if (fileName == null) {
58             return null;
59         }
60         final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
61         return extensionPos < 0
62                 ? EMPTY_STRING
63                 : fileName.substring(extensionPos + 1);
64     }
65
66     public static String getFullPath(final String parentDir, final String... childDirs) {
67         StringBuilder sb = new StringBuilder();
68         sb.append(parentDir);
69         if (childDirs.length > 0) {
70             for (String childDir : childDirs) {
71                 sb.append(isWindows() ? WINDOWS_SEPARATOR : UNIX_SEPARATOR).append(childDir);
72             }
73         }
74         return sb.toString();
75     }
76
77     public static String fixPath(final String path) {
78         if (isWindows()) {
79             return path.startsWith(String.valueOf(UNIX_SEPARATOR))
80                     && path.contains(PATH_COLON)
81                     ? path.substring(1)
82                     : path;
83         }
84         return path;
85     }
86
87     public static String getPathFromJar(final String jarPath) {
88         final int jarEndPos = jarPath.lastIndexOf(JAR_SEPARATOR);
89         return jarEndPos < 0
90                 ? EMPTY_STRING
91                 : jarPath.substring(jarEndPos + JAR_SEPARATOR.length());
92     }
93
94     private static String removeExtension(final String fileName) {
95         if (fileName == null) {
96             return null;
97         }
98         final int extensionPos = fileName.lastIndexOf(EXTENSION_SEPARATOR);
99         return extensionPos < 0
100                 ? fileName
101                 : fileName.substring(0, extensionPos);
102     }
103
104     private static boolean isWindows() {
105         return (OS.contains("win"));
106     }
107
108     private FilenameUtils() {
109         throw new AssertionError(
110                 "No org.hedgecode.chess.img.FilenameUtils instances!"
111         );
112     }
113
114 }