blob: 4d8b4a002ebc0f5fa8eae570f32b6f502b395038 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2018 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.rj.servi;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.statet.ecommons.runtime.core.ECommonsRuntime;
import org.eclipse.statet.ecommons.runtime.core.ECommonsRuntime.AppEnvironment;
import org.eclipse.statet.rj.servi.RServiUtil;
public class Utils {
public static final boolean IS_WINDOWS= System.getProperty("os.name").toLowerCase().startsWith("win");
public static final boolean IS_MAC= System.getProperty("os.name").toLowerCase().startsWith("mac");
public static void preLoad() {
// Load class for errors
new Status(IStatus.INFO, RServiUtil.RJ_SERVI_ID, Messages.StartNode_error_message);
}
/**
* Utility class to parse command line arguments.
*/
private static class ArgumentParser {
private final String args;
private int index= 0;
private int ch= -1;
public ArgumentParser(final String args) {
this.args= args;
}
public List<String> parseArguments() {
final List<String> v= new ArrayList<>();
this.ch= getNext();
while (this.ch > 0) {
if (Character.isWhitespace((char) this.ch)) {
this.ch= getNext();
}
else {
if (this.ch == '"') {
final StringBuffer buf= new StringBuffer();
buf.append(parseString());
if (buf.length() == 0 && IS_WINDOWS) {
// empty string on windows platform
buf.append("\"\""); //$NON-NLS-1$
}
v.add(buf.toString());
}
else {
v.add(parseToken());
}
}
}
return v;
}
private int getNext() {
if (this.index < this.args.length()) {
return this.args.charAt(this.index++);
}
return -1;
}
private String parseString() {
this.ch= getNext();
if (this.ch == '"') {
this.ch= getNext();
return ""; //$NON-NLS-1$
}
final StringBuffer buf= new StringBuffer();
while (this.ch > 0 && this.ch != '"') {
if (this.ch == '\\') {
this.ch= getNext();
if (this.ch != '"') { // Only escape double quotes
buf.append('\\');
}
else {
if (IS_WINDOWS) {
buf.append('\\');
}
}
}
if (this.ch > 0) {
buf.append((char) this.ch);
this.ch= getNext();
}
}
this.ch= getNext();
return buf.toString();
}
private String parseToken() {
final StringBuffer buf= new StringBuffer();
while (this.ch > 0 && !Character.isWhitespace((char) this.ch)) {
if (this.ch == '\\') {
this.ch= getNext();
if (Character.isWhitespace((char) this.ch)) {
// end of token, don't lose trailing backslash
buf.append('\\');
return buf.toString();
}
if (this.ch > 0) {
if (this.ch != '"') { // Only escape double quotes
buf.append('\\');
}
else {
if (IS_WINDOWS) {
buf.append('\\');
}
}
buf.append((char) this.ch);
this.ch= getNext();
}
else if (this.ch == -1) { // Don't lose a trailing backslash
buf.append('\\');
}
}
else if (this.ch == '"') {
buf.append(parseString());
}
else {
buf.append((char) this.ch);
this.ch= getNext();
}
}
return buf.toString();
}
}
public static List<String> parseArguments(final String args) {
if (args == null) {
return new ArrayList<>(0);
}
else {
final ArgumentParser parser= new ArgumentParser(args);
return parser.parseArguments();
}
}
public static void logInfo(final String message) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.INFO, RServiUtil.RJ_SERVI_ID, message));
}
}
public static void logInfo(final String message, final Throwable e) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.INFO, RServiUtil.RJ_SERVI_ID, message, e));
}
}
public static void logWarning(final String message) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.WARNING, RServiUtil.RJ_SERVI_ID, message));
}
}
public static void logWarning(final String message, final Throwable e) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.WARNING, RServiUtil.RJ_SERVI_ID, message, e));
}
}
public static void logError(final String message) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.ERROR, RServiUtil.RJ_SERVI_ID, message));
}
}
public static void logError(final String message, final Throwable e) {
final AppEnvironment env= ECommonsRuntime.getEnv();
if (env != null) {
env.log(new Status(IStatus.ERROR, RServiUtil.RJ_SERVI_ID, message, e));
}
}
public static String getProperty(final Properties properties, final String key, final String altKey) {
String s= properties.getProperty(key);
if (s == null && altKey != null) {
s= properties.getProperty(altKey);
}
return s;
}
public static void setProperty(final Properties properties, final String key, final String value) {
if (value != null) {
properties.setProperty(key, value);
}
else {
properties.remove(key);
}
}
private Utils() {
}
}