Skip to content

Commit 3133bf3

Browse files
committed
[XML] Optional charset encoding
- Via System property com.reandroid.xml.KeepCharsetEncoding, turns on by any non-null value. - If this property is ON (true), the decoded XML string string will have same encoding as string pool. - By default only AndroidManifest.xml will be encoded with utf-16 - If utf-16 activated, "encoding" attribute under xml declaration will be removed, to let pull parser detect charset by itself.
1 parent 33241e5 commit 3133bf3

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

src/main/java/com/reandroid/apk/xmlencoder/XMLEncodeSource.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.reandroid.apk.xmlencoder;
22

33
import com.reandroid.apk.APKLogger;
4+
import com.reandroid.app.AndroidManifest;
45
import com.reandroid.archive.ByteInputSource;
56
import com.reandroid.arsc.chunk.PackageBlock;
7+
import com.reandroid.arsc.chunk.xml.AndroidManifestBlock;
68
import com.reandroid.arsc.chunk.xml.ResXmlDocument;
79
import com.reandroid.utils.Crc32;
810
import com.reandroid.utils.io.IOUtil;
@@ -68,9 +70,15 @@ private byte[] getArray() throws IOException{
6870
}
6971
private ResXmlDocument encode() throws XmlPullParserException, IOException {
7072
XMLParserSource parserSource = this.parserSource;
71-
logVerbose("Encoding: " + parserSource.getPath());
73+
String path = parserSource.getPath();
74+
logVerbose("Encoding: " + path);
7275
XmlPullParser parser = parserSource.getParser();
73-
ResXmlDocument resXmlDocument = new ResXmlDocument();
76+
ResXmlDocument resXmlDocument;
77+
if (AndroidManifest.FILE_NAME.equals(path)) {
78+
resXmlDocument = new AndroidManifestBlock();
79+
} else {
80+
resXmlDocument = new ResXmlDocument();
81+
}
7482
resXmlDocument.setPackageBlock(this.packageBlock);
7583
resXmlDocument.parse(parser);
7684
IOUtil.close(parser);

src/main/java/com/reandroid/arsc/chunk/xml/ResXmlDocument.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.reandroid.utils.io.FileUtil;
1616
import com.reandroid.xml.XMLDocument;
1717
import com.reandroid.xml.XMLFactory;
18+
import com.reandroid.xml.XMLUtil;
1819
import com.reandroid.xml.base.Document;
1920
import org.xmlpull.v1.XmlPullParser;
2021
import org.xmlpull.v1.XmlPullParserException;
@@ -104,7 +105,7 @@ ResXmlDocumentChunk getChunk() {
104105
@Override
105106
public XMLDocument toXml(boolean decode) {
106107
XMLDocument xmlDocument = new XMLDocument();
107-
xmlDocument.setEncoding("utf-8");
108+
xmlDocument.setEncoding(getEncoding());
108109
Iterator<ResXmlNode> iterator = iterator();
109110
while (iterator.hasNext()) {
110111
ResXmlNode node = iterator.next();
@@ -152,23 +153,29 @@ String nodeTypeName() {
152153
@Override
153154
public JSONObject toJson() {
154155
JSONObject jsonObject = new JSONObject();
155-
jsonObject.put(JSON_encoding, getEncoding());
156+
jsonObject.put(JSON_encoding, getStringPool().getEncoding());
156157
jsonObject.put(JSON_node_type, nodeTypeName());
157158
jsonObject.put(JSON_nodes, nodesToJson());
158159
return jsonObject;
159160
}
160161
@Override
161162
public void fromJson(JSONObject json) {
162-
setEncoding(json.optString(JSON_encoding));
163+
getStringPool().setEncoding(json.optString(JSON_encoding));
163164
nodesFromJson(json);
164165
refresh();
165166
}
166167

167168
public String getEncoding() {
168-
return getStringPool().getEncoding();
169+
if (XMLUtil.KEEP_CHARSET_ENCODING) {
170+
return getStringPool().getEncoding();
171+
} else {
172+
return "utf-8";
173+
}
169174
}
170175
public void setEncoding(String encoding) {
171-
getStringPool().setEncoding(encoding);
176+
if (XMLUtil.KEEP_CHARSET_ENCODING) {
177+
getStringPool().setEncoding(encoding);
178+
}
172179
}
173180
@Override
174181
public int getLineNumber() {
@@ -237,13 +244,15 @@ public ResXmlIDMap getResXmlIDMap() {
237244
public void serialize(XmlSerializer serializer) throws IOException {
238245
serialize(serializer, true);
239246
}
247+
@Override
240248
public void serialize(XmlSerializer serializer, boolean decode) throws IOException {
241249
PackageBlock packageBlock = getPackageBlock();
242-
if(decode && packageBlock == null) {
250+
if (decode && packageBlock == null) {
243251
throw new IOException("Can not decode without package");
244252
}
245253
setIndent(serializer, true);
246-
serializer.startDocument(getEncoding(), null);
254+
String encoding = getEncoding();
255+
serializer.startDocument(encoding, encoding == null ? Boolean.FALSE : null);
247256
fixNamespaces();
248257
removeUnusedNamespaces();
249258

src/main/java/com/reandroid/arsc/pool/StringPool.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,10 @@ public String getEncoding() {
344344
if (isUtf8()) {
345345
return "utf-8";
346346
}
347-
return "utf-16";
347+
return null;
348348
}
349349
public void setEncoding(String encoding) {
350-
boolean utf8;
351-
if (encoding != null) {
352-
utf8 = !StringsUtil.toLowercase(encoding).startsWith("utf-16");
353-
} else {
354-
utf8 = true;
355-
}
356-
setUtf8(utf8);
350+
setUtf8(encoding != null && !StringsUtil.toLowercase(encoding).startsWith("utf-16"));
357351
}
358352

359353
abstract StringArray<T> newInstance(OffsetArray offsets, IntegerItem itemCount, IntegerItem itemStart, boolean is_utf8);

src/main/java/com/reandroid/xml/XMLFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.xmlpull.v1.XmlSerializer;
2424

2525
import java.io.*;
26-
import java.nio.charset.StandardCharsets;
2726

2827
public class XMLFactory {
2928

@@ -37,6 +36,7 @@ public static XmlPullParser newPullParser(File file) throws XmlPullParserExcepti
3736
XmlPullParser parser = newPullParser();
3837
try {
3938
parser.setInput(FileUtil.inputStream(file), null);
39+
XMLUtil.setLocation(parser, file);
4040
} catch (IOException ex) {
4141
throw new XmlPullParserException(ex.getMessage());
4242
}
@@ -74,7 +74,7 @@ public static XmlSerializer newSerializer(File file, String encoding) throws IOE
7474
}
7575
public static XmlSerializer newSerializer(OutputStream outputStream) throws IOException{
7676
XmlSerializer serializer = newSerializer();
77-
serializer.setOutput(outputStream, StandardCharsets.UTF_8.name());
77+
serializer.setOutput(outputStream, "utf-8");
7878
return serializer;
7979
}
8080
public static XmlSerializer newSerializer(OutputStream outputStream, String encoding) throws IOException{

src/main/java/com/reandroid/xml/XMLUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
public class XMLUtil {
2828

29+
public static final boolean KEEP_CHARSET_ENCODING = System.getProperty("com.reandroid.xml.KeepCharsetEncoding") != null;
30+
31+
2932
public static void expectEvent(XmlPullParser parser, int expect) throws XmlPullParserException {
3033
int event = parser.getEventType();
3134
if (event != expect) {

0 commit comments

Comments
 (0)