mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 19:14:38 +02:00
8144967: javax.xml.transform.Source and org.xml.sax.InputSource can be empty
Reviewed-by: darcy, rriggs
This commit is contained in:
parent
180c59d147
commit
a4d59b2815
7 changed files with 381 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2016, 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
|
||||
|
@ -52,4 +52,17 @@ public interface Source {
|
|||
* if setSystemId was not called.
|
||||
*/
|
||||
public String getSystemId();
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code Source} object is empty. Empty means
|
||||
* that there is no input available from this Source.
|
||||
*
|
||||
* @implSpec The default implementation of this method throws
|
||||
* {@link UnsupportedOperationException}.
|
||||
*
|
||||
* @return true if the {@code Source} object is empty, false otherwise
|
||||
*/
|
||||
default boolean isEmpty() {
|
||||
throw new UnsupportedOperationException("The isEmpty method is not supported.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2016, 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
|
||||
|
@ -122,6 +122,7 @@ public class DOMSource implements Source {
|
|||
*
|
||||
* @param systemID Base URL for this DOM tree.
|
||||
*/
|
||||
@Override
|
||||
public void setSystemId(String systemID) {
|
||||
this.systemID = systemID;
|
||||
}
|
||||
|
@ -132,7 +133,25 @@ public class DOMSource implements Source {
|
|||
*
|
||||
* @return Base URL for this DOM tree.
|
||||
*/
|
||||
@Override
|
||||
public String getSystemId() {
|
||||
return this.systemID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code DOMSource} object is empty. Empty is
|
||||
* defined as follows:
|
||||
* <ul>
|
||||
* <li>if the system identifier and node are {@code null};
|
||||
* </li>
|
||||
* <li>if the system identifier is null, and the {@code node} has no child nodes.
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
* @return true if the {@code DOMSource} object is empty, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return systemID == null && (node == null || !node.hasChildNodes());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2016, 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
|
||||
|
@ -147,6 +147,7 @@ public class SAXSource implements Source {
|
|||
*
|
||||
* @param systemId The system identifier as a URI string.
|
||||
*/
|
||||
@Override
|
||||
public void setSystemId(String systemId) {
|
||||
|
||||
if (null == inputSource) {
|
||||
|
@ -162,6 +163,7 @@ public class SAXSource implements Source {
|
|||
*
|
||||
* @return Base URL for the <code>Source</code>, or <code>null</code>.
|
||||
*/
|
||||
@Override
|
||||
public String getSystemId() {
|
||||
|
||||
if (inputSource == null) {
|
||||
|
@ -207,4 +209,22 @@ public class SAXSource implements Source {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code SAXSource} object is empty. Empty is
|
||||
* defined as follows:
|
||||
* <ul>
|
||||
* <li>if the system identifier and {@code InputSource} are {@code null};
|
||||
* </li>
|
||||
* <li>if the system identifier is {@code null}, and the {@code InputSource}
|
||||
* is empty.
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
* @return true if the {@code SAXSource} object is empty, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return getSystemId() == null && (inputSource == null || inputSource.isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2016, 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
|
||||
|
@ -209,6 +209,7 @@ public class StAXSource implements Source {
|
|||
* @throws UnsupportedOperationException Is <strong>always</strong>
|
||||
* thrown by this method.
|
||||
*/
|
||||
@Override
|
||||
public void setSystemId(final String systemId) {
|
||||
|
||||
throw new UnsupportedOperationException(
|
||||
|
@ -229,8 +230,21 @@ public class StAXSource implements Source {
|
|||
*
|
||||
* @return System identifier used by this <code>StAXSource</code>.
|
||||
*/
|
||||
@Override
|
||||
public String getSystemId() {
|
||||
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code StAXSource} object is empty. Since a
|
||||
* {@code StAXSource} object can never be empty, this method always returns
|
||||
* false.
|
||||
*
|
||||
* @return unconditionally false
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2016, 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
|
||||
|
@ -26,8 +26,10 @@
|
|||
package javax.xml.transform.stream;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
|
@ -233,6 +235,7 @@ public class StreamSource implements Source {
|
|||
*
|
||||
* @param systemId The system identifier as a URL string.
|
||||
*/
|
||||
@Override
|
||||
public void setSystemId(String systemId) {
|
||||
this.systemId = systemId;
|
||||
}
|
||||
|
@ -243,6 +246,7 @@ public class StreamSource implements Source {
|
|||
* @return The system identifier that was set with setSystemId, or null
|
||||
* if setSystemId was not called.
|
||||
*/
|
||||
@Override
|
||||
public String getSystemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
@ -259,6 +263,59 @@ public class StreamSource implements Source {
|
|||
this.systemId = f.toURI().toASCIIString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code StreamSource} object is empty. Empty is
|
||||
* defined as follows:
|
||||
* <ul>
|
||||
* <li>All of the input sources, including the public identifier, system
|
||||
* identifier, byte stream, and character stream, are {@code null}.
|
||||
* </li>
|
||||
* <li>The public identifier and system identifier are {@code null}, and
|
||||
* byte and character stream are either {@code null} or contain no byte or
|
||||
* character.
|
||||
* <p>
|
||||
* Note that this method will reset the byte stream if it is provided, or
|
||||
* the character stream if the byte stream is not provided.
|
||||
* </li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* In case of error while checking the byte or character stream, the method
|
||||
* will return false to allow the XML processor to handle the error.
|
||||
*
|
||||
* @return true if the {@code StreamSource} object is empty, false otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return (publicId == null && systemId == null && isStreamEmpty());
|
||||
}
|
||||
|
||||
private boolean isStreamEmpty() {
|
||||
boolean empty = true;
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.reset();
|
||||
int bytesRead = inputStream.available();
|
||||
if (bytesRead > 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (reader != null) {
|
||||
reader.reset();
|
||||
int c = reader.read();
|
||||
reader.reset();
|
||||
if (c != -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
//in case of error, return false
|
||||
return false;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Internal state.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue