8269938: Enhance XML processing passes redux

Reviewed-by: bae
Backport-of: ff4e11b1a83d6910e39f1d46bea154f0876f39da
This commit is contained in:
Ekaterina Vergizova 2022-01-31 10:54:14 +00:00 committed by Andrew Brygin
parent 3e94375f1f
commit eed8ca4ee9
6 changed files with 61 additions and 152 deletions

View File

@ -57,6 +57,7 @@ import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
import com.sun.org.apache.xerces.internal.utils.ObjectFactory;
import jdk.xml.internal.JdkXmlUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
@ -2027,17 +2028,8 @@ public class AbstractDOMParser extends AbstractXMLDocumentParser {
else {
fInternalSubset.append (name);
}
fInternalSubset.append (' ');
if (publicId != null) {
fInternalSubset.append ("PUBLIC '");
fInternalSubset.append (publicId);
fInternalSubset.append ("' '");
}
else {
fInternalSubset.append ("SYSTEM '");
}
fInternalSubset.append (literalSystemId);
fInternalSubset.append ("'>\n");
fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
fInternalSubset.append (">\n");
}
// NOTE: We only know how to create these nodes for the Xerces
@ -2167,20 +2159,8 @@ public class AbstractDOMParser extends AbstractXMLDocumentParser {
if (fInternalSubset != null && !fInDTDExternalSubset) {
fInternalSubset.append ("<!ENTITY ");
fInternalSubset.append (name);
fInternalSubset.append (' ');
if (publicId != null) {
fInternalSubset.append ("PUBLIC '");
fInternalSubset.append (publicId);
if (literalSystemId != null) {
fInternalSubset.append ("' '");
fInternalSubset.append (literalSystemId);
}
}
else {
fInternalSubset.append ("SYSTEM '");
fInternalSubset.append (literalSystemId);
}
fInternalSubset.append ("' NDATA ");
fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
fInternalSubset.append (" NDATA ");
fInternalSubset.append (notation);
fInternalSubset.append (">\n");
}
@ -2247,19 +2227,8 @@ public class AbstractDOMParser extends AbstractXMLDocumentParser {
if (fInternalSubset != null && !fInDTDExternalSubset) {
fInternalSubset.append ("<!NOTATION ");
fInternalSubset.append (name);
if (publicId != null) {
fInternalSubset.append (" PUBLIC '");
fInternalSubset.append (publicId);
if (literalSystemId != null) {
fInternalSubset.append ("' '");
fInternalSubset.append (literalSystemId);
}
}
else {
fInternalSubset.append (" SYSTEM '");
fInternalSubset.append (literalSystemId);
}
fInternalSubset.append ("'>\n");
fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
fInternalSubset.append (">\n");
}
// NOTE: We only know how to create these nodes for the Xerces

View File

@ -31,6 +31,7 @@ import javax.xml.transform.Result;
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
import jdk.xml.internal.JdkXmlUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@ -678,28 +679,10 @@ public final class ToHTMLStream extends ToStream
final java.io.Writer writer = m_writer;
try
{
writer.write("<!DOCTYPE html");
if (null != doctypePublic)
{
writer.write(" PUBLIC \"");
writer.write(doctypePublic);
writer.write('"');
}
if (null != doctypeSystem)
{
if (null == doctypePublic)
writer.write(" SYSTEM \"");
else
writer.write(" \"");
writer.write(doctypeSystem);
writer.write('"');
}
writer.write('>');
outputLineSep();
writer.write("<!DOCTYPE html");
writer.write(JdkXmlUtils.getDTDExternalDecl(doctypePublic, doctypeSystem));
writer.write('>');
outputLineSep();
}
catch(IOException e)
{

View File

@ -52,7 +52,7 @@ import org.xml.sax.SAXException;
* serializers (xml, html, text ...) that write output to a stream.
*
* @xsl.usage internal
* @LastModified: Apr 2021
* @LastModified: July 2021
*/
abstract public class ToStream extends SerializerBase
{
@ -894,16 +894,8 @@ abstract public class ToStream extends SerializerBase
m_writer.write("<!ENTITY ");
m_writer.write(name);
if (publicId != null) {
m_writer.write(" PUBLIC \"");
m_writer.write(publicId);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(systemId);
}
m_writer.write("\" >");
m_writer.write(JdkXmlUtils.getDTDExternalDecl(publicId, systemId));
m_writer.write(">");
m_writer.write(m_lineSep, 0, m_lineSepLen);
} catch (IOException e) {
// TODO Auto-generated catch block
@ -1872,27 +1864,11 @@ abstract public class ToStream extends SerializerBase
final java.io.Writer writer = m_writer;
writer.write("<!DOCTYPE ");
writer.write(name);
String systemId = getDoctypeSystem();
writer.write(JdkXmlUtils.getDTDExternalDecl(getDoctypePublic(), systemId));
String doctypePublic = getDoctypePublic();
if (null != doctypePublic)
if (null != systemId)
{
writer.write(" PUBLIC \"");
writer.write(doctypePublic);
writer.write('\"');
}
String doctypeSystem = getDoctypeSystem();
if (null != doctypeSystem)
{
char quote = JdkXmlUtils.getQuoteChar(doctypeSystem);
if (null == doctypePublic) {
writer.write(" SYSTEM");
}
writer.write(" ");
writer.write(quote);
writer.write(doctypeSystem);
writer.write(quote);
if (closeDecl)
{
writer.write(">");
@ -1900,17 +1876,6 @@ abstract public class ToStream extends SerializerBase
closeDecl = false; // done closing
}
}
boolean dothis = false;
if (dothis)
{
// at one point this code seemed right,
// but not anymore - Brian M.
if (closeDecl)
{
writer.write('>');
writer.write(m_lineSep, 0, m_lineSepLen);
}
}
}
catch (IOException e)
{
@ -3312,16 +3277,8 @@ abstract public class ToStream extends SerializerBase
m_writer.write("<!NOTATION ");
m_writer.write(name);
if (pubID != null) {
m_writer.write(" PUBLIC \"");
m_writer.write(pubID);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(sysID);
}
m_writer.write("\" >");
m_writer.write(JdkXmlUtils.getDTDExternalDecl(pubID, sysID));
m_writer.write(">");
m_writer.write(m_lineSep, 0, m_lineSepLen);
} catch (IOException e) {
// TODO Auto-generated catch block
@ -3342,16 +3299,8 @@ abstract public class ToStream extends SerializerBase
m_writer.write("<!ENTITY ");
m_writer.write(name);
if (pubID != null) {
m_writer.write(" PUBLIC \"");
m_writer.write(pubID);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(sysID);
}
m_writer.write("\" NDATA ");
m_writer.write(JdkXmlUtils.getDTDExternalDecl(pubID, sysID));
m_writer.write(" NDATA ");
m_writer.write(notationName);
m_writer.write(" >");
m_writer.write(m_lineSep, 0, m_lineSepLen);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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
@ -28,6 +28,7 @@ package com.sun.xml.internal.stream.events;
import javax.xml.stream.events.EntityDeclaration;
import javax.xml.stream.events.XMLEvent;
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
import jdk.xml.internal.JdkXmlUtils;
/**
*
@ -129,18 +130,12 @@ public class EntityDeclarationImpl extends DummyEvent implements EntityDeclarati
//escape quotes, lt and amps
writer.write(" \"");
charEncode(writer, fReplacementText);
writer.write("\"");
} else {
//external entity
String pubId = getPublicId();
if (pubId != null) {
writer.write(" PUBLIC \"");
writer.write(pubId);
} else {
writer.write(" SYSTEM \"");
writer.write(getSystemId());
}
writer.write(JdkXmlUtils.getDTDExternalDecl(getPublicId(), getSystemId()));
}
writer.write("\"");
if (fNotationName != null) {
writer.write(" NDATA ");
writer.write(fNotationName);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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
@ -28,6 +28,7 @@ package com.sun.xml.internal.stream.events;
import javax.xml.stream.events.NotationDeclaration;
import javax.xml.stream.events.XMLEvent;
import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl;
import jdk.xml.internal.JdkXmlUtils;
/**
* Implementation of NotationDeclaration event.
@ -88,16 +89,7 @@ public class NotationDeclarationImpl extends DummyEvent implements NotationDecla
{
writer.write("<!NOTATION ");
writer.write(getName());
if (fPublicId != null) {
writer.write(" PUBLIC \"");
writer.write(fPublicId);
writer.write("\"");
} else if (fSystemId != null) {
writer.write(" SYSTEM");
writer.write(" \"");
writer.write(fSystemId);
writer.write("\"");
}
writer.write(JdkXmlUtils.getDTDExternalDecl(fPublicId, fSystemId));
writer.write('>');
}
}

View File

@ -249,19 +249,40 @@ public class JdkXmlUtils {
}
/**
* Returns the character to be used to quote the input content. Between
* single and double quotes, this method returns the one that is not found
* in the input. Returns double quote by default.
* Returns the external declaration for a DTD construct.
*
* @param s the input string
* @return returns the quote not found in the input
* @param publicId the public identifier
* @param systemId the system identifier
* @return a DTD external declaration
*/
public static char getQuoteChar(String s) {
if (s != null && s.indexOf('"') > -1) {
return '\'';
} else {
return '"';
public static String getDTDExternalDecl(String publicId, String systemId) {
StringBuilder sb = new StringBuilder();
if (null != publicId) {
sb.append(" PUBLIC ");
sb.append(quoteString(publicId));
}
if (null != systemId) {
if (null == publicId) {
sb.append(" SYSTEM ");
} else {
sb.append(" ");
}
sb.append(quoteString(systemId));
}
return sb.toString();
}
/**
* Returns the input string quoted with double quotes or single ones if
* there is a double quote in the string.
* @param s the input string, can not be null
* @return the quoted string
*/
private static String quoteString(String s) {
char c = (s.indexOf('"') > -1) ? '\'' : '"';
return c + s + c;
}
private static XMLReader getXMLReaderWSAXFactory(boolean overrideDefaultParser) {