cleanup document related functions

This commit is contained in:
Connor Tumbleson 2013-11-15 12:08:21 -06:00
parent 497b2f9e11
commit 8d0801e6db

View File

@ -155,11 +155,7 @@ final public class AndrolibResources {
// change application:debug to true // change application:debug to true
try { try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory Document doc = loadDocument(filePath);
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filePath.toString());
Node application = doc.getElementById("application"); Node application = doc.getElementById("application");
// load attr // load attr
@ -171,15 +167,7 @@ final public class AndrolibResources {
attr.removeNamedItem("debug"); attr.removeNamedItem("debug");
} }
// save manifest saveDocument(filePath, doc);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
} }
@ -188,22 +176,19 @@ final public class AndrolibResources {
public void adjust_package_manifest(ResTable resTable, String filePath) public void adjust_package_manifest(ResTable resTable, String filePath)
throws AndrolibException { throws AndrolibException {
// check if packages different, and that package is not equal to // check if packages different, and that package is not equal to "android"
// "android"
Map<String, String> packageInfo = resTable.getPackageInfo(); Map<String, String> packageInfo = resTable.getPackageInfo();
if ((packageInfo.get("cur_package").equalsIgnoreCase(packageInfo.get("orig_package")) || String currentPackage = packageInfo.get("cur_package");
("android".equalsIgnoreCase(packageInfo.get("cur_package")) || String originalPackage = packageInfo.get("orig_package");
("com.htc".equalsIgnoreCase(packageInfo.get("cur_package")))))) {
if (currentPackage.equalsIgnoreCase(originalPackage) || "android".equalsIgnoreCase(currentPackage)
|| "com.htc".equalsIgnoreCase(currentPackage)) {
LOGGER.info("Regular manifest package..."); LOGGER.info("Regular manifest package...");
} else { } else {
try { try {
LOGGER.info("Renamed manifest package found! Fixing..."); LOGGER.info("Renamed manifest package found! Fixing...");
DocumentBuilderFactory docFactory = DocumentBuilderFactory Document doc = loadDocument(filePath);
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filePath.toString());
// Get the manifest line // Get the manifest line
Node manifest = doc.getFirstChild(); Node manifest = doc.getFirstChild();
@ -212,17 +197,9 @@ final public class AndrolibResources {
NamedNodeMap attr = manifest.getAttributes(); NamedNodeMap attr = manifest.getAttributes();
Node nodeAttr = attr.getNamedItem("package"); Node nodeAttr = attr.getNamedItem("package");
mPackageRenamed = nodeAttr.getNodeValue(); mPackageRenamed = nodeAttr.getNodeValue();
nodeAttr.setNodeValue(packageInfo.get("cur_package")); nodeAttr.setNodeValue(currentPackage);
// re-save manifest. saveDocument(filePath, doc);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
} }
@ -235,12 +212,8 @@ final public class AndrolibResources {
File f = new File(filePath); File f = new File(filePath);
if (f.exists()) { if (f.exists()) {
// remove versionCode and versionName
try { try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); Document doc = loadDocument(filePath);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filePath.toString());
Node manifest = doc.getFirstChild(); Node manifest = doc.getFirstChild();
// load attr // load attr
@ -255,21 +228,34 @@ final public class AndrolibResources {
if (vName != null) { if (vName != null) {
attr.removeNamedItem("android:versionName"); attr.removeNamedItem("android:versionName");
} }
saveDocument(filePath, doc);
// save manifest
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) { } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
} }
} }
} }
private Document loadDocument(String filename)
throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filename);
return doc;
}
private void saveDocument(String filename, Document doc)
throws IOException, SAXException, ParserConfigurationException, TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filename));
transformer.transform(source, result);
}
public void decode(ResTable resTable, ExtFile apkFile, File outDir) public void decode(ResTable resTable, ExtFile apkFile, File outDir)
throws AndrolibException { throws AndrolibException {
Duo<ResFileDecoder, AXmlResourceParser> duo = getResFileDecoder(); Duo<ResFileDecoder, AXmlResourceParser> duo = getResFileDecoder();