ResFileDecoder: rewritten file type recognition algorithm.

This commit is contained in:
Ryszard Wiśniewski 2010-06-01 13:09:46 +02:00
parent e4ecd30a27
commit 0aa2015fda
2 changed files with 34 additions and 47 deletions

View File

@ -19,7 +19,6 @@ package brut.androlib.res;
import brut.androlib.AndrolibException;
import brut.androlib.res.data.*;
import brut.androlib.res.data.value.ResFileValue;
import brut.androlib.res.data.value.ResXmlSerializable;
import brut.androlib.res.decoder.*;
import brut.androlib.res.util.ExtFile;
@ -59,7 +58,7 @@ final public class AndrolibResources {
attrDecoder.setCurrentPackage(
resTable.listMainPackages().iterator().next());
Directory in, out;
Directory in, out, out9Patch;
try {
in = apkFile.getDirectory();
out = new FileDirectory(outDir);
@ -67,18 +66,18 @@ final public class AndrolibResources {
fileDecoder.decode(
in, "AndroidManifest.xml", out, "AndroidManifest.xml", "xml");
out9Patch = out.createDir("9patch/res");
in = in.getDir("res");
out = out.createDir("res");
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
File out9PatchDir = new File(outDir, "9patch/res");
ExtMXSerializer xmlSerializer = getResXmlSerializer();
for (ResPackage pkg : resTable.listMainPackages()) {
attrDecoder.setCurrentPackage(pkg);
for (ResResource res : pkg.listFiles()) {
decodeFile(res, in, out, fileDecoder, out9PatchDir);
fileDecoder.decode(res, in, out, out9Patch);
}
for (ResValuesFile valuesFile : pkg.listValuesFiles()) {
generateValuesFile(valuesFile, out, xmlSerializer);
@ -188,31 +187,6 @@ final public class AndrolibResources {
return serial;
}
private void decodeFile(ResResource res, Directory in, Directory out,
ResFileDecoder fileDecoder, File out9PatchDir)
throws AndrolibException {
ResFileValue fileValue = (ResFileValue) res.getValue();
String inName = fileValue.getStrippedPath();
String outName = res.getFilePath();
fileDecoder.decode(in, inName, out, outName);
if (inName.endsWith(".9.png")) {
File out9PatchFile = new File(
out9PatchDir, outName + ".png");
out9PatchFile.getParentFile().mkdirs();
try {
BrutIO.copyAndClose(in.getFileInput(inName),
new FileOutputStream(out9PatchFile));
} catch (IOException ex) {
throw new AndrolibException(ex);
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
}
}
}
private void generateValuesFile(ResValuesFile valuesFile, Directory out,
XmlSerializer serial) throws AndrolibException {
try {

View File

@ -18,8 +18,11 @@
package brut.androlib.res.decoder;
import brut.androlib.AndrolibException;
import brut.androlib.res.data.ResResource;
import brut.androlib.res.data.value.ResFileValue;
import brut.directory.Directory;
import brut.directory.DirectoryException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -36,32 +39,42 @@ public class ResFileDecoder {
this.mDecoders = decoders;
}
public void decode(Directory inDir, String inFileName, Directory outDir,
String outResName) throws AndrolibException {
String ext = "";
public void decode(ResResource res, Directory inDir, Directory outDir,
Directory out9Patch) throws AndrolibException {
ResFileValue fileValue = (ResFileValue) res.getValue();
String inFileName = fileValue.getStrippedPath();
String outResName = res.getFilePath();
String typeName = res.getResSpec().getType().getName();
String ext = null;
String outFileName;
int extPos = inFileName.lastIndexOf(".");
if (extPos != -1) {
if (extPos == -1) {
outFileName = outResName;
} else {
ext = inFileName.substring(extPos);
outFileName = outResName + ext;
}
if (inFileName.startsWith("raw/")) {
decode(inDir, inFileName, outDir, outResName + ext, "raw");
if (typeName.equals("raw")) {
decode(inDir, inFileName, outDir, outFileName, "raw");
return;
}
if (inFileName.endsWith(".9.png")) {
decode(inDir, inFileName, outDir, outResName + ".png", "raw");
return;
if (typeName.equals("drawable")) {
if (inFileName.toLowerCase().endsWith(".9.png")) {
outFileName = outResName + ".png";
decode(inDir, inFileName, outDir, outFileName, "raw");
decode(inDir, inFileName, out9Patch, outFileName, "raw");
return;
}
if (! ext.equals(".xml")) {
decode(inDir, inFileName, outDir, outFileName, "raw");
return;
}
}
if (inFileName.endsWith(".xml")) {
decode(inDir, inFileName, outDir, outResName + ".xml", "xml");
return;
}
// if (inFileName.endsWith(".html")) {
// decode(inDir, inFileName, outDir, outResName + ".html", "xml");
// return;
// }
decode(inDir, inFileName, outDir, outResName + ext, "raw");
decode(inDir, inFileName, outDir, outFileName, "xml");
}
public void decode(Directory inDir, String inFileName, Directory outDir,