]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.application.arguments;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.InputStream;\r
16 import java.net.URL;\r
17 import java.util.ArrayList;\r
18 import java.util.List;\r
19 \r
20 import org.eclipse.core.runtime.IStatus;\r
21 import org.eclipse.core.runtime.Platform;\r
22 import org.eclipse.core.runtime.Status;\r
23 import org.simantics.application.internal.Activator;\r
24 \r
25 /**\r
26  * Simple utilities for IApplication implementation.\r
27  */\r
28 public class ApplicationUtils {\r
29 \r
30     /**\r
31      * Appends properties from the specified URL to system properties which are\r
32      * available through {@link System#getProperties()}.\r
33      * \r
34      * @param props a valid URL or <code>null</code> for no operation.\r
35      */\r
36     public static void loadSystemProperties(URL props) {\r
37         if (props != null) {\r
38             InputStream in = null;\r
39             try {\r
40                 in = props.openStream();\r
41                 System.getProperties().load(in);\r
42             } catch (IOException e) {\r
43                 Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Failed to load system properties: " + props, e));\r
44             } finally {\r
45                 try {\r
46                     if (in != null)\r
47                         in.close();\r
48                 } catch (IOException e) {\r
49                     Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Failed to close system properties: " + props, e));\r
50                 }\r
51             }\r
52         }\r
53     }\r
54 \r
55     /**\r
56      * Decodes a dot ('.') -separated path into the path elements. The '\'\r
57      * character is used for escaping dots themselves.\r
58      * \r
59      * <p>Examples of the results:</p>\r
60      * <ul>\r
61      * <li>decodePath("abc") => ["abc"]</li>\r
62      * <li>decodePath("abc.def.efg") => ["abc", "def", "efg"]</li>\r
63      * <li>decodePath("abc\\..def\\..ghi.jkl") => ["abc.def..ghi", "jkl"]</li>\r
64      * <li>decodePath("abc\\..def\\..ghi.jkl\\.") => ["abc.def.ghi", "jkl."]</li>\r
65      * <li>decodePath("abc\\..def\\..ghi.jkl\\..") => ["abc.def.ghi", "jkl."]</li>\r
66      * <li>decodePath("abc\\..def.ghi\\.jkl") => ["abc.def", "ghi.jkl"]</li>\r
67      * <li>decodePath("abc\\..def.ghi\\.jkl.") => ["abc.def", "ghi.jkl"]</li>\r
68      * <li>decodePath("abc\\..def.ghi\\.jkl\\..") => ["abc.def", "ghi.jkl."]</li>\r
69      * </ul>\r
70      * \r
71      * @param dottedPath\r
72      * @return the elements of the dot-separated path as a String array\r
73      */\r
74     public static String[] decodePath(String dottedPath) {\r
75         if (dottedPath == null)\r
76             return new String[0];\r
77 \r
78         List<String> result = new ArrayList<String>();\r
79 \r
80         int index = 0;\r
81         while (dottedPath.length() > 0) {\r
82             int dotIndex = dottedPath.indexOf(".", index);\r
83             // Skip escaped dots\r
84             if (dotIndex < 0) {\r
85                 if (dottedPath.length() > 0) {\r
86                     // Take the rest of the string and stop splitting.\r
87                     result.add(dottedPath.replace("\\.", "."));\r
88                 }\r
89                 break;\r
90             }\r
91             if (dotIndex > 0 && dottedPath.charAt(dotIndex - 1) == '\\') {\r
92                 index = dotIndex + 1;\r
93                 continue;\r
94             }\r
95             // Grab until the next dot and replace any escaped dots with dots.\r
96             String pathPart = dottedPath.substring(0, dotIndex);\r
97             result.add(pathPart.replace("\\.", "."));\r
98             dottedPath = dottedPath.substring(dotIndex + 1);\r
99             index = 0;\r
100         }\r
101 \r
102         return result.toArray(new String[result.size()]);\r
103     }\r
104     \r
105     /**\r
106      * Encodes a path of names into a single dot-separated ('.') path string.\r
107      * All '.' characters in the path segment names themselves are escaped with\r
108      * a single '\' character before concatenation into the result.\r
109      * \r
110      * <p>\r
111      * String encoded with this method can be decoded with\r
112      * {@link #decodePath(String)}.\r
113      * \r
114      * @param namePath a path of names\r
115      * @return the path encoded into a single string\r
116      */\r
117     public static String encodePath(String[] namePath) {\r
118         StringBuilder b = new StringBuilder();\r
119         boolean first = true;\r
120         for (String s : namePath) {\r
121             if (!first)\r
122                 b.append('.');\r
123             first = false;\r
124             b.append(s.replace(".", "\\."));\r
125         }\r
126         return b.toString();\r
127     }\r
128 \r
129     /**\r
130      * Parse the arguments received by the application (available through\r
131      * {@link Platform#getApplicationArgs()}) for instances of the specified\r
132      * arguments. Found matches are contained in the resulting IArguments\r
133      * instance.\r
134      * \r
135      * @param accepted the arguments that should be parsed\r
136      * @return the argument matches that were found\r
137      */\r
138     public static IArguments parseApplicationArguments(IArgumentFactory<?>... accepted) {\r
139         return Arguments.parse(Platform.getApplicationArgs(), accepted);\r
140     }\r
141     \r
142 //    public static void main(String[] args) {\r
143 //        System.out.println(Arrays.toString(decodePath("abc")));\r
144 //        System.out.println(Arrays.toString(decodePath("abc.def.efg")));\r
145 //        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl")));\r
146 //        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl\\.")));\r
147 //        System.out.println(Arrays.toString(decodePath("abc\\..def\\..ghi.jkl\\..")));\r
148 //        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl")));\r
149 //        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl.")));\r
150 //        System.out.println(Arrays.toString(decodePath("abc\\..def.ghi\\.jkl\\..")));\r
151 //    }\r
152 \r
153 }\r