/*******************************************************************************
* 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 null
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.
*
*
Examples of the results:
** 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\\.."))); // } }