Commit c54745e2 authored by Mygod's avatar Mygod

Adapt to simple-obfs plugin options parser

parent e3cf54f7
...@@ -23,12 +23,11 @@ public final class PluginOptions extends HashMap<String, String> { ...@@ -23,12 +23,11 @@ public final class PluginOptions extends HashMap<String, String> {
super(initialCapacity, loadFactor); super(initialCapacity, loadFactor);
} }
public PluginOptions(String options) throws IllegalArgumentException { private PluginOptions(String options, boolean parseId) throws IllegalArgumentException {
if (TextUtils.isEmpty(options)) return; if (TextUtils.isEmpty(options)) return;
final StringTokenizer tokenizer = new StringTokenizer(options, "\\=;", true); final StringTokenizer tokenizer = new StringTokenizer(options, "\\=;", true);
final StringBuilder current = new StringBuilder(); final StringBuilder current = new StringBuilder();
String key = null; String key = null;
boolean firstEntry = true;
while (tokenizer.hasMoreTokens()) { while (tokenizer.hasMoreTokens()) {
String nextToken = tokenizer.nextToken(); String nextToken = tokenizer.nextToken();
if ("\\".equals(nextToken)) current.append(tokenizer.nextToken()); if ("\\".equals(nextToken)) current.append(tokenizer.nextToken());
...@@ -36,22 +35,28 @@ public final class PluginOptions extends HashMap<String, String> { ...@@ -36,22 +35,28 @@ public final class PluginOptions extends HashMap<String, String> {
if (key != null) throw new IllegalArgumentException("Duplicate keys in " + options); if (key != null) throw new IllegalArgumentException("Duplicate keys in " + options);
key = current.toString(); key = current.toString();
current.setLength(0); current.setLength(0);
} else if (";".equals(nextToken)) { } else if (";".equals(nextToken))
if (key != null) { if (key != null) {
put(key, current.toString()); put(key, current.toString());
key = null; key = null;
} else if (firstEntry) id = current.toString(); } else if (parseId) {
else throw new IllegalArgumentException("Value missing in " + options); id = current.toString();
firstEntry = false; parseId = false;
} else {
put(current.toString(), null);
current.setLength(0);
} }
} }
} }
public PluginOptions(String options) throws IllegalArgumentException {
this(options, true);
}
public PluginOptions(String id, String options) throws IllegalArgumentException { public PluginOptions(String id, String options) throws IllegalArgumentException {
this(options); this(options, false);
this.id = id; this.id = id;
} }
public String id = ""; public String id;
private static void append(StringBuilder result, String str) { private static void append(StringBuilder result, String str) {
for (int i = 0; i < str.length(); ++i) { for (int i = 0; i < str.length(); ++i) {
...@@ -65,11 +70,14 @@ public final class PluginOptions extends HashMap<String, String> { ...@@ -65,11 +70,14 @@ public final class PluginOptions extends HashMap<String, String> {
public String toString(boolean trimId) { public String toString(boolean trimId) {
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
if (!trimId) if (TextUtils.isEmpty(id)) return ""; else append(result, id); if (!trimId) if (TextUtils.isEmpty(id)) return ""; else append(result, id);
for (Entry<String, String> entry : entrySet()) if (entry.getValue() != null) { for (Entry<String, String> entry : entrySet()) {
if (result.length() > 0) result.append(';'); if (result.length() > 0) result.append(';');
append(result, entry.getKey()); append(result, entry.getKey());
String value = entry.getValue();
if (value != null) {
result.append('='); result.append('=');
append(result, entry.getValue()); append(result, value);
}
} }
return result.toString(); return result.toString();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment