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 * 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 com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
import java.util.Locale; import java.util.Locale;
import java.util.Stack; import java.util.Stack;
import jdk.xml.internal.JdkXmlUtils;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection; import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment; import org.w3c.dom.Comment;
@ -84,7 +85,7 @@ import org.xml.sax.SAXException;
* @author Andy Clark, IBM * @author Andy Clark, IBM
* @author Elena Litani, IBM * @author Elena Litani, IBM
* *
* @LastModified: Jan 2019 * @LastModified: July 2021
*/ */
public class AbstractDOMParser extends AbstractXMLDocumentParser { public class AbstractDOMParser extends AbstractXMLDocumentParser {
@ -2041,17 +2042,8 @@ public class AbstractDOMParser extends AbstractXMLDocumentParser {
else { else {
fInternalSubset.append (name); fInternalSubset.append (name);
} }
fInternalSubset.append (' '); fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
if (publicId != null) { fInternalSubset.append (">\n");
fInternalSubset.append ("PUBLIC '");
fInternalSubset.append (publicId);
fInternalSubset.append ("' '");
}
else {
fInternalSubset.append ("SYSTEM '");
}
fInternalSubset.append (literalSystemId);
fInternalSubset.append ("'>\n");
} }
// NOTE: We only know how to create these nodes for the Xerces // 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) { if (fInternalSubset != null && !fInDTDExternalSubset) {
fInternalSubset.append ("<!ENTITY "); fInternalSubset.append ("<!ENTITY ");
fInternalSubset.append (name); fInternalSubset.append (name);
fInternalSubset.append (' '); fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
if (publicId != null) { fInternalSubset.append (" NDATA ");
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 (notation); fInternalSubset.append (notation);
fInternalSubset.append (">\n"); fInternalSubset.append (">\n");
} }
@ -2261,19 +2241,8 @@ public class AbstractDOMParser extends AbstractXMLDocumentParser {
if (fInternalSubset != null && !fInDTDExternalSubset) { if (fInternalSubset != null && !fInDTDExternalSubset) {
fInternalSubset.append ("<!NOTATION "); fInternalSubset.append ("<!NOTATION ");
fInternalSubset.append (name); fInternalSubset.append (name);
if (publicId != null) { fInternalSubset.append (JdkXmlUtils.getDTDExternalDecl(publicId, literalSystemId));
fInternalSubset.append (" PUBLIC '"); fInternalSubset.append (">\n");
fInternalSubset.append (publicId);
if (literalSystemId != null) {
fInternalSubset.append ("' '");
fInternalSubset.append (literalSystemId);
}
}
else {
fInternalSubset.append (" SYSTEM '");
fInternalSubset.append (literalSystemId);
}
fInternalSubset.append ("'>\n");
} }
// NOTE: We only know how to create these nodes for the Xerces // 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.MsgKey;
import com.sun.org.apache.xml.internal.serializer.utils.Utils; import com.sun.org.apache.xml.internal.serializer.utils.Utils;
import javax.xml.transform.ErrorListener; import javax.xml.transform.ErrorListener;
import jdk.xml.internal.JdkXmlUtils;
/** /**
* This serializer takes a series of SAX or * This serializer takes a series of SAX or
@ -41,7 +42,7 @@ import javax.xml.transform.ErrorListener;
* because it is used from another package. * because it is used from another package.
* *
* @xsl.usage internal * @xsl.usage internal
* @LastModified: June 2021 * @LastModified: July 2021
*/ */
public final class ToHTMLStream extends ToStream public final class ToHTMLStream extends ToStream
{ {
@ -679,28 +680,10 @@ public final class ToHTMLStream extends ToStream
final java.io.Writer writer = m_writer; final java.io.Writer writer = m_writer;
try try
{ {
writer.write("<!DOCTYPE html"); writer.write("<!DOCTYPE html");
writer.write(JdkXmlUtils.getDTDExternalDecl(doctypePublic, doctypeSystem));
if (null != doctypePublic) writer.write('>');
{ outputLineSep();
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();
} }
catch(IOException e) catch(IOException e)
{ {

View File

@ -896,16 +896,8 @@ abstract public class ToStream extends SerializerBase {
m_writer.write("<!ENTITY "); m_writer.write("<!ENTITY ");
m_writer.write(name); m_writer.write(name);
if (publicId != null) { m_writer.write(JdkXmlUtils.getDTDExternalDecl(publicId, systemId));
m_writer.write(" PUBLIC \""); m_writer.write(">");
m_writer.write(publicId);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(systemId);
}
m_writer.write("\" >");
m_writer.write(m_lineSep, 0, m_lineSepLen); m_writer.write(m_lineSep, 0, m_lineSepLen);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -1974,27 +1966,11 @@ abstract public class ToStream extends SerializerBase {
final Writer writer = m_writer; final Writer writer = m_writer;
writer.write("<!DOCTYPE "); writer.write("<!DOCTYPE ");
writer.write(name); writer.write(name);
String systemId = getDoctypeSystem();
writer.write(JdkXmlUtils.getDTDExternalDecl(getDoctypePublic(), systemId));
String doctypePublic = getDoctypePublic(); if (null != systemId)
if (null != doctypePublic)
{ {
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) if (closeDecl)
{ {
writer.write(">"); writer.write(">");
@ -2002,17 +1978,6 @@ abstract public class ToStream extends SerializerBase {
closeDecl = false; // done closing 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) catch (IOException e)
{ {
@ -3577,16 +3542,8 @@ abstract public class ToStream extends SerializerBase {
m_writer.write("<!NOTATION "); m_writer.write("<!NOTATION ");
m_writer.write(name); m_writer.write(name);
if (pubID != null) { m_writer.write(JdkXmlUtils.getDTDExternalDecl(pubID, sysID));
m_writer.write(" PUBLIC \""); m_writer.write(">");
m_writer.write(pubID);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(sysID);
}
m_writer.write("\" >");
m_writer.write(m_lineSep, 0, m_lineSepLen); m_writer.write(m_lineSep, 0, m_lineSepLen);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -3607,16 +3564,8 @@ abstract public class ToStream extends SerializerBase {
m_writer.write("<!ENTITY "); m_writer.write("<!ENTITY ");
m_writer.write(name); m_writer.write(name);
if (pubID != null) { m_writer.write(JdkXmlUtils.getDTDExternalDecl(pubID, sysID));
m_writer.write(" PUBLIC \""); m_writer.write(" NDATA ");
m_writer.write(pubID);
}
else {
m_writer.write(" SYSTEM \"");
m_writer.write(sysID);
}
m_writer.write("\" NDATA ");
m_writer.write(notationName); m_writer.write(notationName);
m_writer.write(" >"); m_writer.write(" >");
m_writer.write(m_lineSep, 0, m_lineSepLen); 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. * parameters and filters if any during serialization.
* *
* @xsl.usage internal * @xsl.usage internal
* @LastModified: Apr 2021 * @LastModified: July 2021
*/ */
final class DOM3TreeWalker { final class DOM3TreeWalker {
@ -506,23 +506,7 @@ final class DOM3TreeWalker {
dtd.append("<!DOCTYPE "); dtd.append("<!DOCTYPE ");
dtd.append(docTypeName); dtd.append(docTypeName);
if (null != publicId) { dtd.append(JdkXmlUtils.getDTDExternalDecl(publicId, systemId));
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(" [ "); dtd.append(" [ ");
dtd.append(fNewLine); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.EntityDeclaration;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 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 //escape quotes, lt and amps
writer.write(" \""); writer.write(" \"");
charEncode(writer, fReplacementText); charEncode(writer, fReplacementText);
writer.write("\"");
} else { } else {
//external entity //external entity
String pubId = getPublicId(); writer.write(JdkXmlUtils.getDTDExternalDecl(getPublicId(), getSystemId()));
if (pubId != null) {
writer.write(" PUBLIC \"");
writer.write(pubId);
} else {
writer.write(" SYSTEM \"");
writer.write(getSystemId());
}
} }
writer.write("\"");
if (fNotationName != null) { if (fNotationName != null) {
writer.write(" NDATA "); writer.write(" NDATA ");
writer.write(fNotationName); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.NotationDeclaration;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl; import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl;
import jdk.xml.internal.JdkXmlUtils;
/** /**
* Implementation of NotationDeclaration event. * Implementation of NotationDeclaration event.
@ -88,16 +89,7 @@ public class NotationDeclarationImpl extends DummyEvent implements NotationDecla
{ {
writer.write("<!NOTATION "); writer.write("<!NOTATION ");
writer.write(getName()); writer.write(getName());
if (fPublicId != null) { writer.write(JdkXmlUtils.getDTDExternalDecl(fPublicId, fSystemId));
writer.write(" PUBLIC \"");
writer.write(fPublicId);
writer.write("\"");
} else if (fSystemId != null) {
writer.write(" SYSTEM");
writer.write(" \"");
writer.write(fSystemId);
writer.write("\"");
}
writer.write('>'); writer.write('>');
} }
} }

View File

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