blob: 4cf12b4a661b805279ff6742d26c0b0c85d3641a [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 2017 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.rj.server.util;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.statet.rj.server.RjsStatus;
/**
* Server utilities.
*/
public class ServerUtils {
public static final String RJ_DATA_ID= "org.eclipse.statet.rj.data";
public static final String RJ_SERVER_ID= "org.eclipse.statet.rj.server";
public static final int[] RJ_VERSION= new int[] { 2, 1, 0 };
public static final RjsStatus MISSING_ANSWER_STATUS= new RjsStatus(RjsStatus.ERROR, 121, "Server error (missing answer).");
/**
* Split at next ':'
* @param arg the argument to split
* @return String array of length 2
*/
public static String[] getArgSubValue(final String arg) {
final String[] args= new String[2];
if (arg != null && arg.length() > 0) {
final int idx= arg.indexOf(':');
if (idx >= 0) {
args[0]= arg.substring(0, idx);
args[1]= arg.substring(idx+1);
}
else {
args[0]= arg;
}
}
else {
args[0]= "";
}
return args;
}
/**
* Split at next '='
* @param arg the argument to split
* @return String array of length 2
*/
public static String[] getArgConfigValue(final String arg) {
final String[] args= new String[2];
if (arg != null && arg.length() > 0) {
final int idx= arg.indexOf('=');
if (idx >= 0) {
args[0]= arg.substring(0, idx);
args[1]= arg.substring(idx+1);
}
else {
args[0]= arg;
}
}
else {
args[0]= "";
}
return args;
}
/**
* Split at ','
* @param arg the argument to split
* @return List with String
*/
public static List<String> getArgValueList(final String arg) {
if (arg != null && arg.length() > 0) {
return Arrays.asList(arg.split(","));
}
else {
return Collections.emptyList();
}
}
public static void prettyPrint(final Map map, final StringBuilder sb) {
final String sep= System.getProperty("line.separator")+"\t";
final Set<Entry<?, ?>> entrySet= map.entrySet();
for (final Entry<?, ?> entry : entrySet) {
sb.append(sep);
sb.append(entry.getKey());
sb.append('=');
final Object value= entry.getValue();
if (value != null) {
sb.append(value);
}
}
}
public static void prettyPrint(final Collection<?> list, final StringBuilder sb) {
final String sep= System.getProperty("line.separator")+"\t";
for (final Object value : list) {
sb.append(sep);
if (value != null) {
sb.append(value);
}
}
}
public static void prettyPrintVersion(final int[] version, final StringBuilder sb) {
if (version == null || version.length == 0) {
sb.append("<missing>");
}
else {
sb.append(version[0]);
for (int i= 1; i < version.length; i++) {
sb.append('.');
sb.append(version[i]);
}
}
}
public static String concatPathVar(final Collection<String> entries) {
if (entries.isEmpty()) {
return "";
}
final StringBuilder sb= new StringBuilder();
for (final String entry : entries) {
sb.append(entry);
sb.append(File.pathSeparatorChar);
}
return sb.substring(0, sb.length()-1);
}
private static String encodeCodebaseEntry(String path) {
if (path == null || path.isEmpty()) {
return null;
}
URI uri= null;
try {
if (path.startsWith("file:")) {
path= path.substring(5);
uri= new URI("file", null, path, null);
}
else {
if (File.separatorChar == '\\') {
path= path.replace('\\', '/');
}
if (path.charAt(0) != '/') {
path= '/' + path;
}
uri= new URI("file", null, path, null);
}
return uri.toString();
}
catch (final URISyntaxException e) {
throw new IllegalArgumentException("Invalid entry for codebase", e);
}
}
/**
* Concats the specified entries to a valid codebase property value.
* The entries have to be path in the local file system. It is recommend to specify the entries
* as URL with the schema 'file'.
*/
public static String concatCodebase(final Collection<String> entries) {
if (entries.isEmpty()) {
return "";
}
final StringBuilder sb= new StringBuilder();
for (final String entry : entries) {
final String path= encodeCodebaseEntry(entry);
if (path != null) {
sb.append(path);
sb.append(' ');
}
}
return sb.substring(0, sb.length()-1);
}
public static boolean delDir(final File dir) {
final String[] children= dir.list();
for (final String child : children) {
final File file= new File(dir, child);
if (file.isDirectory()) {
delDir(file);
}
else {
file.delete();
}
}
return dir.delete();
}
public static void cleanDir(final File dir, final String exclude) {
final String[] children= dir.list();
for (final String child : children) {
if (child.equals(exclude)) {
continue;
}
final File file= new File(dir, child);
if (file.isDirectory()) {
delDir(file);
}
else {
file.delete();
}
}
}
}