Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3 Output NameSpace format #4

Merged
merged 4 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions src/main/java/android/content/res/AXMLResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

/**
* Main AXMLResource object
Expand Down Expand Up @@ -162,19 +164,81 @@ public void print() {
log("%s", header.toXML(stringSection, resourceSection, 0));
Iterator<Chunk> iterator = chunks.iterator();
int indents = 0;
List<String> namespaceXmlList = new ArrayList<String>();

while (iterator.hasNext()) {
Chunk chunk = iterator.next();
if (chunk.getChunkType() == ChunkType.END_TAG) {
indents--;
}

if (chunk.getChunkType() == ChunkType.START_NAMESPACE) {
namespaceXmlList.add(chunk.toXML(stringSection, resourceSection, indents));
} else if (chunk.getChunkType() == ChunkType.END_NAMESPACE) {
// ignore
} else {
if (namespaceXmlList.isEmpty()) {
log("%s", chunk.toXML(stringSection, resourceSection, indents));
} else {
log("%s", appendNameSpace(chunk.toXML(stringSection, resourceSection, indents), namespaceXmlList));
namespaceXmlList.clear();
}
}

if (chunk.getChunkType() == ChunkType.START_TAG) {
indents++;
}
}

}

public String toXML() {
StringBuilder xmlStrbui = new StringBuilder();
xmlStrbui.append(header.toXML(stringSection, resourceSection, 0)).append('\n');
Iterator<Chunk> iterator = chunks.iterator();
int indents = 0;
List<String> namespaceXmlList = new ArrayList<String>();

while (iterator.hasNext()) {
Chunk chunk = iterator.next();
if (chunk.getChunkType() == ChunkType.END_TAG) {
indents--;
}
// if(chunk.getChunkType() == ChunkType.START_NAMESPACE)

log("%s", chunk.toXML(stringSection, resourceSection, indents));
if (chunk.getChunkType() == ChunkType.START_NAMESPACE) {
namespaceXmlList.add(chunk.toXML(stringSection, resourceSection, indents));
} else if (chunk.getChunkType() == ChunkType.END_NAMESPACE) {
// ignore
} else {
if (namespaceXmlList.isEmpty()) {
xmlStrbui.append(chunk.toXML(stringSection, resourceSection, indents)).append('\n');
} else {
xmlStrbui.append(appendNameSpace(chunk.toXML(stringSection, resourceSection, indents), namespaceXmlList)).append('\n');
namespaceXmlList.clear();
}
}

if (chunk.getChunkType() == ChunkType.START_TAG) {
indents++;
}
}
return xmlStrbui.toString();
}

private String appendNameSpace(String preChunkXml, List<String> namespaceXmlList) {
StringBuilder strbui = new StringBuilder(preChunkXml);
int index;
if ((index = strbui.indexOf("\n")) == -1 && (index = strbui.indexOf(" ")) == -1
&& (index = strbui.indexOf("/>")) == -1 && (index = strbui.indexOf(">")) == -1) {
throw new RuntimeException("Append name space fail. chunk xml: " + preChunkXml);
}
StringBuilder namespaceStrbui = new StringBuilder();
for (String namespaceXml : namespaceXmlList) {
namespaceStrbui.append("\n\t");
namespaceStrbui.append(namespaceXml);
}
strbui.insert(index, namespaceStrbui.toString());
return strbui.toString();
}

private static void log(String format, Object... arguments) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/android/content/res/TestAXMLResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -52,6 +58,27 @@ public void testPrinting() throws IOException {
underTest.print();
}

@Test
public void testToXml() throws IOException, ParserConfigurationException {
InputStream testStream = this.getClass().getClassLoader().getResourceAsStream(largeFromMalware);

underTest = new AXMLResource(testStream);
String xml = underTest.toXML();

try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
Node manifestNode = document.getFirstChild();
NamedNodeMap manifestNodeAttributes = manifestNode.getAttributes();
assertEquals("http://schemas.android.com/apk/res/android", manifestNodeAttributes.getNamedItem("xmlns:android").getNodeValue());
assertEquals("3133", manifestNodeAttributes.getNamedItem("android:versionCode").getNodeValue());
assertEquals("1.9.3", manifestNodeAttributes.getNamedItem("android:versionName").getNodeValue());
assertEquals("com.faithcomesbyhearing.android.pt.bibleis", manifestNodeAttributes.getNamedItem("package").getNodeValue());
} catch (SAXException e) {
// Is not xml
assertTrue(false);
}
}

@Test
public void testInsertApplicationAttribute() throws IOException {
InputStream testStream = this.getClass().getClassLoader().getResourceAsStream(largeFromMalware);
Expand Down