fix: xml array encoding/decoding of integer and string #12

This commit is contained in:
REAndroid 2023-01-16 06:58:33 -05:00
parent 1c53bd295c
commit ee5db344af
3 changed files with 48 additions and 9 deletions

View File

@ -137,4 +137,7 @@ public class ApkUtil {
public static final String NAME_data="data"; public static final String NAME_data="data";
public static final String RES_DIR_NAME="res"; public static final String RES_DIR_NAME="res";
public static final String FILE_NAME_PUBLIC_XML ="public.xml"; public static final String FILE_NAME_PUBLIC_XML ="public.xml";
public static final String TAG_STRING_ARRAY = "string-array";
public static final String TAG_INTEGER_ARRAY = "integer-array";
} }

View File

@ -15,12 +15,17 @@
*/ */
package com.reandroid.lib.apk.xmldecoder; package com.reandroid.lib.apk.xmldecoder;
import com.reandroid.lib.apk.ApkUtil;
import com.reandroid.lib.arsc.decoder.ValueDecoder; import com.reandroid.lib.arsc.decoder.ValueDecoder;
import com.reandroid.lib.arsc.value.ResValueBag; import com.reandroid.lib.arsc.value.ResValueBag;
import com.reandroid.lib.arsc.value.ResValueBagItem; import com.reandroid.lib.arsc.value.ResValueBagItem;
import com.reandroid.lib.arsc.value.ValueType;
import com.reandroid.lib.common.EntryStore; import com.reandroid.lib.common.EntryStore;
import com.reandroid.xml.XMLElement; import com.reandroid.xml.XMLElement;
import java.util.HashSet;
import java.util.Set;
class XMLArrayDecoder extends BagDecoder{ class XMLArrayDecoder extends BagDecoder{
public XMLArrayDecoder(EntryStore entryStore) { public XMLArrayDecoder(EntryStore entryStore) {
super(entryStore); super(entryStore);
@ -30,11 +35,19 @@ class XMLArrayDecoder extends BagDecoder{
public void decode(ResValueBag resValueBag, XMLElement parentElement) { public void decode(ResValueBag resValueBag, XMLElement parentElement) {
ResValueBagItem[] bagItems = resValueBag.getBagItems(); ResValueBagItem[] bagItems = resValueBag.getBagItems();
EntryStore entryStore=getEntryStore(); EntryStore entryStore=getEntryStore();
Set<ValueType> valueTypes = new HashSet<>();
for(int i=0;i<bagItems.length;i++){ for(int i=0;i<bagItems.length;i++){
String value = ValueDecoder.decodeIntEntry(entryStore, bagItems[i]); ResValueBagItem bagItem = bagItems[i];
String value = ValueDecoder.decodeIntEntry(entryStore, bagItem);
XMLElement child = new XMLElement("item"); XMLElement child = new XMLElement("item");
child.setTextContent(value); child.setTextContent(value);
parentElement.addChild(child); parentElement.addChild(child);
valueTypes.add(bagItem.getValueType());
}
if(valueTypes.contains(ValueType.STRING)){
parentElement.setTagName(ApkUtil.TAG_STRING_ARRAY);
}else if(valueTypes.size()==1 && valueTypes.contains(ValueType.INT_DEC)){
parentElement.setTagName(ApkUtil.TAG_INTEGER_ARRAY);
} }
} }
@Override @Override

View File

@ -15,6 +15,7 @@
*/ */
package com.reandroid.lib.apk.xmlencoder; package com.reandroid.lib.apk.xmlencoder;
import com.reandroid.lib.apk.ApkUtil;
import com.reandroid.lib.arsc.array.ResValueBagItemArray; import com.reandroid.lib.arsc.array.ResValueBagItemArray;
import com.reandroid.lib.arsc.decoder.ValueDecoder; import com.reandroid.lib.arsc.decoder.ValueDecoder;
import com.reandroid.lib.arsc.value.ResValueBag; import com.reandroid.lib.arsc.value.ResValueBag;
@ -29,7 +30,14 @@ class XMLValuesEncoderArray extends XMLValuesEncoderBag{
@Override @Override
void encodeChildes(XMLElement parentElement, ResValueBag resValueBag){ void encodeChildes(XMLElement parentElement, ResValueBag resValueBag){
int count = parentElement.getChildesCount(); int count = parentElement.getChildesCount();
boolean tag_string="string-array".equals(parentElement.getTagName()); String tagName = parentElement.getTagName();
boolean force_string = false;
boolean force_integer = false;
if(ApkUtil.TAG_STRING_ARRAY.equals(tagName)){
force_string = true;
}else if(ApkUtil.TAG_INTEGER_ARRAY.equals(tagName)){
force_integer = true;
}
ResValueBagItemArray itemArray = resValueBag.getResValueBagItemArray(); ResValueBagItemArray itemArray = resValueBag.getResValueBagItemArray();
for(int i=0;i<count;i++){ for(int i=0;i<count;i++){
XMLElement child=parentElement.getChildAt(i); XMLElement child=parentElement.getChildAt(i);
@ -39,12 +47,24 @@ class XMLValuesEncoderArray extends XMLValuesEncoderBag{
String valueText=child.getTextContent(); String valueText=child.getTextContent();
if(ValueDecoder.isReference(valueText)){ if(force_string){
bagItem.setValueAsString(ValueDecoder
.unEscapeSpecialCharacter(valueText));
}else if(force_integer){
valueText=trimText(valueText);
if(!ValueDecoder.isInteger(valueText)){
throw new EncodeException("Invalid integer value for array name="
+parentElement.getAttributeValue("name")
+", entry no"+(i+1)+", near line: " + child.getLineNumber());
}
bagItem.setTypeAndData(ValueType.INT_DEC,
ValueDecoder.parseInteger(valueText));
}else if(ValueDecoder.isReference(valueText)){
bagItem.setTypeAndData(ValueType.REFERENCE, bagItem.setTypeAndData(ValueType.REFERENCE,
getMaterials().resolveReference(valueText)); getMaterials().resolveReference(valueText));
}else if(EncodeUtil.isEmpty(valueText)) { }else if(EncodeUtil.isEmpty(valueText)) {
bagItem.setTypeAndData(ValueType.NULL, 0); bagItem.setTypeAndData(ValueType.NULL, 0);
}else if(!tag_string){ }else {
ValueDecoder.EncodeResult encodeResult = ValueDecoder.EncodeResult encodeResult =
ValueDecoder.encodeGuessAny(valueText); ValueDecoder.encodeGuessAny(valueText);
if(encodeResult!=null){ if(encodeResult!=null){
@ -54,10 +74,13 @@ class XMLValuesEncoderArray extends XMLValuesEncoderBag{
bagItem.setValueAsString(ValueDecoder bagItem.setValueAsString(ValueDecoder
.unEscapeSpecialCharacter(valueText)); .unEscapeSpecialCharacter(valueText));
} }
}else {
bagItem.setValueAsString(ValueDecoder
.unEscapeSpecialCharacter(valueText));
} }
} }
} }
private static String trimText(String text){
if(text==null){
return null;
}
return text.trim();
}
} }