1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.application.arguments;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
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;
26 * Simple utilities for IApplication implementation.
28 public class ApplicationUtils {
31 * Appends properties from the specified URL to system properties which are
32 * available through {@link System#getProperties()}.
34 * @param props a valid URL or <code>null</code> for no operation.
36 public static void loadSystemProperties(URL props) {
38 InputStream in = null;
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));
48 } catch (IOException e) {
49 Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Failed to close system properties: " + props, e));
56 * Decodes a dot ('.') -separated path into the path elements. The '\'
57 * character is used for escaping dots themselves.
59 * <p>Examples of the results:</p>
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>
72 * @return the elements of the dot-separated path as a String array
74 public static String[] decodePath(String dottedPath) {
75 if (dottedPath == null)
78 List<String> result = new ArrayList<String>();
81 while (dottedPath.length() > 0) {
82 int dotIndex = dottedPath.indexOf(".", index);
85 if (dottedPath.length() > 0) {
86 // Take the rest of the string and stop splitting.
87 result.add(dottedPath.replace("\\.", "."));
91 if (dotIndex > 0 && dottedPath.charAt(dotIndex - 1) == '\\') {
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);
102 return result.toArray(new String[result.size()]);
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.
111 * String encoded with this method can be decoded with
112 * {@link #decodePath(String)}.
114 * @param namePath a path of names
115 * @return the path encoded into a single string
117 public static String encodePath(String[] namePath) {
118 StringBuilder b = new StringBuilder();
119 boolean first = true;
120 for (String s : namePath) {
124 b.append(s.replace(".", "\\."));
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
135 * @param accepted the arguments that should be parsed
136 * @return the argument matches that were found
138 public static IArguments parseApplicationArguments(IArgumentFactory<?>... accepted) {
139 return Arguments.parse(Platform.getApplicationArgs(), accepted);
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\\..")));