]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / strings / FileNameUtils.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/strings/FileNameUtils.java
new file mode 100644 (file)
index 0000000..a9c53a4
--- /dev/null
@@ -0,0 +1,138 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * Created on Jan 21, 2005\r
+ * \r
+ * Copyright Toni Kalajainen\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.simantics.utils.strings;\r
+\r
+import java.io.File;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ * \r
+ */\r
+public class FileNameUtils {\r
+\r
+    /**\r
+     * Creates directories\r
+     * @param path\r
+     */\r
+    public static void forcePath(String path) {\r
+        // Create directories\r
+        (new File(path)).mkdirs();\r
+    }\r
+\r
+    public static void deleteFile(String filename) {\r
+        (new File(filename)).delete();\r
+    }\r
+\r
+    /**\r
+     * Extracts file's directory Excludes last / \\r
+     * \r
+     * @param filename\r
+     *            the file\r
+     * @return the path\r
+     */\r
+    public static String extractFileDir(String filename) {\r
+        // construct directory name\r
+        String splitted[] = filename.replaceAll("\\\\", "/").split("/");\r
+        if (splitted.length > 1)\r
+            return filename.substring(0, filename.length()\r
+                    - splitted[splitted.length - 1].length() - 1);\r
+        return "";\r
+    }\r
+\r
+    /**\r
+     * Extracts file's name\r
+     * \r
+     * @param path\r
+     *            the path\r
+     * @return the filename\r
+     */\r
+    public static String extractFileName(String path) {\r
+        // construct directory name\r
+        String splitted[] = path.replaceAll("\\\\", "/").split("/");\r
+        if (splitted.length > 1)\r
+            return splitted[splitted.length-1];\r
+        return path;\r
+    }\r
+\r
+    /**\r
+     * Extracts file's name before dot\r
+     * \r
+     * @param filename\r
+     *            the file name\r
+     * @return the name\r
+     */\r
+    public static String extractFilePreDot(String filename) {\r
+        // construct directory name\r
+        String splitted[] = filename.split("\\.");\r
+        if (splitted.length > 1)\r
+            return filename.substring(0, filename.length()\r
+                    - splitted[splitted.length - 1].length() - 1);\r
+        return filename;\r
+    }\r
+\r
+\r
+    /**\r
+     * Extracts file's extension\r
+     * \r
+     * @param filename\r
+     *            the file name\r
+     * @return the name\r
+     */\r
+    public static String extractFileExt(String filename) {\r
+        // construct directory name\r
+        String splitted[] = filename.split("\\.");\r
+        if (splitted.length > 1)\r
+            return splitted[splitted.length-1];\r
+        return "";\r
+    }\r
+\r
+    /**\r
+     * Extracts file's directory Ixcludes last / \\r
+     * \r
+     * @param filename\r
+     *            the file\r
+     * @return the path\r
+     */\r
+    public static String extractFilePath(String filename) {\r
+        // construct directory name\r
+        String splitted[] = filename.replaceAll("\\\\", "/").split("/");\r
+        if (splitted.length > 1)\r
+            return filename.substring(0, filename.length()\r
+                    - splitted[splitted.length - 1].length());\r
+        return "";\r
+    }\r
+\r
+    public static void main(String[] args) {\r
+        System.out.println(extractFileDir("c:\\jees/pox/file.dat"));\r
+        System.out.println(extractFilePath("c:\\jees/pox/file.dat"));\r
+        System.out.println(extractFileName("c:\\jees/pox/file.dat"));\r
+        System.out.println(extractFilePreDot("c:\\jees/pox/file.dat"));\r
+        System.out.println(extractFileExt("c:\\jees/pox/file.dat"));\r
+    }\r
+\r
+}\r