From 4ce906db2b241d521d2c3feac540eef9c0695f81 Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Wed, 19 Dec 2012 14:53:33 +0000 Subject: [PATCH] 8005248: (props) Integrate small footprint parser into Properties Reviewed-by: joehw, mchung, psandoz, erikj --- jdk/make/jdk/Makefile | 20 ++++--- jdk/make/jdk/asm/Makefile | 40 ------------- .../share/classes/java/util/Properties.java | 2 +- .../util/xml/BasicXmlPropertiesProvider.java | 59 +++++++++++++++++++ .../java/util/Properties/LoadAndStoreXML.java | 45 ++++++++++++-- .../util/Properties/invalidxml/BadCase.xml | 9 +++ .../invalidxml/BadDocType.xml.excluded | 9 +++ .../Properties/invalidxml/NoClosingTag.xml | 8 +++ .../invalidxml/NoDocType.xml.excluded | 8 +++ .../util/Properties/invalidxml/NoRoot.xml | 4 ++ .../util/Properties/invalidxml/NotQuoted.xml | 9 +++ .../util/Properties/invalidxml/README.txt | 5 ++ 12 files changed, 163 insertions(+), 55 deletions(-) delete mode 100644 jdk/make/jdk/asm/Makefile create mode 100644 jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java create mode 100644 jdk/test/java/util/Properties/invalidxml/BadCase.xml create mode 100644 jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded create mode 100644 jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml create mode 100644 jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded create mode 100644 jdk/test/java/util/Properties/invalidxml/NoRoot.xml create mode 100644 jdk/test/java/util/Properties/invalidxml/NotQuoted.xml create mode 100644 jdk/test/java/util/Properties/invalidxml/README.txt diff --git a/jdk/make/jdk/Makefile b/jdk/make/jdk/Makefile index b8ac7422749..686c94a5758 100644 --- a/jdk/make/jdk/Makefile +++ b/jdk/make/jdk/Makefile @@ -1,5 +1,5 @@ # -# Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -23,16 +23,18 @@ # questions. # -# -# Makefile for building all of java -# - BUILDDIR = .. +PACKAGE = jdk PRODUCT = jdk +JAVAC_LINT_OPTIONS=-Xlint:all include $(BUILDDIR)/common/Defs.gmk -SUBDIRS = asm -include $(BUILDDIR)/common/Subdirs.gmk +# +# Files to compile +# +AUTO_FILES_JAVA_DIRS = jdk -all build clean clobber:: - $(SUBDIRS-loop) +# +# Rules +# +include $(BUILDDIR)/common/Classes.gmk diff --git a/jdk/make/jdk/asm/Makefile b/jdk/make/jdk/asm/Makefile deleted file mode 100644 index 48b8f609644..00000000000 --- a/jdk/make/jdk/asm/Makefile +++ /dev/null @@ -1,40 +0,0 @@ -# -# Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved. -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. -# -# This code is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 only, as -# published by the Free Software Foundation. Oracle designates this -# particular file as subject to the "Classpath" exception as provided -# by Oracle in the LICENSE file that accompanied this code. -# -# This code is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -# version 2 for more details (a copy is included in the LICENSE file that -# accompanied this code). -# -# You should have received a copy of the GNU General Public License version -# 2 along with this work; if not, write to the Free Software Foundation, -# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. -# -# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA -# or visit www.oracle.com if you need additional information or have any -# questions. -# - -BUILDDIR = ../.. -PACKAGE = jdk.internal.org.objectweb.asm -PRODUCT = jdk -JAVAC_LINT_OPTIONS=-Xlint:all -include $(BUILDDIR)/common/Defs.gmk - -# -# Files to compile -# -AUTO_FILES_JAVA_DIRS = jdk/internal/org/objectweb/asm - -# -# Rules -# -include $(BUILDDIR)/common/Classes.gmk diff --git a/jdk/src/share/classes/java/util/Properties.java b/jdk/src/share/classes/java/util/Properties.java index e084479c6d9..3e899d00113 100644 --- a/jdk/src/share/classes/java/util/Properties.java +++ b/jdk/src/share/classes/java/util/Properties.java @@ -1188,7 +1188,7 @@ class Properties extends Hashtable { provider = loadProviderAsService(cl); if (provider != null) return provider; - throw new InternalError("No fallback"); + return new jdk.internal.util.xml.BasicXmlPropertiesProvider(); }}); } diff --git a/jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java b/jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java new file mode 100644 index 00000000000..654f29e399f --- /dev/null +++ b/jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.internal.util.xml; + +import java.util.Properties; +import java.util.InvalidPropertiesFormatException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +import sun.util.spi.XmlPropertiesProvider; + +/** + * A {@code XmlPropertiesProvider} implementation that uses the UKit XML parser. + */ + +public class BasicXmlPropertiesProvider extends XmlPropertiesProvider { + + private final PropertiesDefaultHandler handler = new PropertiesDefaultHandler(); + + public BasicXmlPropertiesProvider() { } + + @Override + public void load(Properties props, InputStream in) + throws IOException, InvalidPropertiesFormatException + { + handler.load(props, in); + } + + @Override + public void store(Properties props, OutputStream os, String comment, + String encoding) + throws IOException + { + handler.store(props, os, comment, encoding); + } +} diff --git a/jdk/test/java/util/Properties/LoadAndStoreXML.java b/jdk/test/java/util/Properties/LoadAndStoreXML.java index fd1d81f47e6..337b3e00a42 100644 --- a/jdk/test/java/util/Properties/LoadAndStoreXML.java +++ b/jdk/test/java/util/Properties/LoadAndStoreXML.java @@ -23,13 +23,16 @@ /* * @test - * @bug 8000354 8000685 + * @bug 8000354 8000685 8004371 * @summary Basic test of storeToXML and loadToXML + * @run main LoadAndStoreXML + * @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML */ import java.io.*; import java.util.*; import java.security.*; +import java.nio.file.*; public class LoadAndStoreXML { @@ -67,6 +70,8 @@ public class LoadAndStoreXML { * read with Properties#loadFromXML. */ static void testLoadAndStore(String encoding) throws IOException { + System.out.println("testLoadAndStore, encoding=" + encoding); + Properties props = new Properties(); props.put("k1", "foo"); props.put("k2", "bar"); @@ -89,6 +94,8 @@ public class LoadAndStoreXML { * Test loadFromXML with a document that does not have an encoding declaration */ static void testLoadWithoutEncoding() throws IOException { + System.out.println("testLoadWithoutEncoding"); + Properties expected = new Properties(); expected.put("foo", "bar"); @@ -107,10 +114,11 @@ public class LoadAndStoreXML { } } - /** - * Test loadFromXML with unsupported encoding - */ - static void testLoadWithBadEncoding() throws IOException { + /** + * Test loadFromXML with unsupported encoding + */ + static void testLoadWithBadEncoding() throws IOException { + System.out.println("testLoadWithBadEncoding"); String s = "" + "" + "" + @@ -128,6 +136,7 @@ public class LoadAndStoreXML { * Test storeToXML with unsupported encoding */ static void testStoreWithBadEncoding() throws IOException { + System.out.println("testStoreWithBadEncoding"); Properties props = new Properties(); props.put("foo", "bar"); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -137,6 +146,26 @@ public class LoadAndStoreXML { } catch (UnsupportedEncodingException expected) { } } + /** + * Test loadFromXML with malformed documents + */ + static void testLoadWithMalformedDoc(Path dir) throws IOException { + try (DirectoryStream stream = Files.newDirectoryStream(dir, "*.xml")) { + for (Path file: stream) { + System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName()); + try (InputStream in = Files.newInputStream(file)) { + Properties props = new Properties(); + try { + props.loadFromXML(in); + throw new RuntimeException("InvalidPropertiesFormatException not thrown"); + } catch (InvalidPropertiesFormatException x) { + System.out.println(x); + } + } + } + } + } + public static void main(String[] args) throws IOException { testLoadAndStore("UTF-8"); @@ -145,6 +174,12 @@ public class LoadAndStoreXML { testLoadWithBadEncoding(); testStoreWithBadEncoding(); + // malformed documents + String src = System.getProperty("test.src"); + String subdir = "invalidxml"; + Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir); + testLoadWithMalformedDoc(dir); + // re-run sanity test with security manager Policy orig = Policy.getPolicy(); Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"), diff --git a/jdk/test/java/util/Properties/invalidxml/BadCase.xml b/jdk/test/java/util/Properties/invalidxml/BadCase.xml new file mode 100644 index 00000000000..0995943df7d --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/BadCase.xml @@ -0,0 +1,9 @@ + + + + + + +bar +baz + diff --git a/jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded b/jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded new file mode 100644 index 00000000000..7aea19ec558 --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded @@ -0,0 +1,9 @@ + + + + + + +bar +baz + diff --git a/jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml b/jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml new file mode 100644 index 00000000000..f3020fb26d1 --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml @@ -0,0 +1,8 @@ + + + + + + +bar +baz diff --git a/jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded b/jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded new file mode 100644 index 00000000000..1adb54982f5 --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded @@ -0,0 +1,8 @@ + + + + + +bar +baz + diff --git a/jdk/test/java/util/Properties/invalidxml/NoRoot.xml b/jdk/test/java/util/Properties/invalidxml/NoRoot.xml new file mode 100644 index 00000000000..dbd0f45131d --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/NoRoot.xml @@ -0,0 +1,4 @@ + + + + diff --git a/jdk/test/java/util/Properties/invalidxml/NotQuoted.xml b/jdk/test/java/util/Properties/invalidxml/NotQuoted.xml new file mode 100644 index 00000000000..92448c87385 --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/NotQuoted.xml @@ -0,0 +1,9 @@ + + + + + + +bar +baz + diff --git a/jdk/test/java/util/Properties/invalidxml/README.txt b/jdk/test/java/util/Properties/invalidxml/README.txt new file mode 100644 index 00000000000..6e2a7f83b64 --- /dev/null +++ b/jdk/test/java/util/Properties/invalidxml/README.txt @@ -0,0 +1,5 @@ + +This directory contains test cases for the LoadAndStoreXML test case. Each file +in this directory should be a malformed XML document that should cause the +java.util.Properties loadFromXML method to throw InvalidPropertiesFormatException. +