Skip to content

Commit

Permalink
Fix: Return defaultValue for XmlResourceParser #59
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Jun 23, 2024
1 parent 49352f4 commit b119f31
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 103 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/reandroid/arsc/chunk/xml/ParserEventList.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ public String getText(){
return null;
}
public int getLineNumber(){
if(type!=ParserEvent.COMMENT
&& type!=ParserEvent.START_TAG
&& type!=ParserEvent.END_TAG){
return 0;
if(type != ParserEvent.COMMENT
&& type != ParserEvent.TEXT
&& type != ParserEvent.START_TAG
&& type != ParserEvent.END_TAG){
return -1;
}
ResXmlNode xmlNode = getXmlNode();
if(mCurrent.isEndComment() || type==ParserEvent.END_TAG){
if(mCurrent.isEndComment() || type == ParserEvent.END_TAG){
return ((ResXmlElement)xmlNode).getEndLineNumber();
}
if(type==ParserEvent.TEXT){
if(type == ParserEvent.TEXT){
return ((ResXmlTextNode)xmlNode).getLineNumber();
}
return ((ResXmlElement)xmlNode).getStartLineNumber();
Expand Down
178 changes: 81 additions & 97 deletions src/main/java/com/reandroid/arsc/chunk/xml/ResXmlPullParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
import com.reandroid.arsc.coder.XmlSanitizer;
import com.reandroid.arsc.value.ValueType;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.StringsUtil;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* Direct implementation of:
* https://android.googlesource.com/platform/frameworks/base/+/main/core/java/android/content/res/XmlBlock.java
* */

public class ResXmlPullParser implements XmlResourceParser {

private PackageBlock mCurrentPackage;
private final ParserEventList mEventList = new ParserEventList();
private ResXmlDocument mDocument;
Expand All @@ -53,20 +58,10 @@ public PackageBlock getCurrentPackage(){
}
public void setCurrentPackage(PackageBlock packageBlock){
this.mCurrentPackage = packageBlock;
if(mDocument != null){
if(mDocument != null) {
mDocument.setPackageBlock(packageBlock);
}
}
public synchronized ResXmlPullParser getParser(){
if(isBusy()){
return new ResXmlPullParser(getCurrentPackage());
}
closeDocument();
return this;
}
public synchronized boolean isBusy() {
return !mEventList.hasNext();
}
public synchronized void setResXmlDocument(ResXmlDocument xmlDocument){
closeDocument();
this.mDocument = xmlDocument;
Expand Down Expand Up @@ -139,7 +134,21 @@ public String getAttributeValue(String namespace, String name) {
}
@Override
public String getPositionDescription() {
return null;
StringBuilder builder = new StringBuilder();
builder.append(" Binary XML file line #");
builder.append(mEventList.getLineNumber());
ResXmlElement element = getCurrentElement();
if(element != null) {
if (mEventList.getType() == START_TAG) {
builder.append(" START_TAG ");
} else {
builder.append(" END_TAG ");
}
builder.append('<');
builder.append(element.getName(true));
builder.append('>');
}
return builder.toString();
}
@Override
public int getAttributeNameResource(int index) {
Expand All @@ -150,140 +159,94 @@ public int getAttributeNameResource(int index) {
return 0;
}
@Override
public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null){
return 0;
}
List<String> list = Arrays.asList(options);
int index = list.indexOf(decodeAttributeValue(xmlAttribute));
if(index==-1){
return defaultValue;
}
return index;
}
@Override
public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null || xmlAttribute.getValueType() != ValueType.BOOLEAN){
return defaultValue;
if(xmlAttribute != null) {
int v = getAttributeIntValue(xmlAttribute, 0);
return v != 0;
}
return xmlAttribute.getValueAsBoolean();
return defaultValue;
}
@Override
public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.ATTRIBUTE
||valueType==ValueType.REFERENCE
||valueType==ValueType.DYNAMIC_ATTRIBUTE
||valueType==ValueType.DYNAMIC_REFERENCE){
if(xmlAttribute != null && xmlAttribute.getValueType() == ValueType.REFERENCE){
return xmlAttribute.getData();
}
return defaultValue;
}
@Override
public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.DEC
||valueType==ValueType.HEX){
return xmlAttribute.getData();
}
return defaultValue;
return getAttributeIntValue(getAttribute(namespace, attribute), defaultValue);
}
@Override
public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.DEC){
return xmlAttribute.getData();
}
return defaultValue;
return getAttributeIntValue(getAttribute(namespace, attribute), defaultValue);
}
@Override
public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.FLOAT){
return Float.intBitsToFloat(xmlAttribute.getData());
if(xmlAttribute != null){
ValueType valueType = xmlAttribute.getValueType();
if(valueType == ValueType.FLOAT) {
return Float.intBitsToFloat(xmlAttribute.getData());
}
}
return defaultValue;
}

@Override
public int getAttributeListValue(int index, String[] options, int defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null){
return 0;
if(xmlAttribute == null ||
xmlAttribute.getValueType() != ValueType.STRING
|| options == null || options.length == 0){
return defaultValue;
}
List<String> list = Arrays.asList(options);
int i = list.indexOf(decodeAttributeValue(xmlAttribute));
if(i==-1){
return convertValueToList(xmlAttribute.getValueAsString(), options, defaultValue);
}
@Override
public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
ResXmlAttribute xmlAttribute = getAttribute(namespace, attribute);
if(xmlAttribute == null ||
xmlAttribute.getValueType() != ValueType.STRING
|| options == null || options.length == 0){
return defaultValue;
}
return index;
return convertValueToList(xmlAttribute.getValueAsString(), options, defaultValue);
}
@Override
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null || xmlAttribute.getValueType() != ValueType.BOOLEAN){
return defaultValue;
if(xmlAttribute != null) {
int v = getAttributeIntValue(xmlAttribute, 0);
return v != 0;
}
return xmlAttribute.getValueAsBoolean();
return defaultValue;
}
@Override
public int getAttributeResourceValue(int index, int defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.ATTRIBUTE
||valueType==ValueType.REFERENCE
||valueType==ValueType.DYNAMIC_ATTRIBUTE
||valueType==ValueType.DYNAMIC_REFERENCE){
if(xmlAttribute != null && xmlAttribute.getValueType() == ValueType.REFERENCE){
return xmlAttribute.getData();
}
return defaultValue;
}
@Override
public int getAttributeIntValue(int index, int defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null){
return defaultValue;
}
return xmlAttribute.getData();
return getAttributeIntValue(getResXmlAttributeAt(index), defaultValue);
}
@Override
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null){
return 0;
}
return xmlAttribute.getData();
return getAttributeIntValue(getResXmlAttributeAt(index), defaultValue);
}
@Override
public float getAttributeFloatValue(int index, float defaultValue) {
ResXmlAttribute xmlAttribute = getResXmlAttributeAt(index);
if(xmlAttribute == null){
return 0;
}
ValueType valueType=xmlAttribute.getValueType();
if(valueType==ValueType.FLOAT){
return Float.intBitsToFloat(xmlAttribute.getData());
if(xmlAttribute != null) {
if(xmlAttribute.getValueType() == ValueType.FLOAT){
return Float.intBitsToFloat(xmlAttribute.getData());
}
}
return defaultValue;
}
Expand Down Expand Up @@ -319,7 +282,7 @@ public int getIdAttributeResourceValue(int defaultValue) {
return attribute.getNameId();
}
}
return 0;
return defaultValue;
}
@Override
public int getStyleAttribute() {
Expand Down Expand Up @@ -578,6 +541,27 @@ public ResXmlElement getCurrentElement() {
}
return null;
}
private int convertValueToList(String value, String[] options, int defaultValue) {
if (!StringsUtil.isEmpty(value)) {
for (int i = 0; i < options.length; i++) {
if (value.equals(options[i])) {
return i;
}
}
}
return defaultValue;
}

private int getAttributeIntValue(ResXmlAttribute xmlAttribute, int defaultValue) {
if(xmlAttribute != null) {
int type = xmlAttribute.getType() & 0xff;
if(type > 0x10 && type <= 0x1f) {
return xmlAttribute.getData();
}
// TODO: resolve if type is REFERENCE
}
return defaultValue;
}
private int getRealAttributeIndex(int index){
if(isCountNamespacesAsAttribute()){
index = index - getNamespaceCountInternal();
Expand Down

0 comments on commit b119f31

Please sign in to comment.