Extract and store raw value of ResScalarValue.

Many attr values in axml files store their raw string value. Now it's extracted, so any ResScalarValue could make some use of it.
This commit is contained in:
Ryszard Wiśniewski 2011-05-09 10:41:29 +02:00
parent 7d4edce7ee
commit 5740064cee
16 changed files with 49 additions and 57 deletions

View File

@ -99,7 +99,7 @@ public class ResAttr extends ResBagValue implements ResValuesXmlSerializable {
int resId = items[i].m1; int resId = items[i].m1;
pkg.addSynthesizedRes(resId); pkg.addSynthesizedRes(resId);
attrItems[j++] = new Duo<ResReferenceValue, ResIntValue>( attrItems[j++] = new Duo<ResReferenceValue, ResIntValue>(
factory.newReference(resId), (ResIntValue) items[i].m2); factory.newReference(resId, null), (ResIntValue) items[i].m2);
} }
switch (type & 0xff0000) { switch (type & 0xff0000) {
case TYPE_ENUM: case TYPE_ENUM:

View File

@ -22,8 +22,8 @@ package brut.androlib.res.data.value;
public class ResBoolValue extends ResScalarValue implements ResValuesXmlSerializable { public class ResBoolValue extends ResScalarValue implements ResValuesXmlSerializable {
private final boolean mValue; private final boolean mValue;
public ResBoolValue(boolean value) { public ResBoolValue(boolean value, String rawValue) {
super("bool"); super("bool", rawValue);
this.mValue = value; this.mValue = value;
} }

View File

@ -20,8 +20,8 @@ package brut.androlib.res.data.value;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class ResColorValue extends ResIntValue { public class ResColorValue extends ResIntValue {
public ResColorValue(int value) { public ResColorValue(int value, String rawValue) {
super(value, "color"); super(value, rawValue, "color");
} }
@Override @Override

View File

@ -26,8 +26,8 @@ import org.xmlpull.v1.XmlSerializer;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class ResDimenValue extends ResIntValue { public class ResDimenValue extends ResIntValue {
public ResDimenValue(int value) { public ResDimenValue(int value, String rawValue) {
super(value, "dimen"); super(value, rawValue, "dimen");
} }
@Override @Override

View File

@ -22,8 +22,8 @@ package brut.androlib.res.data.value;
public class ResFloatValue extends ResScalarValue { public class ResFloatValue extends ResScalarValue {
private final float mValue; private final float mValue;
public ResFloatValue(float value) { public ResFloatValue(float value, String rawValue) {
super("float"); super("float", rawValue);
this.mValue = value; this.mValue = value;
} }

View File

@ -23,8 +23,8 @@ import brut.androlib.AndrolibException;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class ResFractionValue extends ResIntValue { public class ResFractionValue extends ResIntValue {
public ResFractionValue(int value) { public ResFractionValue(int value, String rawValue) {
super(value, "fraction"); super(value, rawValue, "fraction");
} }
@Override @Override

View File

@ -24,12 +24,12 @@ import brut.androlib.AndrolibException;
public class ResIntValue extends ResScalarValue implements ResValuesXmlSerializable { public class ResIntValue extends ResScalarValue implements ResValuesXmlSerializable {
protected final int mValue; protected final int mValue;
public ResIntValue(int value) { public ResIntValue(int value, String rawValue) {
this(value, "integer"); this(value, rawValue, "integer");
} }
public ResIntValue(int value, String type) { public ResIntValue(int value, String rawValue, String type) {
super(type); super(type, rawValue);
this.mValue = value; this.mValue = value;
} }

View File

@ -27,12 +27,13 @@ public class ResReferenceValue extends ResIntValue {
private final ResPackage mPackage; private final ResPackage mPackage;
private final boolean mTheme; private final boolean mTheme;
public ResReferenceValue(ResPackage package_, int value) { public ResReferenceValue(ResPackage package_, int value, String rawValue) {
this(package_, value, false); this(package_, value, rawValue, false);
} }
public ResReferenceValue(ResPackage package_, int value, boolean theme) { public ResReferenceValue(ResPackage package_, int value, String rawValue,
super(value, "reference"); boolean theme) {
super(value, rawValue, "reference");
mPackage = package_; mPackage = package_;
mTheme = theme; mTheme = theme;
} }

View File

@ -28,9 +28,11 @@ import org.xmlpull.v1.XmlSerializer;
public abstract class ResScalarValue extends ResValue public abstract class ResScalarValue extends ResValue
implements ResXmlEncodable, ResValuesXmlSerializable { implements ResXmlEncodable, ResValuesXmlSerializable {
protected final String mType; protected final String mType;
protected final String mRawValue;
protected ResScalarValue(String type) { protected ResScalarValue(String type, String rawValue) {
mType = type; mType = type;
mRawValue = rawValue;
} }
public abstract String toResXmlFormat() throws AndrolibException; public abstract String toResXmlFormat() throws AndrolibException;

View File

@ -30,7 +30,7 @@ public class ResStringValue extends ResScalarValue
} }
public ResStringValue(String value, String type) { public ResStringValue(String value, String type) {
super(type); super(type, value);
this.mValue = value; this.mValue = value;
} }

View File

@ -34,7 +34,7 @@ public class ResStyleValue extends ResBagValue implements ResValuesXmlSerializab
mItems = new Duo[items.length]; mItems = new Duo[items.length];
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
mItems[i] = new Duo<ResReferenceValue, ResScalarValue>( mItems[i] = new Duo<ResReferenceValue, ResScalarValue>(
factory.newReference(items[i].m1), items[i].m2); factory.newReference(items[i].m1, null), items[i].m2);
} }
} }

View File

@ -31,30 +31,30 @@ public class ResValueFactory {
this.mPackage = pakage_; this.mPackage = pakage_;
} }
public ResScalarValue factory(int type, int value) public ResScalarValue factory(int type, int value, String rawValue)
throws AndrolibException { throws AndrolibException {
switch (type) { switch (type) {
case TypedValue.TYPE_REFERENCE: case TypedValue.TYPE_REFERENCE:
return newReference(value); return newReference(value, rawValue);
case TypedValue.TYPE_ATTRIBUTE: case TypedValue.TYPE_ATTRIBUTE:
return newReference(value, true); return newReference(value, rawValue, true);
case TypedValue.TYPE_FLOAT: case TypedValue.TYPE_FLOAT:
return new ResFloatValue(Float.intBitsToFloat(value)); return new ResFloatValue(Float.intBitsToFloat(value), rawValue);
case TypedValue.TYPE_DIMENSION: case TypedValue.TYPE_DIMENSION:
return new ResDimenValue(value); return new ResDimenValue(value, rawValue);
case TypedValue.TYPE_FRACTION: case TypedValue.TYPE_FRACTION:
return new ResFractionValue(value); return new ResFractionValue(value, rawValue);
case TypedValue.TYPE_INT_BOOLEAN: case TypedValue.TYPE_INT_BOOLEAN:
return new ResBoolValue(value != 0); return new ResBoolValue(value != 0, rawValue);
} }
if (type >= TypedValue.TYPE_FIRST_COLOR_INT if (type >= TypedValue.TYPE_FIRST_COLOR_INT
&& type <= TypedValue.TYPE_LAST_COLOR_INT) { && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return new ResColorValue(value); return new ResColorValue(value, rawValue);
} }
if (type >= TypedValue.TYPE_FIRST_INT if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) { && type <= TypedValue.TYPE_LAST_INT) {
return new ResIntValue(value); return new ResIntValue(value, rawValue);
} }
throw new AndrolibException("Invalid value type: "+ type); throw new AndrolibException("Invalid value type: "+ type);
@ -69,7 +69,7 @@ public class ResValueFactory {
public ResBagValue bagFactory(int parent, public ResBagValue bagFactory(int parent,
Duo<Integer, ResScalarValue>[] items) throws AndrolibException { Duo<Integer, ResScalarValue>[] items) throws AndrolibException {
ResReferenceValue parentVal = newReference(parent); ResReferenceValue parentVal = newReference(parent, null);
if (items.length == 0) { if (items.length == 0) {
return new ResBagValue(parentVal); return new ResBagValue(parentVal);
@ -88,11 +88,12 @@ public class ResValueFactory {
return new ResStyleValue(parentVal, items, this); return new ResStyleValue(parentVal, items, this);
} }
public ResReferenceValue newReference(int resID) { public ResReferenceValue newReference(int resID, String rawValue) {
return newReference(resID, false); return newReference(resID, rawValue, false);
} }
public ResReferenceValue newReference(int resID, boolean theme) { public ResReferenceValue newReference(int resID, String rawValue,
return new ResReferenceValue(mPackage, resID, theme); boolean theme) {
return new ResReferenceValue(mPackage, resID, rawValue, theme);
} }
} }

View File

@ -221,7 +221,7 @@ public class ARSCDecoder {
return type == TypedValue.TYPE_STRING ? return type == TypedValue.TYPE_STRING ?
mPkg.getValueFactory().factory(mTableStrings.getHTML(data)) : mPkg.getValueFactory().factory(mTableStrings.getHTML(data)) :
mPkg.getValueFactory().factory(type, data); mPkg.getValueFactory().factory(type, data, null);
} }
private ResConfigFlags readConfigFlags() throws IOException, AndrolibException { private ResConfigFlags readConfigFlags() throws IOException, AndrolibException {
@ -300,7 +300,7 @@ public class ARSCDecoder {
mPkg.addResSpec(spec); mPkg.addResSpec(spec);
mType.addResSpec(spec); mType.addResSpec(spec);
ResValue value = new ResBoolValue(false); ResValue value = new ResBoolValue(false, null);
ResResource res = new ResResource( ResResource res = new ResResource(
mPkg.getOrCreateConfig(new ResConfigFlags()), spec, value); mPkg.getOrCreateConfig(new ResConfigFlags()), spec, value);
mPkg.addResource(res); mPkg.addResource(res);

View File

@ -308,20 +308,13 @@ public class AXmlResourceParser implements XmlResourceParser {
int offset = getAttributeOffset(index); int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
int valueString = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING]; int valueRaw = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING];
if (mAttrDecoder != null && ( if (mAttrDecoder != null) {
valueString == -1
|| valueType == TypedValue.TYPE_REFERENCE
|| valueType == TypedValue.TYPE_ATTRIBUTE
|| (
valueType >= TypedValue.TYPE_FIRST_COLOR_INT
&& valueType <= TypedValue.TYPE_LAST_COLOR_INT
)
)) {
try { try {
return mAttrDecoder.decode(valueType, valueData, return mAttrDecoder.decode(valueType, valueData,
getAttributeNameResource(index)); valueRaw == -1 ? null : m_strings.getString(valueRaw),
getAttributeNameResource(index));
} catch (AndrolibException ex) { } catch (AndrolibException ex) {
setFirstError(ex); setFirstError(ex);
LOGGER.log(Level.WARNING, String.format( LOGGER.log(Level.WARNING, String.format(
@ -333,11 +326,6 @@ public class AXmlResourceParser implements XmlResourceParser {
} }
} }
if (valueString != -1) {
return AndrolibResources.escapeTextForResXml(
m_strings.getString(valueString));
}
return TypedValue.coerceToString(valueType, valueData); return TypedValue.coerceToString(valueType, valueData);
} }

View File

@ -25,10 +25,10 @@ import brut.androlib.res.data.value.ResScalarValue;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class ResAttrDecoder { public class ResAttrDecoder {
public String decode(int type, int value, int attrResId) public String decode(int type, int value, String rawValue, int attrResId)
throws AndrolibException { throws AndrolibException {
ResScalarValue resValue = mCurrentPackage.getValueFactory() ResScalarValue resValue = mCurrentPackage.getValueFactory()
.factory(type, value); .factory(type, value, rawValue);
if (attrResId == 0) { if (attrResId == 0) {
return resValue.toResXmlFormat(); return resValue.toResXmlFormat();
} }

View File

@ -88,7 +88,7 @@ public class ResFileDecoder {
LOGGER.log(Level.SEVERE, String.format( LOGGER.log(Level.SEVERE, String.format(
"Could not decode file, replacing by FALSE value: %s", "Could not decode file, replacing by FALSE value: %s",
inFileName, outFileName), ex); inFileName, outFileName), ex);
res.replace(new ResBoolValue(false)); res.replace(new ResBoolValue(false, null));
} }
} }