Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SnakeYAML for manual YAML Parser #3191

Merged
merged 8 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Consolidate ApkInfo tests in the package brut.androlib.apk, unify int…
…erface YamlReader and add ApkInfoSerializationTest read -> write -> read test
  • Loading branch information
sv99 committed Jul 26, 2023
commit 23459d3c5b568027d2adf03d92670e07cdf9ee22
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.yaml.snakeyaml.introspector.PropertyUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -182,7 +183,8 @@ public static ApkInfo load(InputStream is) throws AndrolibException {
// return getYaml().loadAs(is, ApkInfo.class);
YamlReader reader = new YamlReader(is);
ApkInfo apkInfo = new ApkInfo();
return reader.readRoot(apkInfo);
reader.readRoot(apkInfo);
return apkInfo;
}

// public static ApkInfo load(File appDir)
Expand Down Expand Up @@ -210,13 +212,13 @@ public static ApkInfo load(File appDir)
@Override
public void readItem(YamlReader reader) throws AndrolibException {
YamlLine line = reader.getLine();
switch (line.key) {
switch (line.getKey()) {
case "version": {
this.version = line.getValueString();
this.version = line.getValue();
break;
}
case "apkFileName": {
this.apkFileName = line.getValueString();
this.apkFileName = line.getValue();
break;
}
case "isFrameworkApk": {
Expand All @@ -229,7 +231,7 @@ public void readItem(YamlReader reader) throws AndrolibException {
break;
}
case "sdkInfo": {
this.sdkInfo = reader.readMap();
reader.readMap(sdkInfo);
break;
}
case "packageInfo": {
Expand All @@ -256,11 +258,13 @@ public void readItem(YamlReader reader) throws AndrolibException {
break;
}
case "unknownFiles": {
this.unknownFiles = reader.readMap();
this.unknownFiles = new LinkedHashMap<>();
reader.readMap(unknownFiles);
break;
}
case "doNotCompress": {
this.doNotCompress = reader.readStringList();
this.doNotCompress = new ArrayList<>();
reader.readStringList(doNotCompress);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public class PackageInfo implements YamlSerializable {
@Override
public void readItem(YamlReader reader) throws AndrolibException {
YamlLine line = reader.getLine();
switch (line.key) {
switch (line.getKey()) {
case "forcedPackageId": {
forcedPackageId = line.getValueString();
forcedPackageId = line.getValue();
break;
}
case "renameManifestPackage": {
renameManifestPackage = line.getValueString();
renameManifestPackage = line.getValue();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import brut.androlib.exceptions.AndrolibException;

import java.util.ArrayList;
import java.util.List;

public class UsesFramework implements YamlSerializable {
Expand All @@ -27,13 +28,14 @@ public class UsesFramework implements YamlSerializable {
@Override
public void readItem(YamlReader reader) throws AndrolibException {
YamlLine line = reader.getLine();
switch (line.key) {
switch (line.getKey()) {
case "ids": {
ids = reader.readIntList();
ids = new ArrayList<>();
reader.readIntList(ids);
break;
}
case "tag": {
tag = line.getValueString();
tag = line.getValue();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public class VersionInfo implements YamlSerializable {
@Override
public void readItem(YamlReader reader) throws AndrolibException {
YamlLine line = reader.getLine();
switch (line.key) {
switch (line.getKey()) {
case "versionCode": {
versionCode = line.getValueString();
versionCode = line.getValue();
break;
}
case "versionName": {
versionName = line.getValueString();
versionName = line.getValue();
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
public class YamlLine {

public int indent = 0;
public String key = "";
public String value = "";
private String key = "";
private String value = "";
public boolean isComment;
public boolean isEmpty;
public boolean hasColon;
Expand Down Expand Up @@ -59,7 +59,11 @@ public YamlLine(String line) {
}
}

public String getValueString() {
public static String unescape(String value) {
iBotPeaches marked this conversation as resolved.
Show resolved Hide resolved
return YamlStringEscapeUtils.unescapeString(value);
}

public String getValue() {
if (value.equals("null"))
return null;
String res = YamlStringEscapeUtils.unescapeString(value);
Expand All @@ -69,7 +73,7 @@ public String getValueString() {
return res;
}

public String getKeyString() {
public String getKey() {
String res = YamlStringEscapeUtils.unescapeString(key);
// remove quotation marks
res = res.replaceAll("^\"|\"$", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ interface Updater<T> {
/**
* Read root object from start to end
*/
public <T extends YamlSerializable> T readRoot(T obj) throws AndrolibException {
public <T extends YamlSerializable> void readRoot(T obj) throws AndrolibException {
if (isEnd())
return obj;
return;
int objIndent = 0;
skipInsignificant();
while (true) {
if (isEnd())
return obj;
return;
YamlLine line = getLine();
// skip don't checked line or lines with other indent
if (objIndent != line.indent || !line.hasColon) {
Expand All @@ -108,11 +108,11 @@ public <T extends YamlSerializable> T readRoot(T obj) throws AndrolibException {
* The object data should be placed on the next line
* and have indent.
*/
public <T> T readObject(T obj,
public <T> void readObject(T obj,
Checker check,
Updater<T> updater) throws AndrolibException {
if (isEnd())
return obj;
return;
int prevIndent = getIndent();
// detect indent for the object data
nextLine();
Expand All @@ -122,24 +122,23 @@ public <T> T readObject(T obj,
// otherwise stop reading
if (objIndent <= prevIndent || !check.check(line)) {
pushLine();
return obj;
return;
}
updater.update(obj, this);
while (nextLine()) {
if (isEnd())
return obj;
return;
line = getLine();
if (objIndent != line.indent || !check.check(line)) {
pushLine();
return obj;
return;
}
updater.update(obj, this);
}
return obj;
}

<T extends YamlSerializable> T readObject(T obj) throws AndrolibException {
return readObject(obj,
<T extends YamlSerializable> void readObject(T obj) throws AndrolibException {
readObject(obj,
line -> line.hasColon,
YamlSerializable::readItem);
}
Expand All @@ -149,54 +148,51 @@ <T extends YamlSerializable> T readObject(T obj) throws AndrolibException {
* The list data should be placed on the next line.
* Data should have same indent. May by same with name.
*/
public <T> List<T> readList(List<T> list,
public <T> void readList(List<T> list,
Updater<List<T>> updater) throws AndrolibException {
if (isEnd())
return list;
return;
int listIndent = getIndent();
nextLine();
int dataIndent = getIndent();
while (true) {
if (isEnd())
return list;
return;
// check incorrect data indent
if (dataIndent < listIndent) {
pushLine();
return list;
return;
}
YamlLine line = getLine();
if (dataIndent != line.indent || !line.isItem) {
pushLine();
return list;
return;
}
updater.update(list, this);
nextLine();
}
}

public List<String> readStringList() throws AndrolibException {
List<String> list = new ArrayList<>();
return readList(list,
public void readStringList(List<String> list) throws AndrolibException {
readList(list,
(items, reader) -> {
items.add(reader.getLine().getValueString());
items.add(reader.getLine().getValue());
});
};

public List<Integer> readIntList() throws AndrolibException {
List<Integer> list = new ArrayList<>();
return readList(list,
public void readIntList(List<Integer> list) throws AndrolibException {
readList(list,
(items, reader) -> {
items.add(reader.getLine().getValueInt());
});
};

public Map<String, String> readMap() throws AndrolibException {
Map<String, String> map = new LinkedHashMap<>();
return readObject(map,
public void readMap(Map<String, String> map) throws AndrolibException {
readObject(map,
line -> line.hasColon,
(items, reader) -> {
YamlLine line = reader.getLine();
items.put(line.getKeyString(), line.getValueString());
items.put(line.getKey(), line.getValue());
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.nio.charset.StandardCharsets;
import java.util.*;

public class YamlWriter implements AutoCloseable {
public class YamlWriter implements Closeable {

private int mIndent = 0;
private final PrintWriter mWriter;
Expand All @@ -16,7 +16,7 @@ public YamlWriter(OutputStream out) {
}

@Override
public void close() throws Exception {
public void close() throws IOException {
mWriter.close();
}

Expand All @@ -31,6 +31,10 @@ public String getIndentString() {
return String.join("", Collections.nCopies(mIndent, " "));
}

public static String escape(String value) {
return YamlStringEscapeUtils.escapeString(value);
}

public void nextIndent() {
mIndent += 2;
}
Expand All @@ -46,24 +50,23 @@ public void writeIndent() {

public void writeInt(String key, int value) {
writeIndent();
mWriter.println(key + ": " + value);
mWriter.println(escape(key) + ": " + value);
}

public void writeBool(String key, boolean value) {
writeIndent();
String val = value ? "true": "false";
mWriter.println(key + ": " + val);
mWriter.println(escape(key) + ": " + val);
}

public void writeString(String key, String value, boolean quoted) {
writeIndent();
if (Objects.isNull(value)) {
mWriter.println(key + ": null");
mWriter.println(escape(key) + ": null");
} else {
value = YamlStringEscapeUtils.escapeString(value);
if (quoted)
value = QUOTE + value + QUOTE;
mWriter.println(YamlStringEscapeUtils.escapeString(key) + ": " + value);
mWriter.println(escape(key) + ": " + escape(value));
}
}

Expand All @@ -75,7 +78,7 @@ public <T> void writeList(String key, List<T> list) {
if (Objects.isNull(list))
return;
writeIndent();
mWriter.println(key + ":");
mWriter.println(escape(key) + ":");
for (T item: list) {
writeIndent();
mWriter.println("- " + item);
Expand All @@ -86,7 +89,7 @@ public <K, V> void writeCommonMap(String key, Map<K, V> map) {
if (Objects.isNull(map))
return;
writeIndent();
mWriter.println(key + ":");
mWriter.println(escape(key) + ":");
nextIndent();
for (K mapKey: map.keySet()) {
writeIndent();
Expand All @@ -99,15 +102,10 @@ public void writeStringMap(String key, Map<String, String> map) {
if (Objects.isNull(map))
return;
writeIndent();
mWriter.println(key + ":");
mWriter.println(escape(key) + ":");
nextIndent();
for (String mapKey: map.keySet()) {
writeString(mapKey, map.get(mapKey));
// writeIndent();
// String val = map.get(mapKey);
// mapKey = YamlStringEscapeUtils.escapeString(mapKey);
// val = YamlStringEscapeUtils.escapeString(val);
// mWriter.println(mapKey + ": " + val);
}
prevIndent();
}
Expand All @@ -116,7 +114,7 @@ public <T extends YamlSerializable> void writeObject(String key, T obj) {
if (Objects.isNull(obj))
return;
writeIndent();
mWriter.println(key + ":");
mWriter.println(escape(key) + ":");
nextIndent();
obj.write(this);
prevIndent();
Expand Down
Loading