mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8213325: (props) Properties.loadFromXML does not fully comply with the spec
Reviewed-by: alanb, rriggs, dfuchs, naoto
This commit is contained in:
parent
f0279fd499
commit
bffe795d2d
18 changed files with 131 additions and 243 deletions
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue