--- old/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXBaseWriter.java 2020-03-30 21:44:23.180845980 +0000 +++ new/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXBaseWriter.java 2020-03-30 21:44:22.593831374 +0000 @@ -1,6 +1,5 @@ /* - * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. - * @LastModified: Oct 2017 + * Copyright (c) 2005, 2020, 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 @@ -36,213 +35,231 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.ext.Locator2; import org.xml.sax.helpers.DefaultHandler; - public abstract class SAX2StAXBaseWriter extends DefaultHandler - implements - LexicalHandler { + implements + LexicalHandler { + protected boolean isCDATA; - protected boolean isCDATA; + protected StringBuffer CDATABuffer; - protected StringBuffer CDATABuffer; + protected List namespaces; - protected List namespaces; + protected Locator docLocator; - protected Locator docLocator; + protected XMLReporter reporter; - protected XMLReporter reporter; + String xmlVersion = null, encoding = null; - public SAX2StAXBaseWriter() { - } + public SAX2StAXBaseWriter() { + } - public SAX2StAXBaseWriter(XMLReporter reporter) { - this.reporter = reporter; - } + public SAX2StAXBaseWriter(XMLReporter reporter) { + this.reporter = reporter; + } - public void setXMLReporter(XMLReporter reporter) { - this.reporter = reporter; - } + public void setXMLReporter(XMLReporter reporter) { + this.reporter = reporter; + } - public void setDocumentLocator(Locator locator) { - this.docLocator = locator; + public void setDocumentLocator(Locator locator) { + this.docLocator = locator; + } + + private void updateVersionAndEncoding() { + if (docLocator != null && docLocator instanceof Locator2) { + Locator2 l2 = (Locator2) docLocator; + xmlVersion = l2.getXMLVersion(); + encoding = l2.getEncoding(); } + } + public void setXmlVersion(String version) { + this.xmlVersion = version; + } - public Location getCurrentLocation() { - if (docLocator != null) { - return new SAXLocation(docLocator); - } else { - return null; - } + public void setEncoding(String encoding) { + this.encoding = encoding; + } - } + void writeStartDocument() throws SAXException { + updateVersionAndEncoding(); + } - public void error(SAXParseException e) throws SAXException { - reportException("ERROR", e); + public Location getCurrentLocation() { + if (docLocator != null) { + return new SAXLocation(docLocator); + } else { + return null; } + } - public void fatalError(SAXParseException e) throws SAXException { - reportException("FATAL", e); - } + public void error(SAXParseException e) throws SAXException { + reportException("ERROR", e); + } - public void warning(SAXParseException e) throws SAXException { - reportException("WARNING", e); - } + public void fatalError(SAXParseException e) throws SAXException { + reportException("FATAL", e); + } - public void startDocument() throws SAXException { - namespaces = new ArrayList<>(2); - } + public void warning(SAXParseException e) throws SAXException { + reportException("WARNING", e); + } - public void endDocument() throws SAXException { - namespaces = null; - } + public void startDocument() throws SAXException { + namespaces = new ArrayList<>(2); + } - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - namespaces = null; - } + public void endDocument() throws SAXException { + namespaces = null; + } - public void endElement(String uri, String localName, String qName) - throws SAXException { - namespaces = null; - } + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + namespaces = null; + } - public void startPrefixMapping(String prefix, String uri) - throws SAXException { + public void endElement(String uri, String localName, String qName) + throws SAXException { + namespaces = null; + } - if (prefix == null) { - prefix = ""; - } else if (prefix.equals("xml")) { - return; - } + public void startPrefixMapping(String prefix, String uri) + throws SAXException { - if (namespaces == null) { - namespaces = new ArrayList<>(2); - } - namespaces.add(prefix); - namespaces.add(uri); + if (prefix == null) { + prefix = ""; + } else if (prefix.equals("xml")) { + return; } - - public void endPrefixMapping(String prefix) throws SAXException { + if (namespaces == null) { + namespaces = new ArrayList<>(2); } + namespaces.add(prefix); + namespaces.add(uri); + } - public void startCDATA() throws SAXException { - isCDATA = true; - if (CDATABuffer == null) { - CDATABuffer = new StringBuffer(); - } else { - CDATABuffer.setLength(0); - } - } + public void endPrefixMapping(String prefix) throws SAXException { + } - public void characters(char[] ch, int start, int length) - throws SAXException { - if (isCDATA) { - CDATABuffer.append(ch, start, length); - } + public void startCDATA() throws SAXException { + isCDATA = true; + if (CDATABuffer == null) { + CDATABuffer = new StringBuffer(); + } else { + CDATABuffer.setLength(0); } + } - public void endCDATA() throws SAXException { - isCDATA = false; - CDATABuffer.setLength(0); + public void characters(char[] ch, int start, int length) + throws SAXException { + if (isCDATA) { + CDATABuffer.append(ch, start, length); } + } - public void comment(char[] ch, int start, int length) throws SAXException { - } + public void endCDATA() throws SAXException { + isCDATA = false; + CDATABuffer.setLength(0); + } - public void endDTD() throws SAXException { - } + public void comment(char[] ch, int start, int length) throws SAXException { + } - public void endEntity(String name) throws SAXException { - } + public void endDTD() throws SAXException { + } - public void startDTD(String name, String publicId, String systemId) - throws SAXException { - } + public void endEntity(String name) throws SAXException { + } - public void startEntity(String name) throws SAXException { - } + public void startDTD(String name, String publicId, String systemId) + throws SAXException { + } - /** - * Used to report a {@link SAXException}to the {@link XMLReporter} - * registered with this handler. - */ - protected void reportException(String type, SAXException e) - throws SAXException { + public void startEntity(String name) throws SAXException { + } - if (reporter != null) { - try { - reporter.report(e.getMessage(), type, e, getCurrentLocation()); - } catch (XMLStreamException e1) { - throw new SAXException(e1); - } - } + /** + * Used to report a {@link SAXException}to the {@link XMLReporter} + * registered with this handler. + */ + protected void reportException(String type, SAXException e) + throws SAXException { + + if (reporter != null) { + try { + reporter.report(e.getMessage(), type, e, getCurrentLocation()); + } catch (XMLStreamException e1) { + throw new SAXException(e1); + } } + } - /** - * Parses an XML qualified name, and places the resulting prefix and local - * name in the provided String array. - * - * @param qName The qualified name to parse. - * @param results An array where parse results will be placed. The prefix - * will be placed at results[0], and the local - * part at results[1] - */ - public static final void parseQName(String qName, String[] results) { + /** + * Parses an XML qualified name, and places the resulting prefix and local + * name in the provided String array. + * + * @param qName The qualified name to parse. + * @param results An array where parse results will be placed. The prefix + * will be placed at results[0], and the local + * part at results[1] + */ + public static final void parseQName(String qName, String[] results) { - String prefix, local; - int idx = qName.indexOf(':'); - if (idx >= 0) { - prefix = qName.substring(0, idx); - local = qName.substring(idx + 1); - } else { - prefix = ""; - local = qName; - } - results[0] = prefix; - results[1] = local; + String prefix, local; + int idx = qName.indexOf(':'); + if (idx >= 0) { + prefix = qName.substring(0, idx); + local = qName.substring(idx + 1); + } else { + prefix = ""; + local = qName; } + results[0] = prefix; + results[1] = local; + } - /** - * {@Link Location}implementation used to expose details from a SAX - * {@link Locator}. - * - * @author christian - */ - private static final class SAXLocation implements Location { + /** + * {@Link Location}implementation used to expose details from a SAX + * {@link Locator}. + * + * @author christian + */ + private static final class SAXLocation implements Location { - private int lineNumber; - private int columnNumber; - private String publicId; - private String systemId; - private SAXLocation(Locator locator) { - lineNumber = locator.getLineNumber(); - columnNumber = locator.getColumnNumber(); - publicId = locator.getPublicId(); - systemId = locator.getSystemId(); - } + private int lineNumber; + private int columnNumber; + private String publicId; + private String systemId; + private SAXLocation(Locator locator) { + lineNumber = locator.getLineNumber(); + columnNumber = locator.getColumnNumber(); + publicId = locator.getPublicId(); + systemId = locator.getSystemId(); + } - public int getLineNumber() { - return lineNumber; - } + public int getLineNumber() { + return lineNumber; + } - public int getColumnNumber() { - return columnNumber; - } + public int getColumnNumber() { + return columnNumber; + } - public int getCharacterOffset() { - return -1; - } + public int getCharacterOffset() { + return -1; + } - public String getPublicId() { - return publicId; - } + public String getPublicId() { + return publicId; + } - public String getSystemId() { - return systemId; - } + public String getSystemId() { + return systemId; } + } } --- old/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXEventWriter.java 2020-03-30 21:44:24.233872182 +0000 +++ new/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXEventWriter.java 2020-03-30 21:44:23.647857601 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2020, 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 @@ -38,38 +38,27 @@ import javax.xml.stream.events.*; import org.xml.sax.Attributes; import org.xml.sax.SAXException; -import org.xml.sax.ext.Locator2; /** * @author Sunitha Reddy */ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter { - private XMLEventWriter writer; - private XMLEventFactory eventFactory; - private List> namespaceStack = new ArrayList<>(); - private boolean needToCallStartDocument = false; - public SAX2StAXEventWriter() { - eventFactory = XMLEventFactory.newInstance(); - } - public SAX2StAXEventWriter(XMLEventWriter writer) { - this.writer = writer; eventFactory = XMLEventFactory.newInstance(); - } public SAX2StAXEventWriter(XMLEventWriter writer, @@ -77,50 +66,31 @@ this.writer = writer; if (factory != null) { - this.eventFactory = factory; - } else { - eventFactory = XMLEventFactory.newInstance(); - } - } public XMLEventWriter getEventWriter() { - return writer; - } - public void setEventWriter(XMLEventWriter writer) { - this.writer = writer; - } - public XMLEventFactory getEventFactory() { - return eventFactory; - } - public void setEventFactory(XMLEventFactory factory) { - this.eventFactory = factory; - } public void startDocument() throws SAXException { - super.startDocument(); - namespaceStack.clear(); - eventFactory.setLocation(getCurrentLocation()); // Encoding and version info will be available only after startElement @@ -129,17 +99,10 @@ needToCallStartDocument = true; } - private void writeStartDocument() throws SAXException { + void writeStartDocument() throws SAXException { + super.writeStartDocument(); try { - if (docLocator == null) - writer.add(eventFactory.createStartDocument()); - else { - try{ - writer.add(eventFactory.createStartDocument(((Locator2)docLocator).getEncoding(),((Locator2)docLocator).getXMLVersion())); - } catch(ClassCastException e){ - writer.add(eventFactory.createStartDocument()); - } - } + writer.add(eventFactory.createStartDocument(encoding, xmlVersion)); } catch (XMLStreamException e) { throw new SAXException(e); } @@ -147,17 +110,12 @@ } public void endDocument() throws SAXException { - eventFactory.setLocation(getCurrentLocation()); try { - writer.add(eventFactory.createEndDocument()); - } catch (XMLStreamException e) { - throw new SAXException(e); - } super.endDocument(); @@ -169,7 +127,6 @@ @SuppressWarnings({"rawtypes", "unchecked"}) public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - if (needToCallStartDocument) { writeStartDocument(); } @@ -184,21 +141,15 @@ namespaceStack.add(events[0]); try { - String[] qname = {null, null}; parseQName(qName, qname); writer.add(eventFactory.createStartElement(qname[0], uri, qname[1], events[1].iterator(), events[0].iterator())); - } catch (XMLStreamException e) { - throw new SAXException(e); - } finally { - super.startElement(uri, localName, qName, attributes); - } } @@ -219,16 +170,11 @@ Iterator nsIter = nsList.iterator(); try { - writer.add(eventFactory.createEndElement(qname[0], uri, qname[1], nsIter)); - } catch (XMLStreamException e) { - throw new SAXException(e); - } - } public void comment(char[] ch, int start, int length) throws SAXException { @@ -243,16 +189,11 @@ eventFactory.setLocation(getCurrentLocation()); try { - writer.add(eventFactory.createComment(new String(ch, start, length))); - } catch (XMLStreamException e) { - throw new SAXException(e); - } - } public void characters(char[] ch, int start, int length) @@ -261,21 +202,15 @@ super.characters(ch, start, length); try { - if (!isCDATA) { - eventFactory.setLocation(getCurrentLocation()); writer.add(eventFactory.createCharacters(new String(ch, start, length))); - } } catch (XMLStreamException e) { - throw new SAXException(e); - } - } public void ignorableWhitespace(char[] ch, int start, int length) @@ -283,7 +218,6 @@ super.ignorableWhitespace(ch, start, length); characters(ch, start, length); - } public void processingInstruction(String target, String data) @@ -298,32 +232,22 @@ super.processingInstruction(target, data); try { - writer.add(eventFactory.createProcessingInstruction(target, data)); - } catch (XMLStreamException e) { - throw new SAXException(e); - } - } public void endCDATA() throws SAXException { eventFactory.setLocation(getCurrentLocation()); try { - writer.add(eventFactory.createCData(CDATABuffer.toString())); - } catch (XMLStreamException e) { - throw new SAXException(e); - } super.endCDATA(); - } @SuppressWarnings({"rawtypes", "unchecked"}) @@ -373,45 +297,31 @@ } } else { - Attribute attribute; if (attrPrefix.length() > 0) { - attribute = eventFactory.createAttribute(attrPrefix, attrURI, attrLocal, attrValue); - } else { - attribute = eventFactory.createAttribute(attrLocal, attrValue); - } if (attrs == null) { attrs = new ArrayList<>(); } attrs.add(attribute); - } } events[0] = (nsMap == null ? Collections.EMPTY_LIST : nsMap.values()); events[1] = (attrs == null ? Collections.EMPTY_LIST : attrs); - } protected Namespace createNamespace(String prefix, String uri) { - if (prefix == null || prefix.length() == 0) { - return eventFactory.createNamespace(uri); - } else { - return eventFactory.createNamespace(prefix, uri); - } - } - } --- old/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXStreamWriter.java 2020-03-30 21:44:25.289898459 +0000 +++ new/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/SAX2StAXStreamWriter.java 2020-03-30 21:44:24.705883927 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2020, 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 @@ -25,12 +25,9 @@ package com.sun.org.apache.xalan.internal.xsltc.trax; -import java.util.Iterator; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; -import javax.xml.stream.XMLEventWriter; - import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.ext.Locator2; @@ -38,265 +35,174 @@ /** * @author Sunitha Reddy */ - public class SAX2StAXStreamWriter extends SAX2StAXBaseWriter { + private XMLStreamWriter writer; - private XMLStreamWriter writer; + private boolean needToCallStartDocument = false; - private boolean needToCallStartDocument = false; + public SAX2StAXStreamWriter() { - public SAX2StAXStreamWriter() { - - } + } - public SAX2StAXStreamWriter(XMLStreamWriter writer) { - - this.writer = writer; - - } + public SAX2StAXStreamWriter(XMLStreamWriter writer) { + this.writer = writer; + } + public XMLStreamWriter getStreamWriter() { + return writer; + } - public XMLStreamWriter getStreamWriter() { + public void setStreamWriter(XMLStreamWriter writer) { + this.writer = writer; + } - return writer; + public void startDocument() throws SAXException { + super.startDocument(); + // Encoding and version info will be available only after startElement + // is called for first time. So, defer START_DOCUMENT event of StAX till + // that point of time. + needToCallStartDocument = true; + } + public void endDocument() throws SAXException { + try { + writer.writeEndDocument(); + } catch (XMLStreamException e) { + throw new SAXException(e); } + super.endDocument(); + } - public void setStreamWriter(XMLStreamWriter writer) { - - this.writer = writer; + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + if (needToCallStartDocument) { + writeStartDocument(); } - public void startDocument() throws SAXException { - - super.startDocument(); - // Encoding and version info will be available only after startElement - // is called for first time. So, defer START_DOCUMENT event of StAX till - // that point of time. - needToCallStartDocument = true; - } + try { - public void endDocument() throws SAXException { + String[] qname = {null, null}; + parseQName(qName, qname); + //Do not call writeStartElement with prefix and namespaceURI, as it writes out + //namespace declaration. + //writer.writeStartElement(qname[0], qname[1], uri); + writer.writeStartElement(qName); - try { + // write attributes and namespaces as attributes + for (int i = 0, s = attributes.getLength(); i < s; i++) { - writer.writeEndDocument(); + parseQName(attributes.getQName(i), qname); - } catch (XMLStreamException e) { + String attrPrefix = qname[0]; + String attrLocal = qname[1]; - throw new SAXException(e); - - } - - super.endDocument(); - - } - - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - - if (needToCallStartDocument) { - try { - if (docLocator == null) - writer.writeStartDocument(); - else { - try{ - writer.writeStartDocument(((Locator2)docLocator).getXMLVersion()); - }catch(ClassCastException e){ - writer.writeStartDocument(); - } - } - - } catch (XMLStreamException e) { - - throw new SAXException(e); + String attrQName = attributes.getQName(i); + String attrValue = attributes.getValue(i); + String attrURI = attributes.getURI(i); + if ("xmlns".equals(attrPrefix) || "xmlns".equals(attrQName)) { + // namespace declaration disguised as an attribute. + // write it as an namespace + if (attrLocal.length() == 0) { + writer.setDefaultNamespace(attrValue); + } else { + writer.setPrefix(attrLocal, attrValue); } - needToCallStartDocument = false; - } - - try { - - String[] qname = {null, null}; - parseQName(qName, qname); - //Do not call writeStartElement with prefix and namespaceURI, as it writes out - //namespace declaration. - //writer.writeStartElement(qname[0], qname[1], uri); - writer.writeStartElement(qName); - - - // No need to write namespaces, as they are written as part of attributes. - /*if (namespaces != null) { - - final int nDecls = namespaces.size(); - for (int i = 0; i < nDecls; i++) { - final String prefix = (String) namespaces.elementAt(i); - if (prefix.length() == 0) { - writer.setDefaultNamespace((String)namespaces.elementAt(++i)); - } else { - writer.setPrefix(prefix, (String) namespaces.elementAt(++i)); - } - - writer.writeNamespace(prefix, (String)namespaces.elementAt(i)); - } - - - }*/ - - // write attributes - for (int i = 0, s = attributes.getLength(); i < s; i++) { - - parseQName(attributes.getQName(i), qname); - - String attrPrefix = qname[0]; - String attrLocal = qname[1]; - - String attrQName = attributes.getQName(i); - String attrValue = attributes.getValue(i); - String attrURI = attributes.getURI(i); - - if ("xmlns".equals(attrPrefix) || "xmlns".equals(attrQName)) { - - // namespace declaration disguised as an attribute. - // write it as an namespace - - if (attrLocal.length() == 0) { - - writer.setDefaultNamespace(attrValue); - - } else { - - writer.setPrefix(attrLocal, attrValue); - - } - - writer.writeNamespace(attrLocal, attrValue); - - } else if (attrPrefix.length() > 0) { - - writer.writeAttribute(attrPrefix, attrURI, attrLocal, - attrValue); - - } else { - writer.writeAttribute(attrQName, attrValue); - } - - } - - } catch (XMLStreamException e) { - throw new SAXException(e); - - } finally { - - super.startElement(uri, localName, qName, attributes); - - } - - } - - public void endElement(String uri, String localName, String qName) - throws SAXException { - - try { - - writer.writeEndElement(); - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } finally { - - super.endElement(uri, localName, qName); - - } - - } - - public void comment(char[] ch, int start, int length) throws SAXException { - - super.comment(ch, start, length); - try { - - writer.writeComment(new String(ch, start, length)); - - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } - - } - - public void characters(char[] ch, int start, int length) - throws SAXException { - - super.characters(ch, start, length); - try { - - if (!isCDATA) { - - writer.writeCharacters(ch, start, length); - - } - - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } - - } - - public void endCDATA() throws SAXException { - - try { - - writer.writeCData(CDATABuffer.toString()); - - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } - - super.endCDATA(); + writer.writeNamespace(attrLocal, attrValue); + } else if (attrPrefix.length() > 0) { + writer.writeAttribute(attrPrefix, attrURI, attrLocal, + attrValue); + } else { + writer.writeAttribute(attrQName, attrValue); + } + } + + } catch (XMLStreamException e) { + throw new SAXException(e); + } finally { + super.startElement(uri, localName, qName, attributes); + } + } + + public void endElement(String uri, String localName, String qName) + throws SAXException { + try { + writer.writeEndElement(); + } catch (XMLStreamException e) { + throw new SAXException(e); + } finally { + super.endElement(uri, localName, qName); + } + } + + public void comment(char[] ch, int start, int length) throws SAXException { + if (needToCallStartDocument) { + writeStartDocument(); + } + super.comment(ch, start, length); + try { + writer.writeComment(new String(ch, start, length)); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + public void characters(char[] ch, int start, int length) + throws SAXException { + super.characters(ch, start, length); + try { + if (!isCDATA) { + writer.writeCharacters(ch, start, length); + } + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + public void endCDATA() throws SAXException { + try { + writer.writeCData(CDATABuffer.toString()); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + + super.endCDATA(); + } + + public void ignorableWhitespace(char[] ch, int start, int length) + throws SAXException { + + super.ignorableWhitespace(ch, start, length); + try { + writer.writeCharacters(ch, start, length); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + public void processingInstruction(String target, String data) + throws SAXException { + + super.processingInstruction(target, data); + try { + writer.writeProcessingInstruction(target, data); + } catch (XMLStreamException e) { + throw new SAXException(e); + } + } + + void writeStartDocument() throws SAXException { + super.writeStartDocument(); + try { + writer.writeStartDocument(xmlVersion); + } catch (XMLStreamException e) { + throw new SAXException(e); } - - public void ignorableWhitespace(char[] ch, int start, int length) - throws SAXException { - - super.ignorableWhitespace(ch, start, length); - try { - - writer.writeCharacters(ch, start, length); - - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } - - } - - public void processingInstruction(String target, String data) - throws SAXException { - - super.processingInstruction(target, data); - try { - - writer.writeProcessingInstruction(target, data); - - } catch (XMLStreamException e) { - - throw new SAXException(e); - - } - - } - + needToCallStartDocument = false; + } } --- /dev/null 2020-03-03 22:06:13.039758260 +0000 +++ new/test/jaxp/javax/xml/jaxp/unittest/transform/ResultTest.java 2020-03-30 21:44:25.760910178 +0000 @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2020, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package transform; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringReader; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stax.StAXResult; +import javax.xml.transform.stream.StreamSource; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.xml.sax.InputSource; +import org.xml.sax.helpers.DefaultHandler; + +/* + * @test + * @bug 8238183 + * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest + * @run testng/othervm transform.ResultTest + * @summary Verifies that the output of a transformation is well-formed when + * StAXResult is used. + */ +public class ResultTest { + // The XML contains a comment before the root element + final static String XML = + "\n" + + "\n" + + "\n"; + static Transformer T; + + @BeforeClass + public void setup() { + try { + T = TransformerFactory.newInstance().newTransformer(); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + + /** + * Transforms the XML using a StAXResult with a StreamWriter. + * @throws Exception if the process fails + */ + @Test + public void testStreamWriter() throws Exception { + try (Reader reader = new StringReader(XML); + OutputStream out = new ByteArrayOutputStream()) { + XMLOutputFactory factory = XMLOutputFactory.newFactory(); + XMLStreamWriter writer = factory.createXMLStreamWriter(out); + StreamSource source = new StreamSource(reader); + StAXResult result = new StAXResult(writer); + T.transform(source, result); + // verify the output is well-formed + parseXML(out.toString()); + } + } + + /** + * Transforms the XML using a StAXResult with a EventWriter. + * @throws Exception if the process fails + */ + @Test + public void testEventWriter() throws Exception { + try (Reader reader = new StringReader(XML); + OutputStream out = new ByteArrayOutputStream()) { + XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); + XMLEventWriter writer = ofactory.createXMLEventWriter(out); + StAXResult result = new StAXResult(writer); + StreamSource source = new StreamSource(reader); + T.transform(source, result); + parseXML(out.toString()); + } + } + + // parses the xml to verify that it's well-formed + private void parseXML(String xml) throws Exception { + SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); + parser.parse(new InputSource(new StringReader(xml)), new DefaultHandler()); + } +}