From: Jesse Wilson Date: Tue, 2 Feb 2010 19:18:33 +0000 (-0800) Subject: Restore our ability to parse an XML Document given a File argument X-Git-Tag: android-x86-2.2~208 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=e4402d94d3bed9d3b8a0aa0496db177e6dfd1473;p=android-x86%2Fdalvik.git Restore our ability to parse an XML Document given a File argument directly, rather than via a stream. When I updated DocumentBuilder but not its subclass DocumentBuilderImpl, some of the assumptions by DocumentBuilderImpl were violated. --- diff --git a/libcore/xml/src/main/java/org/apache/harmony/xml/parsers/DocumentBuilderImpl.java b/libcore/xml/src/main/java/org/apache/harmony/xml/parsers/DocumentBuilderImpl.java index eacf0a070..5a3c48c02 100644 --- a/libcore/xml/src/main/java/org/apache/harmony/xml/parsers/DocumentBuilderImpl.java +++ b/libcore/xml/src/main/java/org/apache/harmony/xml/parsers/DocumentBuilderImpl.java @@ -17,6 +17,8 @@ package org.apache.harmony.xml.parsers; import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; @@ -122,10 +124,16 @@ class DocumentBuilderImpl extends DocumentBuilder { parser.setInput(source.getByteStream(), source.getEncoding()); } else if (source.getCharacterStream() != null) { parser.setInput(source.getCharacterStream()); + } else if (source.getSystemId() != null) { + URL url = new URL(source.getSystemId()); + URLConnection urlConnection = url.openConnection(); + urlConnection.connect(); + String encoding = source.getEncoding(); + // TODO: if null, extract the encoding from the Content-Type header? + parser.setInput(urlConnection.getInputStream(), encoding); } else { - // TODO Accept other sources as well? throw new SAXParseException( - "InputSource needs either stream or reader", null); + "InputSource needs a stream, reader or URI", null); } if(parser.nextToken() == XmlPullParser.END_DOCUMENT) { diff --git a/libcore/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java b/libcore/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java index 292c2f1a7..02b6d8025 100644 --- a/libcore/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java +++ b/libcore/xml/src/test/java/tests/api/javax/xml/parsers/DocumentBuilderTest.java @@ -16,40 +16,35 @@ package tests.api.javax.xml.parsers; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.logging.Logger; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - +import dalvik.annotation.KnownFailure; +import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; +import dalvik.annotation.TestTargetNew; import junit.framework.TestCase; - import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.EntityReference; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; - import tests.api.org.xml.sax.support.MethodLogger; import tests.api.org.xml.sax.support.MockHandler; import tests.api.org.xml.sax.support.MockResolver; -import dalvik.annotation.KnownFailure; -import dalvik.annotation.TestLevel; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargetNew; +import tests.util.TestEnvironment; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; @TestTargetClass(DocumentBuilder.class) public class DocumentBuilderTest extends TestCase { @@ -133,6 +128,8 @@ public class DocumentBuilderTest extends TestCase { DocumentBuilder db; protected void setUp() throws Exception { + TestEnvironment.reset(); + dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); @@ -266,6 +263,21 @@ public class DocumentBuilderTest extends TestCase { } /** + * Tests that the Base URI for the document is populated with the file URI. + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "parse", + args = {java.io.File.class} + ) + public void testGetBaseURI() throws IOException, SAXException { + File f = resourceToTmpFile("/simple.xml"); + Document d = db.parse(f); + assertTrue(d.getDocumentElement().getBaseURI().startsWith("file://")); + } + + /** * @tests javax.xml.parsers.DocumentBuilder#parse(java.io.File) * Case 1: Try to parse correct xml document. * Case 2: Try to call parse() with null argument. @@ -567,7 +579,6 @@ public class DocumentBuilderTest extends TestCase { method = "parse", args = {java.lang.String.class} ) - @KnownFailure("Android DocumentBuilder should support File sources") public void test_parseLjava_lang_String() { // case 1: Trivial use. File f = new File(getClass().getResource("/simple.xml").getFile());