8238183: SAX2StAXStreamWriter cannot deal with comments prior to the root element
Reviewed-by: naoto, lancea
This commit is contained in:
parent
ff34c4cdf8
commit
acafdb3f60
@ -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.Locator;
|
||||
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<String> namespaces;
|
||||
|
||||
protected List<String> 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 void setXMLReporter(XMLReporter reporter) {
|
||||
this.reporter = reporter;
|
||||
}
|
||||
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
this.docLocator = locator;
|
||||
}
|
||||
|
||||
private void updateVersionAndEncoding() {
|
||||
if (docLocator instanceof Locator2) {
|
||||
Locator2 l2 = (Locator2) docLocator;
|
||||
xmlVersion = l2.getXMLVersion();
|
||||
encoding = l2.getEncoding();
|
||||
}
|
||||
}
|
||||
|
||||
public void setXmlVersion(String version) {
|
||||
this.xmlVersion = version;
|
||||
}
|
||||
|
||||
public void setEncoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
void writeStartDocument() throws SAXException {
|
||||
updateVersionAndEncoding();
|
||||
}
|
||||
|
||||
public Location getCurrentLocation() {
|
||||
if (docLocator != null) {
|
||||
return new SAXLocation(docLocator);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void error(SAXParseException e) throws SAXException {
|
||||
reportException("ERROR", e);
|
||||
}
|
||||
|
||||
public void fatalError(SAXParseException e) throws SAXException {
|
||||
reportException("FATAL", e);
|
||||
}
|
||||
|
||||
public void warning(SAXParseException e) throws SAXException {
|
||||
reportException("WARNING", e);
|
||||
}
|
||||
|
||||
public void startDocument() throws SAXException {
|
||||
namespaces = new ArrayList<>(2);
|
||||
}
|
||||
|
||||
public void endDocument() throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName)
|
||||
throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void startPrefixMapping(String prefix, String uri)
|
||||
throws SAXException {
|
||||
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
} else if (prefix.equals("xml")) {
|
||||
return;
|
||||
}
|
||||
|
||||
public SAX2StAXBaseWriter(XMLReporter reporter) {
|
||||
this.reporter = reporter;
|
||||
if (namespaces == null) {
|
||||
namespaces = new ArrayList<>(2);
|
||||
}
|
||||
namespaces.add(prefix);
|
||||
namespaces.add(uri);
|
||||
}
|
||||
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
}
|
||||
|
||||
public void startCDATA() throws SAXException {
|
||||
isCDATA = true;
|
||||
if (CDATABuffer == null) {
|
||||
CDATABuffer = new StringBuffer();
|
||||
} else {
|
||||
CDATABuffer.setLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void characters(char[] ch, int start, int length)
|
||||
throws SAXException {
|
||||
if (isCDATA) {
|
||||
CDATABuffer.append(ch, start, length);
|
||||
}
|
||||
}
|
||||
|
||||
public void endCDATA() throws SAXException {
|
||||
isCDATA = false;
|
||||
CDATABuffer.setLength(0);
|
||||
}
|
||||
|
||||
public void comment(char[] ch, int start, int length) throws SAXException {
|
||||
}
|
||||
|
||||
public void endDTD() throws SAXException {
|
||||
}
|
||||
|
||||
public void endEntity(String name) throws SAXException {
|
||||
}
|
||||
|
||||
public void startDTD(String name, String publicId, String systemId)
|
||||
throws SAXException {
|
||||
}
|
||||
|
||||
public void startEntity(String name) 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 {
|
||||
|
||||
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 <code>results[0]</code>, and the local
|
||||
* part at <code>results[1]</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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();
|
||||
}
|
||||
|
||||
public void setXMLReporter(XMLReporter reporter) {
|
||||
this.reporter = reporter;
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
this.docLocator = locator;
|
||||
public int getColumnNumber() {
|
||||
return columnNumber;
|
||||
}
|
||||
|
||||
|
||||
public Location getCurrentLocation() {
|
||||
if (docLocator != null) {
|
||||
return new SAXLocation(docLocator);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getCharacterOffset() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void error(SAXParseException e) throws SAXException {
|
||||
reportException("ERROR", e);
|
||||
public String getPublicId() {
|
||||
return publicId;
|
||||
}
|
||||
|
||||
public void fatalError(SAXParseException e) throws SAXException {
|
||||
reportException("FATAL", e);
|
||||
}
|
||||
|
||||
public void warning(SAXParseException e) throws SAXException {
|
||||
reportException("WARNING", e);
|
||||
}
|
||||
|
||||
public void startDocument() throws SAXException {
|
||||
namespaces = new ArrayList<>(2);
|
||||
}
|
||||
|
||||
public void endDocument() throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName)
|
||||
throws SAXException {
|
||||
namespaces = null;
|
||||
}
|
||||
|
||||
public void startPrefixMapping(String prefix, String uri)
|
||||
throws SAXException {
|
||||
|
||||
if (prefix == null) {
|
||||
prefix = "";
|
||||
} else if (prefix.equals("xml")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (namespaces == null) {
|
||||
namespaces = new ArrayList<>(2);
|
||||
}
|
||||
namespaces.add(prefix);
|
||||
namespaces.add(uri);
|
||||
}
|
||||
|
||||
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
}
|
||||
|
||||
public void startCDATA() throws SAXException {
|
||||
isCDATA = true;
|
||||
if (CDATABuffer == null) {
|
||||
CDATABuffer = new StringBuffer();
|
||||
} else {
|
||||
CDATABuffer.setLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void characters(char[] ch, int start, int length)
|
||||
throws SAXException {
|
||||
if (isCDATA) {
|
||||
CDATABuffer.append(ch, start, length);
|
||||
}
|
||||
}
|
||||
|
||||
public void endCDATA() throws SAXException {
|
||||
isCDATA = false;
|
||||
CDATABuffer.setLength(0);
|
||||
}
|
||||
|
||||
public void comment(char[] ch, int start, int length) throws SAXException {
|
||||
}
|
||||
|
||||
public void endDTD() throws SAXException {
|
||||
}
|
||||
|
||||
public void endEntity(String name) throws SAXException {
|
||||
}
|
||||
|
||||
public void startDTD(String name, String publicId, String systemId)
|
||||
throws SAXException {
|
||||
}
|
||||
|
||||
public void startEntity(String name) 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 {
|
||||
|
||||
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 <code>results[0]</code>, and the local
|
||||
* part at <code>results[1]</code>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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();
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return lineNumber;
|
||||
}
|
||||
|
||||
public int getColumnNumber() {
|
||||
return columnNumber;
|
||||
}
|
||||
|
||||
public int getCharacterOffset() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String getPublicId() {
|
||||
return publicId;
|
||||
}
|
||||
|
||||
public String getSystemId() {
|
||||
return systemId;
|
||||
}
|
||||
public String getSystemId() {
|
||||
return systemId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.XMLStreamException;
|
||||
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<Collection<Namespace>> 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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
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 class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
}
|
||||
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||
|
||||
if (needToCallStartDocument) {
|
||||
writeStartDocument();
|
||||
}
|
||||
@ -184,21 +141,15 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
Iterator<Namespace> 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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
|
||||
super.ignorableWhitespace(ch, start, length);
|
||||
characters(ch, start, length);
|
||||
|
||||
}
|
||||
|
||||
public void processingInstruction(String target, String data)
|
||||
@ -298,32 +232,22 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
|
||||
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 @@ public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
|
||||
}
|
||||
|
||||
} 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 @@ import org.xml.sax.ext.Locator2;
|
||||
/**
|
||||
* @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 XMLStreamWriter getStreamWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public void setStreamWriter(XMLStreamWriter writer) {
|
||||
this.writer = 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);
|
||||
}
|
||||
|
||||
public SAX2StAXStreamWriter(XMLStreamWriter writer) {
|
||||
super.endDocument();
|
||||
}
|
||||
|
||||
this.writer = writer;
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
|
||||
if (needToCallStartDocument) {
|
||||
writeStartDocument();
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
public XMLStreamWriter getStreamWriter() {
|
||||
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);
|
||||
|
||||
return writer;
|
||||
// write attributes and namespaces as attributes
|
||||
for (int i = 0, s = attributes.getLength(); i < s; i++) {
|
||||
|
||||
}
|
||||
parseQName(attributes.getQName(i), qname);
|
||||
|
||||
String attrPrefix = qname[0];
|
||||
String attrLocal = qname[1];
|
||||
|
||||
public void setStreamWriter(XMLStreamWriter writer) {
|
||||
|
||||
this.writer = 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 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;
|
||||
|
||||
writer.writeNamespace(attrLocal, attrValue);
|
||||
|
||||
} else if (attrPrefix.length() > 0) {
|
||||
writer.writeAttribute(attrPrefix, attrURI, attrLocal,
|
||||
attrValue);
|
||||
} else {
|
||||
writer.writeAttribute(attrQName, attrValue);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
} catch (XMLStreamException e) {
|
||||
throw new SAXException(e);
|
||||
} finally {
|
||||
super.startElement(uri, localName, qName, attributes);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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);
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName)
|
||||
throws SAXException {
|
||||
super.endCDATA();
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
writer.writeEndElement();
|
||||
|
||||
} catch (XMLStreamException e) {
|
||||
|
||||
throw new SAXException(e);
|
||||
|
||||
} finally {
|
||||
|
||||
super.endElement(uri, localName, qName);
|
||||
|
||||
}
|
||||
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 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 processingInstruction(String target, String data)
|
||||
throws SAXException {
|
||||
|
||||
super.processingInstruction(target, data);
|
||||
try {
|
||||
writer.writeProcessingInstruction(target, data);
|
||||
} 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);
|
||||
|
||||
}
|
||||
|
||||
void writeStartDocument() throws SAXException {
|
||||
super.writeStartDocument();
|
||||
try {
|
||||
writer.writeStartDocument(xmlVersion);
|
||||
} 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
needToCallStartDocument = false;
|
||||
}
|
||||
}
|
||||
|
109
test/jaxp/javax/xml/jaxp/unittest/transform/ResultTest.java
Normal file
109
test/jaxp/javax/xml/jaxp/unittest/transform/ResultTest.java
Normal file
@ -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 =
|
||||
"<?xml version=\"1.0\" ?>\n"
|
||||
+ "<!--comment-->\n"
|
||||
+ "<root />\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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user