]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.application/src/org/simantics/application/arguments/ApplicationUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.application / src / org / simantics / application / arguments / ApplicationUtils.java
diff --git a/bundles/org.simantics.application/src/org/simantics/application/arguments/ApplicationUtils.java b/bundles/org.simantics.application/src/org/simantics/application/arguments/ApplicationUtils.java
new file mode 100644 (file)
index 0000000..43460fd
--- /dev/null
@@ -0,0 +1,153 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\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
+package org.simantics.application.arguments;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.Status;\r
+import org.simantics.application.internal.Activator;\r
+\r
+/**\r
+ * Simple utilities for IApplication implementation.\r
+ */\r
+public class ApplicationUtils {\r
+\r
+    /**\r
+     * Appends properties from the specified URL to system properties which are\r
+     * available through {@link System#getProperties()}.\r
+     * \r
+     * @param props a valid URL or <code>null</code> for no operation.\r
+     */\r
+    public static void loadSystemProperties(URL props) {\r
+        if (props != null) {\r
+            InputStream in = null;\r
+            try {\r
+                in = props.openStream();\r
+                System.getProperties().load(in);\r
+            } catch (IOException e) {\r
+                Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Failed to load system properties: " + props, e));\r
+            } finally {\r
+                try {\r
+                    if (in != null)\r
+                        in.close();\r
+                } catch (IOException e) {\r
+                    Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Failed to close system properties: " + props, e));\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Decodes a dot ('.') -separated path into the path elements. The '\'\r
+     * character is used for escaping dots themselves.\r
+     * \r
+     * <p>Examples of the results:</p>\r
+     * <ul>\r
+     * <li>decodePath("abc") => ["abc"]</li>\r
+     * <li>decodePath("abc.def.efg") => ["abc", "def", "efg"]</li>\r
+     * <li>decodePath("abc\\..def\\..ghi.jkl") => ["abc.def..ghi", "jkl"]</li>\r
+     * <li>decodePath("abc\\..def\\..ghi.jkl\\.") => ["abc.def.ghi", "jkl."]</li>\r
+     * <li>decodePath("abc\\..def\\..ghi.jkl\\..") => ["abc.def.ghi", "jkl."]</li>\r
+     * <li>decodePath("abc\\..def.ghi\\.jkl") => ["abc.def", "ghi.jkl"]</li>\r
+     * <li>decodePath("abc\\..def.ghi\\.jkl.") => ["abc.def", "ghi.jkl"]</li>\r
+     * <li>decodePath("abc\\..def.ghi\\.jkl\\..") => ["abc.def", "ghi.jkl."]</li>\r
+     * </ul>\r
+     * \r
+     * @param dottedPath\r
+     * @return the elements of the dot-separated path as a String array\r
+     */\r
+    public static String[] decodePath(String dottedPath) {\r
+        if (dottedPath == null)\r
+            return new String[0];\r
+\r
+        List<String> result = new ArrayList<String>();\r
+\r
+        int index = 0;\r
+        while (dottedPath.length() > 0) {\r
+            int dotIndex = dottedPath.indexOf(".", index);\r
+            // Skip escaped dots\r
+            if (dotIndex < 0) {\r
+                if (dottedPath.length() > 0) {\r
+                    // Take the rest of the string and stop splitting.\r
+                    result.add(dottedPath.replace("\\.", "."));\r
+                }\r
+                break;\r
+            }\r
+            if (dotIndex > 0 && dottedPath.charAt(dotIndex - 1) == '\\') {\r
+                index = dotIndex + 1;\r
+                continue;\r
+            }\r
+            // Grab until the next dot and replace any escaped dots with dots.\r
+            String pathPart = dottedPath.substring(0, dotIndex);\r
+            result.add(pathPart.replace("\\.", "."));\r
+            dottedPath = dottedPath.substring(dotIndex + 1);\r
+            index = 0;\r
+        }\r
+\r
+        return result.toArray(new String[result.size()]);\r
+    }\r
+    \r
+    /**\r
+     * Encodes a path of names into a single dot-separated ('.') path string.\r
+     * All '.' characters in the path segment names themselves are escaped with\r
+     * a single '\' character before concatenation into the result.\r
+     * \r
+     * <p>\r
+     * String encoded with this method can be decoded with\r
+     * {@link #decodePath(String)}.\r
+     * \r
+     * @param namePath a path of names\r
+     * @return the path encoded into a single string\r
+     */\r
+    public static String encodePath(String[] namePath) {\r
+        StringBuilder b = new StringBuilder();\r
+        boolean first = true;\r
+        for (String s : namePath) {\r
+            if (!first)\r
+                b.append('.');\r
+            first = false;\r
+            b.append(s.replace(".", "\\."));\r
+        }\r
+        return b.toString();\r
+    }\r
+\r
+    /**\r
+     * Parse the arguments received by the application (available through\r
+     * {@link Platform#getApplicationArgs()}) for instances of the specified\r
+     * arguments. Found matches are contained in the resulting IArguments\r
+     * instance.\r
+     * \r
+     * @param accepted the arguments that should be parsed\r
+     * @return the argument matches that were found\r
+     */\r
+    public static IArguments parseApplicationArguments(IArgumentFactory<?>... accepted) {\r
+        return Arguments.parse(Platform.getApplicationArgs(), accepted);\r
+    }\r
+    \r
+//    public static void main(String[] args) {\r
+//        System.out.println(Arrays.toString(decodePath("abc")));\r
+//        System.out.println(Arrays.toString(decodePath("abc.def.efg")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl\\.")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl\\..")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl.")));\r
+//        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl\\..")));\r
+//    }\r
+\r
+}\r