8213325: (props) Properties.loadFromXML does not fully comply with the spec

Reviewed-by: alanb, rriggs, dfuchs, naoto
This commit is contained in:
Joe Wang 2018-12-04 14:53:00 -08:00
parent f0279fd499
commit bffe795d2d
18 changed files with 131 additions and 243 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2018, 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
@ -73,7 +73,6 @@ package jdk.internal.org.xml.sax;
*/
public interface DTDHandler {
/**
* Receive notification of a notation declaration event.
*
@ -136,6 +135,39 @@ public interface DTDHandler {
String notationName)
throws SAXException;
// from SAX2 extension DeclHandler
/**
* Receive notification of the start of DTD declarations.
*
* The start/endDTD events appear within the start/endDocument events
* from ContentHandler.
*
* @param name The document type name.
* @param publicId The declared public identifier for the
* external DTD subset, or null if none was declared.
* @param systemId The declared system identifier for the
* external DTD subset, or null if none was declared.
* (Note that this is not resolved against the document
* base URI.)
* @throws SAXException the event receiver may throw an exception during processing
*/
default public void startDTD (String name, String publicId, String systemId)
throws SAXException
{
// no op
}
// Custom API for the Properties
/**
* Receive notification of the start of DTD internal subset.
*
* @throws SAXException the event receiver may throw an exception during processing
*/
default public void startInternalSub () throws SAXException
{
// no op
}
}
// end of DTDHandler.java

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, 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
@ -54,11 +54,11 @@ public class PropertiesDefaultHandler extends DefaultHandler {
private static final String ATTR_KEY = "key";
// The required DTD URI for exported properties
private static final String PROPS_DTD_DECL =
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">";
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">";
private static final String PROPS_DTD_URI =
"http://java.sun.com/dtd/properties.dtd";
"http://java.sun.com/dtd/properties.dtd";
private static final String PROPS_DTD =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<!-- DTD for properties -->"
+ "<!ELEMENT properties ( comment?, entry* ) >"
+ "<!ATTLIST properties"
@ -136,15 +136,15 @@ public class PropertiesDefaultHandler extends DefaultHandler {
////////////////////////////////////////////////////////////////////
// Validate while parsing
////////////////////////////////////////////////////////////////////
static final String ALLOWED_ELEMENTS = "properties, comment, entry";
static final String ALLOWED_ELEMENTS = "comment, entry";
static final String ALLOWED_COMMENT = "comment";
////////////////////////////////////////////////////////////////////
// Handler methods
////////////////////////////////////////////////////////////////////
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
boolean sawRoot = false; // whether a valid root element exists
boolean sawComment = false;
boolean validEntry = false;
int rootElem = 0;
String key;
String rootElm;
@ -152,32 +152,38 @@ public class PropertiesDefaultHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
if (rootElem < 2) {
rootElem++;
}
if (rootElm == null) {
fatalError(new SAXParseException("An XML properties document must contain"
if (sawRoot) {
if (!ALLOWED_ELEMENTS.contains(qName)) {
fatalError(new SAXParseException("Element type \"" + qName + "\" must be declared.", null));
}
} else {
// check whether the root has been declared in the DTD
if (rootElm == null) {
fatalError(new SAXParseException("An XML properties document must contain"
+ " the DOCTYPE declaration as defined by java.util.Properties.", null));
}
// check whether the element name matches the declaration
if (!rootElm.equals(qName)) {
fatalError(new SAXParseException("Document root element \"" + qName
+ "\", must match DOCTYPE root \"" + rootElm + "\"", null));
}
// this is a valid root element
sawRoot = true;
}
if (rootElem == 1 && !rootElm.equals(qName)) {
fatalError(new SAXParseException("Document root element \"" + qName
+ "\", must match DOCTYPE root \"" + rootElm + "\"", null));
}
if (!ALLOWED_ELEMENTS.contains(qName)) {
fatalError(new SAXParseException("Element type \"" + qName + "\" must be declared.", null));
}
if (qName.equals(ELEMENT_ENTRY)) {
validEntry = true;
key = attributes.getValue(ATTR_KEY);
if (key == null) {
fatalError(new SAXParseException("Attribute \"key\" is required and must be specified for element type \"entry\"", null));
fatalError(new SAXParseException("Attribute \"key\" is required and " +
"must be specified for element type \"entry\"", null));
}
} else if (qName.equals(ALLOWED_COMMENT)) {
if (sawComment) {
fatalError(new SAXParseException("Only one comment element may be allowed. "
+ "The content of element type \"properties\" must match \"(comment?,entry*)\"", null));
+ "The content of element type \"properties\" must match \"(comment?,entry*)\"", null));
}
sawComment = true;
}
@ -192,8 +198,9 @@ public class PropertiesDefaultHandler extends DefaultHandler {
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (!ALLOWED_ELEMENTS.contains(qName)) {
fatalError(new SAXParseException("Element: " + qName + " is invalid, must match \"(comment?,entry*)\".", null));
if (!ALLOWED_ELEMENTS.contains(qName) && !ELEMENT_ROOT.equals(qName)) {
fatalError(new SAXParseException("Element: " + qName +
" is invalid, must match \"(comment?,entry*)\".", null));
}
if (validEntry) {
@ -203,20 +210,13 @@ public class PropertiesDefaultHandler extends DefaultHandler {
}
}
@Override
public void notationDecl(String name, String publicId, String systemId) throws SAXException {
rootElm = name;
}
@Override
public InputSource resolveEntity(String pubid, String sysid)
throws SAXException, IOException {
throws SAXException, IOException {
{
if (sysid.equals(PROPS_DTD_URI)) {
InputSource is;
is = new InputSource(new StringReader(PROPS_DTD));
is.setSystemId(PROPS_DTD_URI);
return is;
// The properties DTD is known to the handler, no need to parse it
return null;
}
throw new SAXException("Invalid system identifier: " + sysid);
}
@ -236,4 +236,24 @@ public class PropertiesDefaultHandler extends DefaultHandler {
public void warning(SAXParseException x) throws SAXException {
throw x;
}
// SAX2 extension from DTDHandler
@Override
public void startDTD (String name, String publicId, String systemId) throws SAXException
{
if (!ELEMENT_ROOT.equals(name) || !PROPS_DTD_URI.equals(systemId)) {
fatalError(new SAXParseException("An XML properties document must contain"
+ " the DOCTYPE declaration as defined by java.util.Properties.", null));
}
rootElm = name;
}
@Override
public void startInternalSub () throws SAXException
{
fatalError(new SAXParseException("Internal DTD subset is not allowed. " +
"The Properties XML document must have the following DOCTYPE declaration: \n" +
PROPS_DTD_DECL, null));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, 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
@ -650,6 +650,8 @@ public abstract class Parser {
* @exception IOException
*/
private void dtdsub() throws Exception {
startInternalSub(); // reports the event before parsing the subset
char ch;
for (short st = 0; st >= 0;) {
ch = getch();
@ -2230,6 +2232,13 @@ public abstract class Parser {
protected abstract void docType(String name, String pubid, String sysid)
throws SAXException;
/**
* Reports the start of DTD internal subset.
*
* @throws SAXException if the receiver throws SAXException
*/
public abstract void startInternalSub () throws SAXException;
/**
* Reports a comment.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, 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
@ -551,7 +551,16 @@ final class ParserSAX
* @param sysid The system identifier of the entity or <code>null</code>.
*/
protected void docType(String name, String pubid, String sysid) throws SAXException {
mHandDtd.notationDecl(name, pubid, sysid);
mHandDtd.startDTD(name, pubid, sysid);
}
/**
* Reports the start of DTD internal subset.
*
* @throws SAXException if the receiver throws SAXException
*/
public void startInternalSub () throws SAXException {
mHandDtd.startInternalSub();
}
/**

View File

@ -1,105 +0,0 @@
/*
* 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.
*
* 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.
*/
/*
* @test
* @bug 8005280 8004371
* @summary Compatibility test
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* This is a behavior compatibility test.
* Although not defined by the properties.dtd, the constructs
* in Compatibility.xml are supported by the regular JDK XML
* Provider.
*
* @author: Joe Wang
*/
public class CompatibilityTest {
public static void main(String[] args) {
testInternalDTD();
}
/*
* Not in the spec, but the constructs work with the current JDK
*/
static void testInternalDTD() {
String src = System.getProperty("test.src");
if (src == null) {
src = ".";
}
loadPropertyFile(src + "/Compatibility.xml");
}
/*
* 'Store' the populated 'Property' with the specified 'Encoding Type' as an
* XML file. Retrieve the same XML file and 'load' onto a new 'Property' object.
*/
static void loadPropertyFile(String filename) {
try (InputStream in = new FileInputStream(filename)) {
Properties prop = new Properties();
prop.loadFromXML(in);
verifyProperites(prop);
} catch (IOException ex) {
fail(ex.getMessage());
}
}
/*
* This method verifies the first key-value with the original string.
*/
static void verifyProperites(Properties prop) {
try {
for (String key : prop.stringPropertyNames()) {
String val = prop.getProperty(key);
if (key.equals("Key1")) {
if (!val.equals("value1")) {
fail("Key:" + key + "'s value: \nExpected: value1\nFound: " + val);
}
} else if (key.equals("Key2")) {
if (!val.equals("<value2>")) {
fail("Key:" + key + "'s value: \nExpected: <value2>\nFound: " + val);
}
} else if (key.equals("Key3")) {
if (!val.equals("value3")) {
fail("Key:" + key + "'s value: \nExpected: value3\nFound: " + val);
}
}
}
} catch (Exception e) {
fail(e.getMessage());
}
}
static void fail(String err) {
throw new RuntimeException(err);
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<!-- The dtd requires a properties root, but not element -->
<properties>
<comment>comment</comment>
<properties>
<entry key="firstKey">value of the first key</entry>
</properties>
</properties>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for XMLResourceBundleTest.java</comment>
<entry key="type">XML</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for XMLResourceBundleTest.java</comment>
<entry key="type"></entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle</comment>
<entry key="key">root: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle</comment>
<entry key="key">de: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle</comment>
<entry key="key">en: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle</comment>
<entry key="key">fr: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle in named modules.</comment>
<entry key="key">ja: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle in named modules.</comment>
<entry key="key">zh: message</entry>

View File

@ -2,15 +2,7 @@
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for ResourceBundle in named modules.</comment>
<entry key="key">zh-TW: message</entry>

View File

@ -23,15 +23,7 @@
-->
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for UserDefaultControlTest.java</comment>
<entry key="type">XML</entry>

View File

@ -23,15 +23,7 @@
-->
<!---->
<!-- DTD for properties -->
<!DOCTYPE properties [
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
]>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Test data for UserDefaultControlTest.java</comment>
<entry key="type"></entry>