8005248: (props) Integrate small footprint parser into Properties
authoralanb
Wed, 19 Dec 2012 14:53:33 +0000
changeset 14909 eded175efec2
parent 14908 1d01112e9ce5
child 14910 337380568515
8005248: (props) Integrate small footprint parser into Properties Reviewed-by: joehw, mchung, psandoz, erikj
jdk/make/jdk/Makefile
jdk/make/jdk/asm/Makefile
jdk/src/share/classes/java/util/Properties.java
jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java
jdk/test/java/util/Properties/LoadAndStoreXML.java
jdk/test/java/util/Properties/invalidxml/BadCase.xml
jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded
jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml
jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded
jdk/test/java/util/Properties/invalidxml/NoRoot.xml
jdk/test/java/util/Properties/invalidxml/NotQuoted.xml
jdk/test/java/util/Properties/invalidxml/README.txt
--- a/jdk/make/jdk/Makefile	Wed Dec 19 12:09:10 2012 +0000
+++ b/jdk/make/jdk/Makefile	Wed Dec 19 14:53:33 2012 +0000
@@ -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
--- a/jdk/make/jdk/asm/Makefile	Wed Dec 19 12:09:10 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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
--- a/jdk/src/share/classes/java/util/Properties.java	Wed Dec 19 12:09:10 2012 +0000
+++ b/jdk/src/share/classes/java/util/Properties.java	Wed Dec 19 14:53:33 2012 +0000
@@ -1188,7 +1188,7 @@
                         provider = loadProviderAsService(cl);
                         if (provider != null)
                             return provider;
-                        throw new InternalError("No fallback");
+                        return new jdk.internal.util.xml.BasicXmlPropertiesProvider();
                 }});
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/jdk/internal/util/xml/BasicXmlPropertiesProvider.java	Wed Dec 19 14:53:33 2012 +0000
@@ -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);
+    }
+}
--- a/jdk/test/java/util/Properties/LoadAndStoreXML.java	Wed Dec 19 12:09:10 2012 +0000
+++ b/jdk/test/java/util/Properties/LoadAndStoreXML.java	Wed Dec 19 14:53:33 2012 +0000
@@ -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 @@
      * 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 @@
      * 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 @@
         }
     }
 
-     /**
-      * 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 = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
                    "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
                    "<properties>" +
@@ -128,6 +136,7 @@
      * 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 @@
         } catch (UnsupportedEncodingException expected) { }
     }
 
+    /**
+     * Test loadFromXML with malformed documents
+     */
+    static void testLoadWithMalformedDoc(Path dir) throws IOException {
+        try (DirectoryStream<Path> 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 @@
         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"),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/BadCase.xml	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- XML tags are case sensitve, opening and closing tags must use same case -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
+</PROPERTIES>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/BadDocType.xml.excluded	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE namevaluepairs SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- The root element for a XML properties document is properties -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NoClosingTag.xml	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- XML elements must having a closing tag -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NoDocType.xml.excluded	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+
+<!-- An XML properties document has the DOCTYPE declaration with the properties root element -->
+
+<properties>
+<entry key="foo">bar</entry>
+<entry key="gus">baz</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NoRoot.xml	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- XML documents must have a root element -->
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/NotQuoted.xml	Wed Dec 19 14:53:33 2012 +0000
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+
+<!-- XML attribute values must be quoted -->
+
+<properties>
+<entry key=foo>bar</entry>
+<entry key=gus>baz</entry>
+</properties>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Properties/invalidxml/README.txt	Wed Dec 19 14:53:33 2012 +0000
@@ -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.
+