8269938: Enhance XML processing passes redux

Reviewed-by: naoto, lancea, rhalade, ahgross
This commit is contained in:
Joe Wang 2021-07-27 19:14:05 +00:00 committed by Henry Jen
parent 60446746d4
commit c4cf4df4f3
7 changed files with 65 additions and 172 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -56,6 +56,7 @@ import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
import java.util.Locale;
import java.util.Stack;
import jdk.xml.internal.JdkXmlUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
@ -84,7 +85,7 @@ import org.xml.sax.SAXException;
* @author Andy Clark, IBM
* @author Elena Litani, IBM
*
* @LastModified: Jan 2019
* @LastModified: July 2021
*/
public class AbstractDOMParser extends AbstractXMLDocumentParser {
@ -2041,17 +2042,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
@ -2181,20 +2173,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");
}
@ -2261,19 +2241,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 org.xml.sax.SAXException;
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
import javax.xml.transform.ErrorListener;
import jdk.xml.internal.JdkXmlUtils;
/**
* This serializer takes a series of SAX or
@ -41,7 +42,7 @@ import javax.xml.transform.ErrorListener;
* because it is used from another package.
*
* @xsl.usage internal
* @LastModified: June 2021
* @LastModified: July 2021
*/
public final class ToHTMLStream extends ToStream
{
@ -679,28 +680,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

@ -896,16 +896,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
@ -1974,27 +1966,11 @@ abstract public class ToStream extends SerializerBase {
final 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(">");
@ -2002,17 +1978,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)
{
@ -3577,16 +3542,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
@ -3607,16 +3564,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

@ -63,7 +63,7 @@ import org.xml.sax.helpers.LocatorImpl;
* parameters and filters if any during serialization.
*
* @xsl.usage internal
* @LastModified: Apr 2021
* @LastModified: July 2021
*/
final class DOM3TreeWalker {
@ -506,23 +506,7 @@ final class DOM3TreeWalker {
dtd.append("<!DOCTYPE ");
dtd.append(docTypeName);
if (null != publicId) {
dtd.append(" PUBLIC \"");
dtd.append(publicId);
dtd.append('\"');
}
if (null != systemId) {
char quote = JdkXmlUtils.getQuoteChar(systemId);
if (null == publicId) {
dtd.append(" SYSTEM ").append(quote);
} else {
dtd.append(" ").append(quote);
}
dtd.append(systemId);
dtd.append(quote);
}
dtd.append(JdkXmlUtils.getDTDExternalDecl(publicId, systemId));
dtd.append(" [ ");
dtd.append(fNewLine);

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

@ -367,19 +367,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) {