am 02d2b5a4: LayoutLib: When possible ensure parsers are popped from the stack.

* commit '02d2b5a4031c80bfe1012ce2f4f7b3695762abd9':
  LayoutLib: When possible ensure parsers are popped from the stack.
This commit is contained in:
Xavier Ducrohet
2011-02-14 09:57:17 -08:00
committed by Android Git Automerger
5 changed files with 46 additions and 21 deletions

View File

@ -294,7 +294,8 @@ public final class BridgeTypedArray extends TypedArray {
return null; return null;
} }
String value = mResourceData[index].getValue(); ResourceValue resValue = mResourceData[index];
String value = resValue.getValue();
if (value == null) { if (value == null) {
return null; return null;
@ -308,11 +309,13 @@ public final class BridgeTypedArray extends TypedArray {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new FileReader(f)); parser.setInput(new FileReader(f));
ColorStateList colorStateList = ColorStateList.createFromXml( BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(
mContext.getResources(), parser, mContext, resValue.isFramework());
// FIXME: we need to know if this resource is platform or not try {
new BridgeXmlBlockParser(parser, mContext, false)); return ColorStateList.createFromXml(mContext.getResources(), blockParser);
return colorStateList; } finally {
blockParser.ensurePopped();
}
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, Bridge.getLog().error(LayoutLog.TAG_BROKEN,
"Failed to configure parser for " + value, e, null /*data*/); "Failed to configure parser for " + value, e, null /*data*/);

View File

@ -45,6 +45,8 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
private boolean mStarted = false; private boolean mStarted = false;
private int mEventType = START_DOCUMENT; private int mEventType = START_DOCUMENT;
private boolean mPopped = true; // default to true in case it's not pushed.
/** /**
* Builds a {@link BridgeXmlBlockParser}. * Builds a {@link BridgeXmlBlockParser}.
* @param parser The XmlPullParser to get the content from. * @param parser The XmlPullParser to get the content from.
@ -59,6 +61,7 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
if (mContext != null) { if (mContext != null) {
mContext.pushParser(this); mContext.pushParser(this);
mPopped = false;
} }
} }
@ -82,6 +85,13 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
return null; return null;
} }
public void ensurePopped() {
if (mContext != null && mPopped == false) {
mContext.popParser();
mPopped = true;
}
}
// ------- XmlResourceParser implementation // ------- XmlResourceParser implementation
public void setFeature(String name, boolean state) public void setFeature(String name, boolean state)
@ -249,9 +259,9 @@ public class BridgeXmlBlockParser implements XmlResourceParser {
} }
int ev = mParser.next(); int ev = mParser.next();
if (ev == END_TAG && mParser.getDepth() == 1 && mContext != null) { if (ev == END_TAG && mParser.getDepth() == 1) {
// done with parser remove it from the context stack. // done with parser remove it from the context stack.
mContext.popParser(); ensurePopped();
} }
mEventType = ev; mEventType = ev;
return ev; return ev;

View File

@ -76,9 +76,13 @@ abstract class CustomBar extends LinearLayout {
"UTF8"); "UTF8");
BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(
parser, (BridgeContext) context, false); parser, (BridgeContext) context, false /*platformFile*/);
try {
inflater.inflate(bridgeParser, this, true); inflater.inflate(bridgeParser, this, true);
} finally {
bridgeParser.ensurePopped();
}
} }
private InputStream getIcon(String iconName, Density[] densityInOut, String[] pathOut, private InputStream getIcon(String iconName, Density[] densityInOut, String[] pathOut,

View File

@ -182,8 +182,8 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
context.setBridgeInflater(mInflater); context.setBridgeInflater(mInflater);
mInflater.setFactory2(context); mInflater.setFactory2(context);
mBlockParser = new BridgeXmlBlockParser(params.getLayoutDescription(), mBlockParser = new BridgeXmlBlockParser(
context, false /* platformResourceFlag */); params.getLayoutDescription(), context, false /* platformResourceFlag */);
return SUCCESS.createResult(); return SUCCESS.createResult();
} }
@ -562,13 +562,14 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
BridgeContext context = getContext(); BridgeContext context = getContext();
// create a block parser for the XML // create a block parser for the XML
BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(childXml, context, BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(
false /* platformResourceFlag */); childXml, context, false /* platformResourceFlag */);
// inflate the child without adding it to the root since we want to control where it'll // inflate the child without adding it to the root since we want to control where it'll
// get added. We do pass the parentView however to ensure that the layoutParams will // get added. We do pass the parentView however to ensure that the layoutParams will
// be created correctly. // be created correctly.
final View child = mInflater.inflate(blockParser, parentView, false /*attachToRoot*/); final View child = mInflater.inflate(blockParser, parentView, false /*attachToRoot*/);
blockParser.ensurePopped();
invalidateRenderingSize(); invalidateRenderingSize();

View File

@ -126,8 +126,13 @@ public final class ResourceHelper {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new FileReader(f)); parser.setInput(new FileReader(f));
return ColorStateList.createFromXml(context.getResources(), BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(
new BridgeXmlBlockParser(parser, context, resValue.isFramework())); parser, context, resValue.isFramework());
try {
return ColorStateList.createFromXml(context.getResources(), blockParser);
} finally {
blockParser.ensurePopped();
}
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Bridge.getLog().error(LayoutLog.TAG_BROKEN, Bridge.getLog().error(LayoutLog.TAG_BROKEN,
"Failed to configure parser for " + value, e, null /*data*/); "Failed to configure parser for " + value, e, null /*data*/);
@ -164,8 +169,6 @@ public final class ResourceHelper {
* @param context the current context * @param context the current context
*/ */
public static Drawable getDrawable(ResourceValue value, BridgeContext context) { public static Drawable getDrawable(ResourceValue value, BridgeContext context) {
Drawable d = null;
String stringValue = value.getValue(); String stringValue = value.getValue();
if (RenderResources.REFERENCE_NULL.equals(stringValue)) { if (RenderResources.REFERENCE_NULL.equals(stringValue)) {
return null; return null;
@ -205,9 +208,13 @@ public final class ResourceHelper {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new FileReader(f)); parser.setInput(new FileReader(f));
d = Drawable.createFromXml(context.getResources(), BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(
new BridgeXmlBlockParser(parser, context, value.isFramework())); parser, context, value.isFramework());
return d; try {
return Drawable.createFromXml(context.getResources(), blockParser);
} finally {
blockParser.ensurePopped();
}
} catch (Exception e) { } catch (Exception e) {
// this is an error and not warning since the file existence is checked before // this is an error and not warning since the file existence is checked before
// attempting to parse it. // attempting to parse it.