Skip to content

Commit

Permalink
direct serialize xml to bin xml #18
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Jun 19, 2023
1 parent fffe5dc commit f7a4f05
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 69 deletions.
30 changes: 17 additions & 13 deletions src/main/java/com/reandroid/apk/ApkModuleXmlEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
package com.reandroid.apk;

import com.reandroid.apk.xmlencoder.EncodeMaterials;
import com.reandroid.apk.xmlencoder.XMLEncodeSource;
import com.reandroid.apk.xmlencoder.XMLParseEncodeSource;
import com.reandroid.apk.xmlencoder.XMLTableBlockEncoder;
import com.reandroid.archive.FileInputSource;
import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.chunk.TableBlock;
import com.reandroid.arsc.chunk.xml.AndroidManifestBlock;
import com.reandroid.arsc.value.Entry;
import com.reandroid.xml.source.XMLFileSource;
import com.reandroid.xml.source.XMLSource;
import com.reandroid.xml.source.XMLFileParserSource;
import com.reandroid.xml.source.XMLParserSource;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -83,10 +83,11 @@ private void encodeManifestXml(File mainDirectory) {
encodeMaterials.setCurrentPackage(packageBlock);
tableBlock.setCurrentPackage(packageBlock);
}
XMLSource xmlSource =
new XMLFileSource(AndroidManifestBlock.FILE_NAME, file);
XMLEncodeSource xmlEncodeSource =
new XMLEncodeSource(encodeMaterials, xmlSource);
XMLParserSource xmlSource =
new XMLFileParserSource(AndroidManifestBlock.FILE_NAME, file);
XMLParseEncodeSource xmlEncodeSource =
new XMLParseEncodeSource(tableBlock.pickOne(), xmlSource);
xmlEncodeSource.setApkLogger(getApkLogger());
getApkModule().add(xmlEncodeSource);
}
private void scanResFilesDirectory(File mainDirectory) {
Expand All @@ -104,13 +105,16 @@ private void encodeResFile(File resFilesDirectory, File file){
String path = ApkUtil.toArchivePath(resFilesDirectory, file);
logVerbose(path);
Entry entry = getEntry(path);
EncodeMaterials encodeMaterials = getEncodeMaterials();
if(entry == null){
logMessage("Un registered file: " + file);
return;
}
if(file.getName().endsWith(".xml")){
XMLSource xmlSource =
new XMLFileSource(path, file);
XMLEncodeSource xmlEncodeSource =
new XMLEncodeSource(encodeMaterials, xmlSource);
xmlEncodeSource.setEntry(entry);
XMLParserSource xmlSource =
new XMLFileParserSource(path, file);
XMLParseEncodeSource xmlEncodeSource =
new XMLParseEncodeSource(entry.getPackageBlock(), xmlSource);
xmlEncodeSource.setApkLogger(getApkLogger());
getApkModule().add(xmlEncodeSource);
}else {
FileInputSource inputSource = new FileInputSource(file, path);
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/reandroid/apk/xmlencoder/FilePathEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.reandroid.apk.xmlencoder;

import com.reandroid.apk.APKLogger;
import com.reandroid.archive.APKArchive;
import com.reandroid.archive.FileInputSource;
import com.reandroid.archive.InputSource;
import com.reandroid.apk.ApkUtil;
import com.reandroid.apk.UncompressedFiles;
import com.reandroid.arsc.model.ResourceEntry;
import com.reandroid.arsc.value.Entry;
import com.reandroid.xml.source.XMLFileSource;
import com.reandroid.xml.source.XMLSource;
import com.reandroid.xml.source.XMLFileParserSource;
import com.reandroid.xml.source.XMLParserSource;

import java.io.File;
import java.util.List;
Expand All @@ -32,6 +33,7 @@ public class FilePathEncoder {
private final EncodeMaterials materials;
private APKArchive apkArchive;
private UncompressedFiles uncompressedFiles;
private APKLogger mLogger;
public FilePathEncoder(EncodeMaterials encodeMaterials){
this.materials =encodeMaterials;
}
Expand Down Expand Up @@ -92,8 +94,10 @@ private InputSource createRawFileInputSource(String path, File resFile){
return new FileInputSource(resFile, path);
}
private InputSource createXMLEncodeInputSource(String path, File resFile){
XMLSource xmlSource = new XMLFileSource(path, resFile);
return new XMLEncodeSource(materials, xmlSource);
XMLParserSource xmlSource = new XMLFileParserSource(path, resFile);
XMLParseEncodeSource encodeSource = new XMLParseEncodeSource(materials.getCurrentPackage(), xmlSource);
encodeSource.setApkLogger(mLogger);
return encodeSource;
}
private boolean isXmlFile(File resFile){
String name=resFile.getName();
Expand All @@ -113,4 +117,7 @@ private void addUncompressedFiles(String path){
uncompressedFiles.addPath(path);
}
}
public void setApkLogger(APKLogger logger){
this.mLogger = logger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.reandroid.apk.xmlencoder;

import com.reandroid.apk.APKLogger;
import com.reandroid.apk.CrcOutputStream;
import com.reandroid.archive.ByteInputSource;
import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.chunk.xml.ResXmlDocument;
import com.reandroid.arsc.chunk.xml.ResXmlPullSerializer;
import com.reandroid.arsc.util.IOUtil;
import com.reandroid.xml.XmlParserToSerializer;
import com.reandroid.xml.source.XMLParserSource;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.OutputStream;

public class XMLParseEncodeSource extends ByteInputSource {
private final PackageBlock packageBlock;
private final XMLParserSource parserSource;
private ResXmlDocument mResXmlDocument;
private APKLogger mLogger;

public XMLParseEncodeSource(PackageBlock packageBlock, XMLParserSource parserSource) {
super(new byte[0], parserSource.getPath());
this.packageBlock = packageBlock;
this.parserSource = parserSource;
}
@Override
public long getLength() throws IOException {
return getResXmlDocument().countBytes();
}
@Override
public long getCrc() throws IOException{
ResXmlDocument resXmlDocument = getResXmlDocument();
CrcOutputStream outputStream=new CrcOutputStream();
resXmlDocument.writeBytes(outputStream);
return outputStream.getCrcValue();
}
@Override
public long write(OutputStream outputStream) throws IOException {
return getResXmlDocument().writeBytes(outputStream);
}
@Override
public byte[] getBytes() {
try {
return getResXmlDocument().getBytes();
} catch (IOException ignored) {
}
//should not reach here
return new byte[0];
}
@Override
public void disposeInputSource(){
mResXmlDocument = null;
}

public PackageBlock getPackageBlock() {
return packageBlock;
}
public XMLParserSource getParserSource() {
return parserSource;
}
public ResXmlDocument getResXmlDocument() throws IOException{
if(mResXmlDocument == null){
try {
mResXmlDocument = encode();
} catch (XmlPullParserException ex) {
throw new IOException(ex.getMessage());
}
}
return mResXmlDocument;
}
private ResXmlDocument encode() throws XmlPullParserException, IOException {
logVerbose("Encoding: " + getParserSource().getPath());
XmlPullParser parser = getParserSource().getParser();
ResXmlPullSerializer serializer = new ResXmlPullSerializer();
serializer.setCurrentPackage(getPackageBlock());
XmlParserToSerializer parserToSerializer = new XmlParserToSerializer(parser, serializer);
parserToSerializer.write();
IOUtil.close(parser);
return serializer.getResultDocument();
}
public void setApkLogger(APKLogger logger){
this.mLogger = logger;
}
private void logVerbose(String msg){
APKLogger logger = this.mLogger;
if(logger != null){
logger.logVerbose(msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private void encodeValues(List<File> pubXmlFileList) throws XMLException {
File resDir = toResDirectory(pubXmlFile);
encodeResDir(resDir);
FilePathEncoder filePathEncoder = new FilePathEncoder(encodeMaterials);
filePathEncoder.setApkLogger(getApkLogger());
filePathEncoder.setApkArchive(getApkModule().getApkArchive());
filePathEncoder.setUncompressedFiles(getApkModule().getUncompressedFiles());
filePathEncoder.encodePackageResDir(resDir);
Expand Down
Loading

0 comments on commit f7a4f05

Please sign in to comment.