2018-09-17 10:20:34 +03:00
|
|
|
package ru.gravit.utils.helper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.LauncherAPI;
|
2019-03-08 14:58:18 +03:00
|
|
|
import ru.gravit.utils.command.CommandException;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import javax.script.ScriptEngineFactory;
|
|
|
|
import javax.script.ScriptEngineManager;
|
2019-03-08 14:58:18 +03:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.LinkedList;
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
|
|
|
public final class CommonHelper {
|
|
|
|
@LauncherAPI
|
|
|
|
public static final ScriptEngineManager scriptManager = new ScriptEngineManager();
|
|
|
|
@LauncherAPI
|
|
|
|
public static final ScriptEngineFactory nashornFactory = getEngineFactories(scriptManager);
|
|
|
|
|
|
|
|
private static ScriptEngineFactory getEngineFactories(ScriptEngineManager manager) {
|
|
|
|
// Метод похож на костыль но таковым не является, ибо единоразовое получение фактории быстрее, чем её переполучение на ходу.
|
|
|
|
for (ScriptEngineFactory fact : manager.getEngineFactories())
|
2018-09-22 17:33:00 +03:00
|
|
|
if (fact.getNames().contains("nashorn") || fact.getNames().contains("Nashorn")) return fact;
|
2018-09-17 10:07:32 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static String low(String s) {
|
|
|
|
return s.toLowerCase(Locale.US);
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static boolean multiMatches(Pattern[] pattern, String from) {
|
|
|
|
for (Pattern p : pattern)
|
2018-09-22 17:33:00 +03:00
|
|
|
if (p.matcher(from).matches()) return true;
|
2018-09-17 10:07:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static String multiReplace(Pattern[] pattern, String from, String replace) {
|
|
|
|
Matcher m;
|
|
|
|
String tmp = null;
|
|
|
|
for (Pattern p : pattern) {
|
|
|
|
m = p.matcher(from);
|
|
|
|
if (m.matches()) tmp = m.replaceAll(replace);
|
|
|
|
}
|
|
|
|
return tmp != null ? tmp : from;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static ScriptEngine newScriptEngine() {
|
|
|
|
return nashornFactory.getScriptEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static Thread newThread(String name, boolean daemon, Runnable runnable) {
|
|
|
|
Thread thread = new Thread(runnable);
|
|
|
|
thread.setDaemon(daemon);
|
|
|
|
if (name != null)
|
2018-09-22 17:33:00 +03:00
|
|
|
thread.setName(name);
|
2018-09-17 10:07:32 +03:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
@LauncherAPI
|
|
|
|
public static String replace(String source, String... params) {
|
|
|
|
for (int i = 0; i < params.length; i += 2)
|
2018-09-22 17:33:00 +03:00
|
|
|
source = source.replace('%' + params[i] + '%', params[i + 1]);
|
2018-09-17 10:07:32 +03:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CommonHelper() {
|
|
|
|
}
|
2019-03-08 14:58:18 +03:00
|
|
|
|
|
|
|
public static String[] parseCommand(CharSequence line) throws CommandException {
|
|
|
|
boolean quoted = false;
|
|
|
|
boolean wasQuoted = false;
|
|
|
|
|
|
|
|
// Read line char by char
|
|
|
|
Collection<String> result = new LinkedList<>();
|
|
|
|
StringBuilder builder = new StringBuilder(100);
|
|
|
|
for (int i = 0; i <= line.length(); i++) {
|
|
|
|
boolean end = i >= line.length();
|
|
|
|
char ch = end ? '\0' : line.charAt(i);
|
|
|
|
|
|
|
|
// Maybe we should read next argument?
|
|
|
|
if (end || !quoted && Character.isWhitespace(ch)) {
|
|
|
|
if (end && quoted)
|
|
|
|
throw new CommandException("Quotes wasn't closed");
|
|
|
|
|
|
|
|
// Empty args are ignored (except if was quoted)
|
|
|
|
if (wasQuoted || builder.length() > 0)
|
|
|
|
result.add(builder.toString());
|
|
|
|
|
|
|
|
// Reset file builder
|
|
|
|
wasQuoted = false;
|
|
|
|
builder.setLength(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append next char
|
|
|
|
switch (ch) {
|
|
|
|
case '"': // "abc"de, "abc""de" also allowed
|
|
|
|
quoted = !quoted;
|
|
|
|
wasQuoted = true;
|
|
|
|
break;
|
|
|
|
case '\\': // All escapes, including spaces etc
|
|
|
|
if (i + 1 >= line.length())
|
|
|
|
throw new CommandException("Escape character is not specified");
|
|
|
|
char next = line.charAt(i + 1);
|
|
|
|
builder.append(next);
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
default: // Default char, simply append
|
|
|
|
builder.append(ch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return result as array
|
|
|
|
return result.toArray(new String[0]);
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|