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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.application.arguments;
\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
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
26 * Simple utilities for IApplication implementation.
\r
28 public class ApplicationUtils {
\r
31 * Appends properties from the specified URL to system properties which are
\r
32 * available through {@link System#getProperties()}.
\r
34 * @param props a valid URL or <code>null</code> for no operation.
\r
36 public static void loadSystemProperties(URL props) {
\r
37 if (props != null) {
\r
38 InputStream in = null;
\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
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
56 * Decodes a dot ('.') -separated path into the path elements. The '\'
\r
57 * character is used for escaping dots themselves.
\r
59 * <p>Examples of the results:</p>
\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
72 * @return the elements of the dot-separated path as a String array
\r
74 public static String[] decodePath(String dottedPath) {
\r
75 if (dottedPath == null)
\r
76 return new String[0];
\r
78 List<String> result = new ArrayList<String>();
\r
81 while (dottedPath.length() > 0) {
\r
82 int dotIndex = dottedPath.indexOf(".", index);
\r
83 // Skip escaped dots
\r
85 if (dottedPath.length() > 0) {
\r
86 // Take the rest of the string and stop splitting.
\r
87 result.add(dottedPath.replace("\\.", "."));
\r
91 if (dotIndex > 0 && dottedPath.charAt(dotIndex - 1) == '\\') {
\r
92 index = dotIndex + 1;
\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
102 return result.toArray(new String[result.size()]);
\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
111 * String encoded with this method can be decoded with
\r
112 * {@link #decodePath(String)}.
\r
114 * @param namePath a path of names
\r
115 * @return the path encoded into a single string
\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
124 b.append(s.replace(".", "\\."));
\r
126 return b.toString();
\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
135 * @param accepted the arguments that should be parsed
\r
136 * @return the argument matches that were found
\r
138 public static IArguments parseApplicationArguments(IArgumentFactory<?>... accepted) {
\r
139 return Arguments.parse(Platform.getApplicationArgs(), accepted);
\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