diff options
author | Deepanshu Gupta <deepanshu@google.com> | 2015-05-09 13:09:22 -0700 |
---|---|---|
committer | Deepanshu Gupta <deepanshu@google.com> | 2015-05-13 18:10:46 -0700 |
commit | 78af25584633462e4ab8cf9bafe10f43e7cb2d83 (patch) | |
tree | 95f370a8e271362e861a689fdfc6cb4e07496701 /tools | |
parent | 7853e137eda0866c732a301e6b3ffb19408634b4 (diff) | |
download | frameworks_base-78af25584633462e4ab8cf9bafe10f43e7cb2d83.zip frameworks_base-78af25584633462e4ab8cf9bafe10f43e7cb2d83.tar.gz frameworks_base-78af25584633462e4ab8cf9bafe10f43e7cb2d83.tar.bz2 |
Remove kxml dependency.
Create XmlPullParser using callback provided by the client. This enables
clients to choose which XmlPullParser implementation they want.
Change-Id: I9ad97a4777820cdbe5c8fc3716f74ddec9065c70
Diffstat (limited to 'tools')
7 files changed, 151 insertions, 43 deletions
diff --git a/tools/layoutlib/bridge/Android.mk b/tools/layoutlib/bridge/Android.mk index cfd597e..80b4d59 100644 --- a/tools/layoutlib/bridge/Android.mk +++ b/tools/layoutlib/bridge/Android.mk @@ -22,7 +22,6 @@ LOCAL_JAVACFLAGS := -source 6 -target 6 LOCAL_JAVA_LIBRARIES := \ - kxml2-2.3.0 \ icu4j \ layoutlib_api-prebuilt \ tools-common-prebuilt diff --git a/tools/layoutlib/bridge/bridge.iml b/tools/layoutlib/bridge/bridge.iml index 0f96916..0baa5ab 100644 --- a/tools/layoutlib/bridge/bridge.iml +++ b/tools/layoutlib/bridge/bridge.iml @@ -25,11 +25,11 @@ <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="icu4j" level="project" /> - <orderEntry type="library" name="kxml2-2.3.0" level="project" /> <orderEntry type="library" name="layoutlib_api-prebuilt" level="project" /> <orderEntry type="library" name="ninepatch-prebuilt" level="project" /> <orderEntry type="library" name="tools-common-prebuilt" level="project" /> <orderEntry type="library" name="framework.jar" level="project" /> + <orderEntry type="library" scope="TEST" name="kxml2-2.3.0" level="project" /> <orderEntry type="library" scope="TEST" name="guava" level="project" /> <orderEntry type="module-library" scope="TEST"> <library> @@ -44,5 +44,4 @@ </orderEntry> <orderEntry type="library" scope="TEST" name="JUnit4" level="application" /> </component> -</module> - +</module>
\ No newline at end of file diff --git a/tools/layoutlib/bridge/src/android/util/Xml_Delegate.java b/tools/layoutlib/bridge/src/android/util/Xml_Delegate.java index a193330..213e848 100644 --- a/tools/layoutlib/bridge/src/android/util/Xml_Delegate.java +++ b/tools/layoutlib/bridge/src/android/util/Xml_Delegate.java @@ -17,9 +17,9 @@ package android.util; import com.android.layoutlib.bridge.impl.DelegateManager; +import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.tools.layoutlib.annotations.LayoutlibDelegate; -import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -37,11 +37,7 @@ public class Xml_Delegate { @LayoutlibDelegate /*package*/ static XmlPullParser newPullParser() { try { - KXmlParser parser = new KXmlParser(); - // The prebuilt kxml2 library with the IDE doesn't support DOCECL. -// parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true); - parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); - return parser; + return ParserFactory.instantiateParser(null); } catch (XmlPullParserException e) { throw new AssertionError(); } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/ParserFactory.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/ParserFactory.java index 803849f..6ca22b0 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/ParserFactory.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/ParserFactory.java @@ -17,7 +17,10 @@ package com.android.layoutlib.bridge.impl; -import org.kxml2.io.KXmlParser; +import com.android.annotations.NonNull; +import com.android.annotations.Nullable; +import com.android.ide.common.rendering.api.LayoutlibCallback; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -35,24 +38,35 @@ import java.io.InputStream; */ public class ParserFactory { + public final static boolean LOG_PARSER = false; + private final static String ENCODING = "UTF-8"; //$NON-NLS-1$ - public final static boolean LOG_PARSER = false; + // Used to get a new XmlPullParser from the client. + @Nullable + private static LayoutlibCallback sLayoutlibCallback; - public static XmlPullParser create(File f) + public static void setLayoutlibCallback(@Nullable LayoutlibCallback callback) { + sLayoutlibCallback = callback; + } + + @NonNull + public static XmlPullParser create(@NonNull File f) throws XmlPullParserException, FileNotFoundException { InputStream stream = new FileInputStream(f); return create(stream, f.getName(), f.length()); } - public static XmlPullParser create(InputStream stream, String name) + @NonNull + public static XmlPullParser create(@NonNull InputStream stream, @Nullable String name) throws XmlPullParserException { return create(stream, name, -1); } - private static XmlPullParser create(InputStream stream, String name, long size) - throws XmlPullParserException { - KXmlParser parser = instantiateParser(name); + @NonNull + private static XmlPullParser create(@NonNull InputStream stream, @Nullable String name, + long size) throws XmlPullParserException { + XmlPullParser parser = instantiateParser(name); stream = readAndClose(stream, name, size); @@ -60,19 +74,20 @@ public class ParserFactory { return parser; } - private static KXmlParser instantiateParser(String name) throws XmlPullParserException { - KXmlParser parser; - if (name != null) { - parser = new CustomParser(name); - } else { - parser = new KXmlParser(); + @NonNull + public static XmlPullParser instantiateParser(@Nullable String name) + throws XmlPullParserException { + if (sLayoutlibCallback == null) { + throw new XmlPullParserException("ParserFactory not initialized."); } + XmlPullParser parser = sLayoutlibCallback.createParser(name); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); return parser; } - private static InputStream readAndClose(InputStream stream, String name, long size) - throws XmlPullParserException { + @NonNull + private static InputStream readAndClose(@NonNull InputStream stream, @Nullable String name, + long size) throws XmlPullParserException { // just a sanity check. It's doubtful we'll have such big files! if (size > Integer.MAX_VALUE) { throw new XmlPullParserException("File " + name + " is too big to be parsed"); @@ -121,22 +136,8 @@ public class ParserFactory { } finally { try { bufferedStream.close(); - } catch (IOException e) { + } catch (IOException ignored) { } } } - - private static class CustomParser extends KXmlParser { - private final String mName; - - CustomParser(String name) { - super(); - mName = name; - } - - @Override - public String toString() { - return mName; - } - } } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java index c708316..66b0023 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java @@ -39,6 +39,8 @@ import android.view.inputmethod.InputMethodManager_Accessor; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; +import javax.swing.text.html.parser.Parser; + import static com.android.ide.common.rendering.api.Result.Status.ERROR_LOCK_INTERRUPTED; import static com.android.ide.common.rendering.api.Result.Status.ERROR_TIMEOUT; import static com.android.ide.common.rendering.api.Result.Status.SUCCESS; @@ -99,6 +101,9 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso return result; } + // setup the ParserFactory + ParserFactory.setLayoutlibCallback(mParams.getLayoutlibCallback()); + HardwareConfig hardwareConfig = mParams.getHardwareConfig(); // setup the display Metrics. @@ -271,6 +276,7 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso mContext.getRenderResources().setFrameworkResourceIdProvider(null); mContext.getRenderResources().setLogger(null); } + ParserFactory.setLayoutlibCallback(null); } diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParserTest.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParserTest.java index 92fcf90..d87c99f 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParserTest.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParserTest.java @@ -16,14 +16,35 @@ package com.android.layoutlib.bridge.android; +import com.android.annotations.NonNull; +import com.android.ide.common.rendering.api.ActionBarCallback; +import com.android.ide.common.rendering.api.AdapterBinding; +import com.android.ide.common.rendering.api.ILayoutPullParser; +import com.android.ide.common.rendering.api.LayoutlibCallback; +import com.android.ide.common.rendering.api.ResourceReference; +import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.impl.ParserFactory; +import com.android.resources.ResourceType; +import com.android.util.Pair; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.kxml2.io.KXmlParser; import org.w3c.dom.Node; import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; -public class BridgeXmlBlockParserTest extends TestCase { +public class BridgeXmlBlockParserTest { + + @BeforeClass + public void setUp() { + ParserFactory.setLayoutlibCallback(new LayoutlibTestCallback()); + } + + @Test public void testXmlBlockParser() throws Exception { XmlPullParser parser = ParserFactory.create( @@ -65,7 +86,7 @@ public class BridgeXmlBlockParserTest extends TestCase { //------------ /** - * Quick'n'dirty debug helper that dumps an XML structure to stdout. + * Quick 'n' dirty debug helper that dumps an XML structure to stdout. */ @SuppressWarnings("unused") private void dump(Node node, String prefix) { @@ -104,7 +125,82 @@ public class BridgeXmlBlockParserTest extends TestCase { if (n != null) { dump(n, prefix); } + } + @AfterClass + public void tearDown() { + ParserFactory.setLayoutlibCallback(null); } + private static class LayoutlibTestCallback extends LayoutlibCallback { + + @NonNull + @Override + public XmlPullParser createParser(String displayName) throws XmlPullParserException { + return new KXmlParser(); + } + + @Override + public boolean supports(int ideFeature) { + throw new AssertionError(); + } + + @Override + public Object loadView(String name, Class[] constructorSignature, Object[] constructorArgs) + throws Exception { + throw new AssertionError(); + } + + @Override + public String getNamespace() { + throw new AssertionError(); + } + + @Override + @SuppressWarnings("deprecation") + public Pair<ResourceType, String> resolveResourceId(int id) { + throw new AssertionError(); + } + + @Override + public String resolveResourceId(int[] id) { + throw new AssertionError(); + } + + @Override + public Integer getResourceId(ResourceType type, String name) { + throw new AssertionError(); + } + + @Override + @SuppressWarnings("deprecation") + public ILayoutPullParser getParser(String layoutName) { + throw new AssertionError(); + } + + @Override + public ILayoutPullParser getParser(ResourceValue layoutResource) { + throw new AssertionError(); + } + + @Override + public Object getAdapterItemValue(ResourceReference adapterView, Object adapterCookie, + ResourceReference itemRef, int fullPosition, int positionPerType, + int fullParentPosition, int parentPositionPerType, ResourceReference viewRef, + ViewAttribute viewAttribute, Object defaultValue) { + throw new AssertionError(); + } + + @Override + public AdapterBinding getAdapterBinding(ResourceReference adapterViewRef, + Object adapterCookie, + Object viewObject) { + throw new AssertionError(); + } + + @Override + public ActionBarCallback getActionBarCallback() { + throw new AssertionError(); + } + } } diff --git a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java index 5b648ef..b1a1f4d 100644 --- a/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java +++ b/tools/layoutlib/bridge/tests/src/com/android/layoutlib/bridge/intensive/setup/LayoutLibTestCallback.java @@ -17,6 +17,8 @@ package com.android.layoutlib.bridge.intensive.setup; import com.android.SdkConstants; +import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.ide.common.rendering.api.ActionBarCallback; import com.android.ide.common.rendering.api.AdapterBinding; import com.android.ide.common.rendering.api.ILayoutPullParser; @@ -28,6 +30,9 @@ import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.utils.ILogger; +import org.kxml2.io.KXmlParser; +import org.xmlpull.v1.XmlPullParser; + import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -155,4 +160,10 @@ public class LayoutLibTestCallback extends LayoutlibCallback { public boolean supports(int ideFeature) { return false; } + + @NonNull + @Override + public XmlPullParser createParser(@Nullable String name) { + return new KXmlParser(); + } } |