mirror of
https://github.com/revanced/ARSCLib.git
synced 2025-05-01 22:54:26 +02:00
use xml library
This commit is contained in:
parent
83359ff5d8
commit
56d9d75878
@ -21,14 +21,13 @@ import com.reandroid.lib.arsc.group.EntryGroup;
|
|||||||
import com.reandroid.lib.arsc.pool.SpecStringPool;
|
import com.reandroid.lib.arsc.pool.SpecStringPool;
|
||||||
import com.reandroid.lib.json.JSONArray;
|
import com.reandroid.lib.json.JSONArray;
|
||||||
import com.reandroid.lib.json.JSONObject;
|
import com.reandroid.lib.json.JSONObject;
|
||||||
|
import com.reandroid.xml.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class ResourceIds {
|
public class ResourceIds {
|
||||||
private final Table mTable;
|
private final Table mTable;
|
||||||
public ResourceIds(Table table){
|
public ResourceIds(Table table){
|
||||||
this.mTable=table;
|
this.mTable=table;
|
||||||
@ -85,8 +84,11 @@ public class ResourceIds {
|
|||||||
public void fromXml(InputStream inputStream) throws IOException {
|
public void fromXml(InputStream inputStream) throws IOException {
|
||||||
mTable.fromXml(inputStream);
|
mTable.fromXml(inputStream);
|
||||||
}
|
}
|
||||||
public void fromXml(Reader reader) throws IOException {
|
public void fromXml(XMLDocument xmlDocument) throws IOException {
|
||||||
mTable.fromXml(reader);
|
mTable.fromXml(xmlDocument);
|
||||||
|
}
|
||||||
|
public XMLDocument toXMLDocument(){
|
||||||
|
return mTable.toXMLDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Table implements Comparator<Table.Package>{
|
public static class Table implements Comparator<Table.Package>{
|
||||||
@ -115,9 +117,9 @@ public class ResourceIds {
|
|||||||
}
|
}
|
||||||
this.packageMap.put(pkg.id, pkg);
|
this.packageMap.put(pkg.id, pkg);
|
||||||
}
|
}
|
||||||
public void add(Package.Type.Entry entry){
|
public Package add(Package.Type.Entry entry){
|
||||||
if(entry==null){
|
if(entry==null){
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
byte pkgId=entry.getPackageId();
|
byte pkgId=entry.getPackageId();
|
||||||
Package pkg = packageMap.get(pkgId);
|
Package pkg = packageMap.get(pkgId);
|
||||||
@ -126,6 +128,7 @@ public class ResourceIds {
|
|||||||
packageMap.put(pkgId, pkg);
|
packageMap.put(pkgId, pkg);
|
||||||
}
|
}
|
||||||
pkg.add(entry);
|
pkg.add(entry);
|
||||||
|
return pkg;
|
||||||
}
|
}
|
||||||
public Package.Type.Entry getEntry(int resourceId){
|
public Package.Type.Entry getEntry(int resourceId){
|
||||||
byte packageId = (byte) ((resourceId>>24) & 0xff);
|
byte packageId = (byte) ((resourceId>>24) & 0xff);
|
||||||
@ -191,6 +194,15 @@ public class ResourceIds {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public XMLDocument toXMLDocument(){
|
||||||
|
XMLDocument xmlDocument = new XMLDocument();
|
||||||
|
XMLElement documentElement = new XMLElement("resources");
|
||||||
|
for(Package pkg:listPackages()){
|
||||||
|
pkg.toXMLElements(documentElement);
|
||||||
|
}
|
||||||
|
xmlDocument.setDocumentElement(documentElement);
|
||||||
|
return xmlDocument;
|
||||||
|
}
|
||||||
public void writeXml(File file) throws IOException {
|
public void writeXml(File file) throws IOException {
|
||||||
File dir=file.getParentFile();
|
File dir=file.getParentFile();
|
||||||
if(dir!=null && !dir.exists()){
|
if(dir!=null && !dir.exists()){
|
||||||
@ -205,35 +217,29 @@ public class ResourceIds {
|
|||||||
outputStream.close();
|
outputStream.close();
|
||||||
}
|
}
|
||||||
public void writeXml(Writer writer) throws IOException {
|
public void writeXml(Writer writer) throws IOException {
|
||||||
writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
XMLDocument xmlDocument=toXMLDocument();
|
||||||
writer.write("\n<resources>");
|
xmlDocument.write(writer, false);
|
||||||
for(Package pkg:listPackages()){
|
|
||||||
pkg.writeXml(writer);
|
|
||||||
}
|
|
||||||
writer.write("\n</resources>");
|
|
||||||
writer.flush();
|
|
||||||
}
|
}
|
||||||
public void fromXml(File file) throws IOException {
|
public void fromXml(File file) throws IOException {
|
||||||
FileInputStream inputStream=new FileInputStream(file);
|
FileInputStream inputStream=new FileInputStream(file);
|
||||||
fromXml(inputStream);
|
fromXml(inputStream);
|
||||||
}
|
|
||||||
public void fromXml(InputStream inputStream) throws IOException {
|
|
||||||
InputStreamReader reader=new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
|
||||||
fromXml(reader);
|
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
}
|
}
|
||||||
public void fromXml(Reader reader) throws IOException {
|
public void fromXml(InputStream inputStream) throws IOException {
|
||||||
BufferedReader bufferedReader;
|
try {
|
||||||
if(reader instanceof BufferedReader){
|
fromXml(XMLDocument.load(inputStream));
|
||||||
bufferedReader=(BufferedReader) reader;
|
} catch (XMLException ex) {
|
||||||
}else {
|
throw new IOException(ex.getMessage(), ex);
|
||||||
bufferedReader=new BufferedReader(reader);
|
|
||||||
}
|
}
|
||||||
String line;
|
}
|
||||||
while ((line=bufferedReader.readLine())!=null){
|
public void fromXml(XMLDocument xmlDocument) {
|
||||||
add(Package.Type.Entry.fromXml(line));
|
XMLElement documentElement = xmlDocument.getDocumentElement();
|
||||||
|
int count=documentElement.getChildesCount();
|
||||||
|
for(int i=0;i<count;i++){
|
||||||
|
XMLElement element=documentElement.getChildAt(i);
|
||||||
|
Package pkg = add(Package.Type.Entry.fromXml(element));
|
||||||
|
pkg.setPackageName(element.getCommentAt(0));
|
||||||
}
|
}
|
||||||
bufferedReader.close();
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public int compare(Package pkg1, Package pkg2) {
|
public int compare(Package pkg1, Package pkg2) {
|
||||||
@ -347,16 +353,40 @@ public class ResourceIds {
|
|||||||
public int compare(Type t1, Type t2) {
|
public int compare(Type t1, Type t2) {
|
||||||
return t1.compareTo(t2);
|
return t1.compareTo(t2);
|
||||||
}
|
}
|
||||||
public void writeXml(Writer writer) throws IOException {
|
public void toXMLElements(XMLElement documentElement){
|
||||||
writer.write("\n");
|
int count = documentElement.getChildesCount();
|
||||||
if(this.name!=null){
|
|
||||||
writer.write(" <!-- packageName=\"");
|
|
||||||
writer.write(this.name);
|
|
||||||
writer.write("\" -->");
|
|
||||||
}
|
|
||||||
for(Type type:listTypes()){
|
for(Type type:listTypes()){
|
||||||
type.writeXml(writer);
|
type.toXMLElements(documentElement);
|
||||||
}
|
}
|
||||||
|
XMLElement firstElement = documentElement.getChildAt(count);
|
||||||
|
if(firstElement!=null){
|
||||||
|
XMLComment comment = new XMLComment(
|
||||||
|
"packageName=\""+this.name+"\"");
|
||||||
|
firstElement.addComment(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void setPackageName(XMLComment xmlComment){
|
||||||
|
if(xmlComment==null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String pkgName = xmlComment.getCommentText();
|
||||||
|
if(pkgName==null || !pkgName.contains("packageName")){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int i = pkgName.indexOf('"');
|
||||||
|
if(i>0){
|
||||||
|
i++;
|
||||||
|
pkgName=pkgName.substring(i);
|
||||||
|
}else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
i = pkgName.indexOf('"');
|
||||||
|
if(i>0){
|
||||||
|
pkgName=pkgName.substring(0, i);
|
||||||
|
}else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.name=pkgName.trim();
|
||||||
}
|
}
|
||||||
public List<Package.Type.Entry> listEntries(){
|
public List<Package.Type.Entry> listEntries(){
|
||||||
List<Package.Type.Entry> results=new ArrayList<>(countEntries());
|
List<Package.Type.Entry> results=new ArrayList<>(countEntries());
|
||||||
@ -507,10 +537,9 @@ public class ResourceIds {
|
|||||||
jsonObject.put("entries", jsonArray);
|
jsonObject.put("entries", jsonArray);
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
public void writeXml(Writer writer) throws IOException {
|
public void toXMLElements(XMLElement documentElement){
|
||||||
for(Entry entry:listEntries()){
|
for(Entry entry:listEntries()){
|
||||||
writer.write("\n ");
|
documentElement.addChild(entry.toXMLElement());
|
||||||
entry.writeXml(writer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public List<Entry> listEntries(){
|
public List<Entry> listEntries(){
|
||||||
@ -642,38 +671,17 @@ public class ResourceIds {
|
|||||||
jsonObject.put("name", getName());
|
jsonObject.put("name", getName());
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
public String toXml(){
|
public XMLElement toXMLElement(){
|
||||||
StringWriter writer=new StringWriter();
|
XMLElement element=new XMLElement();
|
||||||
try {
|
element.setResourceId(getResourceId());
|
||||||
writeXml(writer);
|
element.addAttribute(new XMLAttribute("id", getHexId()));
|
||||||
writer.flush();
|
element.addAttribute(new XMLAttribute("type", getTypeName()));
|
||||||
writer.close();
|
element.addAttribute(new XMLAttribute("name", getName()));
|
||||||
} catch (IOException ignored) {
|
return element;
|
||||||
}
|
|
||||||
return writer.toString();
|
|
||||||
}
|
|
||||||
public void writeXml(Writer writer) throws IOException {
|
|
||||||
writer.write("<public");
|
|
||||||
writer.write(" id=\"");
|
|
||||||
writer.write(getHexId());
|
|
||||||
writer.write("\"");
|
|
||||||
String tn=getTypeName();
|
|
||||||
if(tn !=null){
|
|
||||||
writer.append(" type=\"");
|
|
||||||
writer.append(tn);
|
|
||||||
writer.append("\"");
|
|
||||||
}
|
|
||||||
String n=getName();
|
|
||||||
if(n!=null){
|
|
||||||
writer.write(" name=\"");
|
|
||||||
writer.append(n);
|
|
||||||
writer.append("\"");
|
|
||||||
}
|
|
||||||
writer.append("/>");
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return toXml();
|
return toXMLElement().toText(false);
|
||||||
}
|
}
|
||||||
public static Entry fromEntryGroup(EntryGroup entryGroup){
|
public static Entry fromEntryGroup(EntryGroup entryGroup){
|
||||||
return new Entry(entryGroup.getResourceId(),
|
return new Entry(entryGroup.getResourceId(),
|
||||||
@ -685,42 +693,12 @@ public class ResourceIds {
|
|||||||
jsonObject.optString("type", null),
|
jsonObject.optString("type", null),
|
||||||
jsonObject.getString("name"));
|
jsonObject.getString("name"));
|
||||||
}
|
}
|
||||||
public static Entry fromXml(String xmlElement){
|
public static Entry fromXml(XMLElement element){
|
||||||
String element=xmlElement;
|
return new Entry(
|
||||||
element=element.replaceAll("[\n\r\t]+", " ");
|
Integer.decode(element.getAttributeValue("id")),
|
||||||
element=element.trim();
|
element.getAttributeValue("type"),
|
||||||
String start="<public ";
|
element.getAttributeValue("name"));
|
||||||
if(!element.startsWith(start) || !element.endsWith(">")){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
element=element.substring(start.length()).trim();
|
|
||||||
Pattern pattern=PATTERN;
|
|
||||||
int id=0;
|
|
||||||
String type=null;
|
|
||||||
String name=null;
|
|
||||||
Matcher matcher=pattern.matcher(element);
|
|
||||||
while (matcher.find()){
|
|
||||||
String attr=matcher.group(1).toLowerCase();
|
|
||||||
String value=matcher.group(2);
|
|
||||||
element=matcher.group(3);
|
|
||||||
if(attr.equals("id")){
|
|
||||||
id=Integer.decode(value);
|
|
||||||
}else if(attr.equals("name")){
|
|
||||||
name=value;
|
|
||||||
}else if(attr.equals("type")){
|
|
||||||
type=value;
|
|
||||||
}
|
|
||||||
matcher= pattern.matcher(element);
|
|
||||||
}
|
|
||||||
if(id==0){
|
|
||||||
throw new DuplicateException("Missing id: "+xmlElement);
|
|
||||||
}
|
|
||||||
if(name==null){
|
|
||||||
throw new DuplicateException("Missing name: "+xmlElement);
|
|
||||||
}
|
|
||||||
return new Entry(id, type, name);
|
|
||||||
}
|
}
|
||||||
private static final Pattern PATTERN=Pattern.compile("^\\s*([^\\s=\"]+)\\s*=\\s*\"([^\"]+)\"(.*)$");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user