mirror of
https://github.com/revanced/Apktool.git
synced 2025-05-19 14:17:05 +02:00
ResValueFactory: string value parsing using regexes.
This commit is contained in:
parent
a469b8b0b3
commit
7200c2efb8
@ -25,6 +25,8 @@ import brut.androlib.res.jni.JniBagItem;
|
|||||||
import brut.androlib.res.jni.JniEntry;
|
import brut.androlib.res.jni.JniEntry;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
||||||
@ -36,26 +38,42 @@ public class ResValueFactory {
|
|||||||
this.mPackage = pakage_;
|
this.mPackage = pakage_;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResScalarValue factory(String string) {
|
public ResScalarValue factory(String string) throws AndrolibException {
|
||||||
if (string.isEmpty()) {
|
if (string.isEmpty()) {
|
||||||
return new ResStringValue(string);
|
return new ResStringValue(string);
|
||||||
}
|
}
|
||||||
|
Integer i;
|
||||||
|
|
||||||
char c = string.charAt(0);
|
char c = string.charAt(0);
|
||||||
if (c == '@' || c == '?') {
|
boolean theme = false;
|
||||||
return newReference(
|
if (c == '?') {
|
||||||
Integer.parseInt(string.substring(1)), c == '?');
|
c = '@';
|
||||||
|
theme = true;
|
||||||
}
|
}
|
||||||
if (c == '#') {
|
if (c == '@' || c == '#') {
|
||||||
return new ResColorValue(
|
i = parseInt(string.substring(1), true);
|
||||||
(int) Long.parseLong(string.substring(1), 16));
|
if (i != null) {
|
||||||
|
switch (c) {
|
||||||
|
case '@':
|
||||||
|
return newReference(i, theme);
|
||||||
|
case '#':
|
||||||
|
return new ResColorValue(i);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (string.startsWith("0x")) {
|
|
||||||
return new ResIntValue(
|
|
||||||
(int) Long.parseLong(string.substring(2), 16));
|
|
||||||
}
|
}
|
||||||
return new ResIntValue(Integer.parseInt(string));
|
Matcher m = resIdPattern.matcher(string.substring(1));
|
||||||
} catch (NumberFormatException ex) {}
|
if (m.matches()) {
|
||||||
|
ResPackage pkg = m.group(1) == null ? mPackage
|
||||||
|
: mPackage.getResTable().getPackage(m.group(1));
|
||||||
|
return newReference(pkg.getType(m.group(2))
|
||||||
|
.getResSpec(m.group(3)).getId().id, theme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i = parseInt(string);
|
||||||
|
if (i != null) {
|
||||||
|
return new ResIntValue(i);
|
||||||
|
}
|
||||||
|
|
||||||
return new ResStringValue(string);
|
return new ResStringValue(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +156,29 @@ public class ResValueFactory {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Integer parseInt(String s) {
|
||||||
|
return parseInt(s, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer parseInt(String s, boolean hex) {
|
||||||
|
if (s.startsWith("0x")) {
|
||||||
|
s = s.substring(2);
|
||||||
|
hex = true;
|
||||||
|
} else if (decPattern.matcher(s).matches()) {
|
||||||
|
return Integer.parseInt(s);
|
||||||
|
}
|
||||||
|
if (hex && hexPattern.matcher(s).matches()) {
|
||||||
|
return (int) Long.parseLong(s, 16);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final static Pattern decPattern = Pattern.compile("-?\\d+");
|
||||||
|
private final static Pattern hexPattern =
|
||||||
|
Pattern.compile("-?[0-9a-fA-F]{1,8}");
|
||||||
|
private final static Pattern resIdPattern =
|
||||||
|
Pattern.compile("\\+?(?:|(.+?):)(.+?)/(.+?)");
|
||||||
|
|
||||||
private final static int TYPE_NULL = 0x00;
|
private final static int TYPE_NULL = 0x00;
|
||||||
private final static int TYPE_REFERENCE = 0x01;
|
private final static int TYPE_REFERENCE = 0x01;
|
||||||
private final static int TYPE_ATTRIBUTE = 0x02;
|
private final static int TYPE_ATTRIBUTE = 0x02;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user