Merge "Move XML object factory logic to libcore"
This commit is contained in:
@@ -16,27 +16,27 @@
|
|||||||
|
|
||||||
package android.util;
|
package android.util;
|
||||||
|
|
||||||
import java.io.IOException;
|
import libcore.util.XmlObjectFactory;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import org.apache.harmony.xml.ExpatReader;
|
|
||||||
import org.kxml2.io.KXmlParser;
|
|
||||||
import org.xml.sax.ContentHandler;
|
import org.xml.sax.ContentHandler;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
import org.xmlpull.v1.XmlPullParserFactory;
|
|
||||||
import org.xmlpull.v1.XmlSerializer;
|
import org.xmlpull.v1.XmlSerializer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XML utility methods.
|
* XML utility methods.
|
||||||
*/
|
*/
|
||||||
public class Xml {
|
public class Xml {
|
||||||
/** @hide */ public Xml() {}
|
private Xml() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name.
|
* {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name.
|
||||||
@@ -52,7 +52,7 @@ public class Xml {
|
|||||||
public static void parse(String xml, ContentHandler contentHandler)
|
public static void parse(String xml, ContentHandler contentHandler)
|
||||||
throws SAXException {
|
throws SAXException {
|
||||||
try {
|
try {
|
||||||
XMLReader reader = new ExpatReader();
|
XMLReader reader = XmlObjectFactory.newXMLReader();
|
||||||
reader.setContentHandler(contentHandler);
|
reader.setContentHandler(contentHandler);
|
||||||
reader.parse(new InputSource(new StringReader(xml)));
|
reader.parse(new InputSource(new StringReader(xml)));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -66,7 +66,7 @@ public class Xml {
|
|||||||
*/
|
*/
|
||||||
public static void parse(Reader in, ContentHandler contentHandler)
|
public static void parse(Reader in, ContentHandler contentHandler)
|
||||||
throws IOException, SAXException {
|
throws IOException, SAXException {
|
||||||
XMLReader reader = new ExpatReader();
|
XMLReader reader = XmlObjectFactory.newXMLReader();
|
||||||
reader.setContentHandler(contentHandler);
|
reader.setContentHandler(contentHandler);
|
||||||
reader.parse(new InputSource(in));
|
reader.parse(new InputSource(in));
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ public class Xml {
|
|||||||
*/
|
*/
|
||||||
public static void parse(InputStream in, Encoding encoding,
|
public static void parse(InputStream in, Encoding encoding,
|
||||||
ContentHandler contentHandler) throws IOException, SAXException {
|
ContentHandler contentHandler) throws IOException, SAXException {
|
||||||
XMLReader reader = new ExpatReader();
|
XMLReader reader = XmlObjectFactory.newXMLReader();
|
||||||
reader.setContentHandler(contentHandler);
|
reader.setContentHandler(contentHandler);
|
||||||
InputSource source = new InputSource(in);
|
InputSource source = new InputSource(in);
|
||||||
source.setEncoding(encoding.expatName);
|
source.setEncoding(encoding.expatName);
|
||||||
@@ -89,7 +89,7 @@ public class Xml {
|
|||||||
*/
|
*/
|
||||||
public static XmlPullParser newPullParser() {
|
public static XmlPullParser newPullParser() {
|
||||||
try {
|
try {
|
||||||
KXmlParser parser = new KXmlParser();
|
XmlPullParser parser = XmlObjectFactory.newXmlPullParser();
|
||||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
|
parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
|
||||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
||||||
return parser;
|
return parser;
|
||||||
@@ -102,25 +102,7 @@ public class Xml {
|
|||||||
* Creates a new xml serializer.
|
* Creates a new xml serializer.
|
||||||
*/
|
*/
|
||||||
public static XmlSerializer newSerializer() {
|
public static XmlSerializer newSerializer() {
|
||||||
try {
|
return XmlObjectFactory.newXmlSerializer();
|
||||||
return XmlSerializerFactory.instance.newSerializer();
|
|
||||||
} catch (XmlPullParserException e) {
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Factory for xml serializers. Initialized on demand. */
|
|
||||||
static class XmlSerializerFactory {
|
|
||||||
static final String TYPE
|
|
||||||
= "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer";
|
|
||||||
static final XmlPullParserFactory instance;
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
instance = XmlPullParserFactory.newInstance(TYPE, null);
|
|
||||||
} catch (XmlPullParserException e) {
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,125 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2007 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package android.sax;
|
|
||||||
|
|
||||||
import android.test.AndroidTestCase;
|
|
||||||
import android.test.suitebuilder.annotation.LargeTest;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.util.Xml;
|
|
||||||
import org.kxml2.io.KXmlParser;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import com.android.frameworks.saxtests.R;
|
|
||||||
|
|
||||||
public class ExpatPerformanceTest extends AndroidTestCase {
|
|
||||||
|
|
||||||
private static final String TAG = ExpatPerformanceTest.class.getSimpleName();
|
|
||||||
|
|
||||||
private byte[] mXmlBytes;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
super.setUp();
|
|
||||||
InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int length;
|
|
||||||
while ((length = in.read(buffer)) != -1) {
|
|
||||||
out.write(buffer, 0, length);
|
|
||||||
}
|
|
||||||
mXmlBytes = out.toByteArray();
|
|
||||||
|
|
||||||
Log.i("***", "File size: " + (mXmlBytes.length / 1024) + "k");
|
|
||||||
}
|
|
||||||
|
|
||||||
@LargeTest
|
|
||||||
public void testPerformance() throws Exception {
|
|
||||||
// try {
|
|
||||||
// Debug.startMethodTracing("expat3");
|
|
||||||
// for (int i = 0; i < 1; i++) {
|
|
||||||
runJavaPullParser();
|
|
||||||
runSax();
|
|
||||||
runExpatPullParser();
|
|
||||||
// }
|
|
||||||
// } finally {
|
|
||||||
// Debug.stopMethodTracing();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
private InputStream newInputStream() {
|
|
||||||
return new ByteArrayInputStream(mXmlBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runSax() throws IOException, SAXException {
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
|
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
|
||||||
Log.i(TAG, "expat SAX: " + elapsed + "ms");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runExpatPullParser() throws XmlPullParserException, IOException {
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
XmlPullParser pullParser = Xml.newPullParser();
|
|
||||||
pullParser.setInput(newInputStream(), "UTF-8");
|
|
||||||
withPullParser(pullParser);
|
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
|
||||||
Log.i(TAG, "expat pull: " + elapsed + "ms");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void runJavaPullParser() throws XmlPullParserException, IOException {
|
|
||||||
XmlPullParser pullParser;
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
pullParser = new KXmlParser();
|
|
||||||
pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
|
||||||
pullParser.setInput(newInputStream(), "UTF-8");
|
|
||||||
withPullParser(pullParser);
|
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
|
||||||
Log.i(TAG, "java pull parser: " + elapsed + "ms");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void withPullParser(XmlPullParser pullParser)
|
|
||||||
throws IOException, XmlPullParserException {
|
|
||||||
int eventType = pullParser.next();
|
|
||||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
|
||||||
switch (eventType) {
|
|
||||||
case XmlPullParser.START_TAG:
|
|
||||||
pullParser.getName();
|
|
||||||
// int nattrs = pullParser.getAttributeCount();
|
|
||||||
// for (int i = 0; i < nattrs; ++i) {
|
|
||||||
// pullParser.getAttributeName(i);
|
|
||||||
// pullParser.getAttributeValue(i);
|
|
||||||
// }
|
|
||||||
break;
|
|
||||||
case XmlPullParser.END_TAG:
|
|
||||||
pullParser.getName();
|
|
||||||
break;
|
|
||||||
case XmlPullParser.TEXT:
|
|
||||||
pullParser.getText();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
eventType = pullParser.next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user