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