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