Skip to content

Commit

Permalink
8249867: xml declaration is not followed by a newline
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, naoto, lancea, dfuchs
  • Loading branch information
JoeWang-Java committed Jan 30, 2021
1 parent fb46c91 commit 69ee314
Show file tree
Hide file tree
Showing 5 changed files with 477 additions and 51 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand All @@ -20,6 +20,7 @@

package com.sun.org.apache.xml.internal.serializer;

import com.sun.org.apache.xml.internal.serializer.dom3.DOMConstants;
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
import com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException;
Expand Down Expand Up @@ -51,7 +52,7 @@
* serializers (xml, html, text ...) that write output to a stream.
*
* @xsl.usage internal
* @LastModified: June 2020
* @LastModified: Jan 2021
*/
abstract public class ToStream extends SerializerBase {

Expand Down Expand Up @@ -492,8 +493,10 @@ void setProp(String name, String val, boolean defaultVal) {
if (OutputPropertiesFactory.S_KEY_INDENT_AMOUNT.equals(name)) {
setIndentAmount(Integer.parseInt(val));
} else if (OutputKeys.INDENT.equals(name)) {
boolean b = val.endsWith("yes") ? true : false;
m_doIndent = b;
m_doIndent = val.endsWith("yes");
} else if ((DOMConstants.S_JDK_PROPERTIES_NS + DOMConstants.S_IS_STANDALONE)
.equals(name)) {
m_isStandalone = val.endsWith("yes");
}

break;
Expand Down
@@ -1,6 +1,5 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -22,26 +21,34 @@

package com.sun.org.apache.xml.internal.serializer.dom3;

import com.sun.org.apache.xerces.internal.impl.Constants;

/**
* DOM Constants used by the DOM Level 3 LSSerializer implementation.
*
* @xsl.usage internal
* @LastModified: Jan 2021
*/
final class DOMConstants {
public final class DOMConstants {
//
// Constants: DOM Level 3 feature ids
//
public static final String DOM3_REC_URL = "http://www.w3.org/TR/DOM-Level-3-LS";

public static final String XERCES_URL = "http://xml.apache.org/xerces-2j";

public static final String ORACLE_URL = "http://www.oracle.com/xml";

// The namespace used to qualified DOM Level 3 DOMConfiguration parameters
public static final String S_DOM3_PROPERTIES_NS = "{"
+ DOMConstants.DOM3_REC_URL + "}";

public static final String S_XERCES_PROPERTIES_NS = "{"
+ DOMConstants.XERCES_URL + "}";

// The namespace used for the JDK-only parameters
public static final String S_JDK_PROPERTIES_NS = "{" + ORACLE_URL + "}";

// xmlns namespaces
private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";

Expand Down Expand Up @@ -113,6 +120,25 @@ final class DOMConstants {
// The xerces serializer specific 'omit-xml-declaration' property used in LSSerializer
public static final String S_XML_VERSION = "xml-version";

/**
* Indicates that the serializer should treat the output as a standalone document.
* The JDK specific standalone property controls whether a newline should be
* added after the XML header.
*
* @see similar property ORACLE_IS_STANDALONE in OutputPropertiesFactory.
*/
public static final String S_IS_STANDALONE = "isStandalone";

// Fully-qualified property name with the JDK Impl prefix
public static final String FQ_IS_STANDALONE =
Constants.ORACLE_JAXP_PROPERTY_PREFIX + S_IS_STANDALONE;

// The property with namespace as the internal DOMConfiguration format
public static final String NS_IS_STANDALONE = S_JDK_PROPERTIES_NS + S_IS_STANDALONE;

// Corresponding System property
public static final String SP_IS_STANDALONE = "jdk.xml.isStandalone";

//
public static final String S_XSL_VALUE_ENTITIES = "com/sun/org/apache/xml/internal/serializer/XMLEntities";

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -21,6 +21,7 @@

package com.sun.org.apache.xml.internal.serializer.dom3;

import com.sun.org.apache.xerces.internal.impl.Constants;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
Expand All @@ -32,7 +33,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

import jdk.xml.internal.SecuritySupport;
import com.sun.org.apache.xml.internal.serializer.DOM3Serializer;
import com.sun.org.apache.xml.internal.serializer.Encodings;
import com.sun.org.apache.xml.internal.serializer.Serializer;
Expand Down Expand Up @@ -67,7 +68,7 @@
* @version $Id:
*
* @xsl.usage internal
* @LastModified: Aug 2019
* @LastModified: Jan 2021
*/
final public class LSSerializerImpl implements DOMConfiguration, LSSerializer {

Expand Down Expand Up @@ -159,6 +160,10 @@ final public class LSSerializerImpl implements DOMConfiguration, LSSerializer {

// Parameter discard-default-content, true [required] (default)
private final static int XMLDECL = 0x1 << 18;

// Parameter is-standalone, jdk specific, not required
private final static int IS_STANDALONE = 0x1 << 19;

// ************************************************************************

// Recognized parameters for which atleast one value can be set
Expand All @@ -182,6 +187,7 @@ final public class LSSerializerImpl implements DOMConfiguration, LSSerializer {
DOMConstants.DOM_FORMAT_PRETTY_PRINT,
DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS,
DOMConstants.DOM_XMLDECL,
DOMConstants.FQ_IS_STANDALONE,
DOMConstants.DOM_ERROR_HANDLER
};

Expand Down Expand Up @@ -351,6 +357,20 @@ public void initializeSerializerProps () {
// xml-declaration
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, "no");

// JDK specific property isStandalone
String p = SecuritySupport.getSystemProperty(DOMConstants.SP_IS_STANDALONE);
if (p == null || p.isEmpty()) {
p = SecuritySupport.readJAXPProperty(DOMConstants.SP_IS_STANDALONE);
}
// the system property is true only if it is "true" and false otherwise
if (p != null && p.equals("true")) {
fFeatures |= IS_STANDALONE;
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE,
DOMConstants.DOM3_EXPLICIT_TRUE);
} else {
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE,
DOMConstants.DOM3_DEFAULT_FALSE);
}
}

// ************************************************************************
Expand Down Expand Up @@ -378,7 +398,8 @@ public boolean canSetParameter(String name, Object value) {
|| name.equalsIgnoreCase(DOMConstants.DOM_WELLFORMED)
|| name.equalsIgnoreCase(DOMConstants.DOM_DISCARD_DEFAULT_CONTENT)
|| name.equalsIgnoreCase(DOMConstants.DOM_FORMAT_PRETTY_PRINT)
|| name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL)){
|| name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL)
|| name.equalsIgnoreCase(DOMConstants.FQ_IS_STANDALONE)){
// both values supported
return true;
}
Expand All @@ -390,11 +411,11 @@ else if (name.equalsIgnoreCase(DOMConstants.DOM_CANONICAL_FORM)
// || name.equalsIgnoreCase(DOMConstants.DOM_NORMALIZE_CHARACTERS)
) {
// true is not supported
return !((Boolean)value).booleanValue();
return !((Boolean)value);
}
else if (name.equalsIgnoreCase(DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
// false is not supported
return ((Boolean)value).booleanValue();
return ((Boolean)value);
}
}
else if (name.equalsIgnoreCase(DOMConstants.DOM_ERROR_HANDLER) &&
Expand Down Expand Up @@ -436,6 +457,8 @@ public Object getParameter(String name) throws DOMException {
return ((fFeatures & PRETTY_PRINT) != 0) ? Boolean.TRUE : Boolean.FALSE;
} else if (name.equalsIgnoreCase(DOMConstants.DOM_XMLDECL)) {
return ((fFeatures & XMLDECL) != 0) ? Boolean.TRUE : Boolean.FALSE;
} else if (name.equalsIgnoreCase(DOMConstants.FQ_IS_STANDALONE)) {
return ((fFeatures & IS_STANDALONE) != 0) ? Boolean.TRUE : Boolean.FALSE;
} else if (name.equalsIgnoreCase(DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
return ((fFeatures & ELEM_CONTENT_WHITESPACE) != 0) ? Boolean.TRUE : Boolean.FALSE;
} else if (name.equalsIgnoreCase(DOMConstants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
Expand Down Expand Up @@ -606,6 +629,10 @@ public void setParameter(String name, Object value) throws DOMException {
} else {
fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, "yes");
}
} else if (name.equalsIgnoreCase(DOMConstants.FQ_IS_STANDALONE)) {
fFeatures = state ? fFeatures | IS_STANDALONE : fFeatures & ~IS_STANDALONE;
fDOMConfigProperties.setProperty(DOMConstants.NS_IS_STANDALONE, state ? "yes" : "no");

} else if (name.equalsIgnoreCase(DOMConstants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
fFeatures = state ? fFeatures | ELEM_CONTENT_WHITESPACE : fFeatures
& ~ELEM_CONTENT_WHITESPACE;
Expand Down

0 comments on commit 69ee314

Please sign in to comment.