8005248: (props) Integrate small footprint parser into Properties
Reviewed-by: joehw, mchung, psandoz, erikj
This commit is contained in:
parent
122ca6521e
commit
4ce906db2b
jdk
make/jdk
src/share/classes
test/java/util/Properties
@ -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
|
||||
|
@ -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
|
@ -1188,7 +1188,7 @@ class Properties extends Hashtable<Object,Object> {
|
||||
provider = loadProviderAsService(cl);
|
||||
if (provider != null)
|
||||
return provider;
|
||||
throw new InternalError("No fallback");
|
||||
return new jdk.internal.util.xml.BasicXmlPropertiesProvider();
|
||||
}});
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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 = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
|
||||
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
|
||||
"<properties>" +
|
||||
@ -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<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 @@ 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"),
|
||||
|
9
jdk/test/java/util/Properties/invalidxml/BadCase.xml
Normal file
9
jdk/test/java/util/Properties/invalidxml/BadCase.xml
Normal file
@ -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>
|
@ -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>
|
@ -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>
|
@ -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>
|
4
jdk/test/java/util/Properties/invalidxml/NoRoot.xml
Normal file
4
jdk/test/java/util/Properties/invalidxml/NoRoot.xml
Normal file
@ -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 -->
|
9
jdk/test/java/util/Properties/invalidxml/NotQuoted.xml
Normal file
9
jdk/test/java/util/Properties/invalidxml/NotQuoted.xml
Normal file
@ -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>
|
5
jdk/test/java/util/Properties/invalidxml/README.txt
Normal file
5
jdk/test/java/util/Properties/invalidxml/README.txt
Normal file
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user