8139584: XMLStreamWriterImpl does not write 'standalone' property
Reviewed-by: lancea
This commit is contained in:
parent
c3c1310573
commit
9598a4300c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2016, 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,6 +25,8 @@
|
||||
|
||||
package com.sun.xml.internal.stream.writers;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl;
|
||||
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import javax.xml.XMLConstants;
|
||||
@ -57,7 +59,7 @@ import org.xml.sax.helpers.NamespaceSupport;
|
||||
* Change StringBuffer to StringBuilder, when JDK 1.5 will be minimum requirement for SJSXP.
|
||||
*/
|
||||
|
||||
public class XMLDOMWriterImpl implements XMLStreamWriter {
|
||||
public class XMLDOMWriterImpl implements XMLStreamWriterBase {
|
||||
|
||||
|
||||
private Document ownerDoc = null;
|
||||
@ -552,7 +554,7 @@ public class XMLDOMWriterImpl implements XMLStreamWriter {
|
||||
* @throws javax.xml.stream.XMLStreamException {@inheritDoc}
|
||||
*/
|
||||
public void writeStartDocument(String version) throws XMLStreamException {
|
||||
ownerDoc.setXmlVersion(version);
|
||||
writeStartDocument(null, version, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -563,8 +565,20 @@ public class XMLDOMWriterImpl implements XMLStreamWriter {
|
||||
* @throws javax.xml.stream.XMLStreamException {@inheritDoc}
|
||||
*/
|
||||
public void writeStartDocument(String encoding, String version) throws XMLStreamException {
|
||||
writeStartDocument(encoding, version, false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeStartDocument(String encoding, String version, boolean standalone, boolean standaloneSet) throws XMLStreamException {
|
||||
if (encoding != null && ownerDoc.getClass().isAssignableFrom(DocumentImpl.class)) {
|
||||
((DocumentImpl)ownerDoc).setXmlEncoding(encoding);
|
||||
}
|
||||
|
||||
ownerDoc.setXmlVersion(version);
|
||||
//TODO: What to do with encoding.-Venu
|
||||
|
||||
if (standaloneSet) {
|
||||
ownerDoc.setXmlStandalone(standalone);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2005, 2016, 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
|
||||
@ -49,14 +49,14 @@ import javax.xml.stream.XMLStreamWriter;
|
||||
public class XMLEventWriterImpl implements XMLEventWriter{
|
||||
|
||||
//delegate everything to XMLStreamWriter..
|
||||
private XMLStreamWriter fStreamWriter ;
|
||||
private final XMLStreamWriterBase fStreamWriter ;
|
||||
private static final boolean DEBUG = false;
|
||||
/**
|
||||
*
|
||||
* @param streamWriter
|
||||
*/
|
||||
public XMLEventWriterImpl(XMLStreamWriter streamWriter){
|
||||
fStreamWriter = streamWriter;
|
||||
fStreamWriter = (XMLStreamWriterBase)streamWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,7 +89,8 @@ public class XMLEventWriterImpl implements XMLEventWriter{
|
||||
StartDocument startDocument = (StartDocument)xMLEvent ;
|
||||
if (DEBUG)System.out.println("Adding StartDocument = " + startDocument.toString());
|
||||
try {
|
||||
fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
|
||||
fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion(),
|
||||
startDocument.isStandalone(), startDocument.standaloneSet());
|
||||
}catch(XMLStreamException e) {
|
||||
fStreamWriter.writeStartDocument(startDocument.getVersion());
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* 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 com.sun.xml.internal.stream.writers;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
|
||||
/**
|
||||
* Base class for shared methods among XMLStreamWriter implementations.
|
||||
*/
|
||||
public interface XMLStreamWriterBase extends XMLStreamWriter {
|
||||
/**
|
||||
* Writes the XML declaration.
|
||||
*
|
||||
* @param encoding the specified encoding
|
||||
* @param version the specified version
|
||||
* @param standalone the flag indicating whether it is standalone
|
||||
* @param standaloneSet the flag indicating whether the standalone attribute is set
|
||||
* @throws XMLStreamException in case of an IOException
|
||||
*/
|
||||
public void writeStartDocument(String encoding, String version,
|
||||
boolean standalone, boolean standaloneSet)
|
||||
throws XMLStreamException;
|
||||
}
|
@ -71,7 +71,7 @@ import com.sun.xml.internal.stream.util.ReadOnlyIterator;
|
||||
* @author Santiago.Pericas-Geertsen@sun.com
|
||||
* @author Sunitha.Reddy@sun.com
|
||||
*/
|
||||
public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriter {
|
||||
public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriterBase {
|
||||
|
||||
public static final String START_COMMENT = "<!--";
|
||||
public static final String END_COMMENT = "-->";
|
||||
@ -1112,59 +1112,91 @@ public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamW
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws XMLStreamException
|
||||
* Writes the XML declaration.
|
||||
*
|
||||
* @throws XMLStreamException in case of an IOException
|
||||
*/
|
||||
public void writeStartDocument() throws XMLStreamException {
|
||||
try {
|
||||
fWriter.write(DEFAULT_XMLDECL);
|
||||
} catch (IOException ex) {
|
||||
throw new XMLStreamException(ex);
|
||||
}
|
||||
writeStartDocument(null, null, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param version
|
||||
* @throws XMLStreamException
|
||||
* Writes the XML declaration.
|
||||
*
|
||||
* @param version the specified version
|
||||
* @throws XMLStreamException in case of an IOException
|
||||
*/
|
||||
public void writeStartDocument(String version) throws XMLStreamException {
|
||||
try {
|
||||
if ((version == null) || version.equals("")) {
|
||||
writeStartDocument();
|
||||
writeStartDocument(null, version, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the XML declaration.
|
||||
*
|
||||
* @param encoding the specified encoding
|
||||
* @param version the specified version
|
||||
* @throws XMLStreamException in case of an IOException
|
||||
*/
|
||||
@Override
|
||||
public void writeStartDocument(String encoding, String version)
|
||||
throws XMLStreamException {
|
||||
writeStartDocument(encoding, version, false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeStartDocument(String encoding, String version,
|
||||
boolean standalone, boolean standaloneSet)
|
||||
throws XMLStreamException {
|
||||
|
||||
try {
|
||||
if ((encoding == null || encoding.length() == 0)
|
||||
&& (version == null || version.length() == 0)
|
||||
&& (!standaloneSet)) {
|
||||
fWriter.write(DEFAULT_XMLDECL);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify the encoding before writing anything
|
||||
if (encoding != null && !encoding.equals("")) {
|
||||
verifyEncoding(encoding);
|
||||
}
|
||||
|
||||
fWriter.write("<?xml version=\"");
|
||||
fWriter.write(version);
|
||||
fWriter.write("\"");
|
||||
|
||||
//fWriter.write(DEFAULT_ENCODING);
|
||||
fWriter.write("?>");
|
||||
if ((version == null) || version.equals("")) {
|
||||
fWriter.write(DEFAULT_XML_VERSION);
|
||||
} else {
|
||||
fWriter.write(version);
|
||||
}
|
||||
|
||||
if (encoding != null && !encoding.equals("")) {
|
||||
fWriter.write("\" encoding=\"");
|
||||
fWriter.write(encoding);
|
||||
}
|
||||
|
||||
if (standaloneSet) {
|
||||
fWriter.write("\" standalone=\"");
|
||||
if (standalone) {
|
||||
fWriter.write("yes");
|
||||
} else {
|
||||
fWriter.write("no");
|
||||
}
|
||||
}
|
||||
|
||||
fWriter.write("\"?>");
|
||||
} catch (IOException ex) {
|
||||
throw new XMLStreamException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param encoding
|
||||
* @param version
|
||||
* @throws XMLStreamException
|
||||
* Verifies that the encoding is consistent between the underlying encoding
|
||||
* and that specified.
|
||||
*
|
||||
* @param encoding the specified encoding
|
||||
* @throws XMLStreamException if they do not match
|
||||
*/
|
||||
public void writeStartDocument(String encoding, String version)
|
||||
throws XMLStreamException {
|
||||
//Revisit : What about standalone ?
|
||||
try {
|
||||
if ((encoding == null) && (version == null)) {
|
||||
writeStartDocument();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (encoding == null) {
|
||||
writeStartDocument(version);
|
||||
|
||||
return;
|
||||
}
|
||||
private void verifyEncoding(String encoding) throws XMLStreamException {
|
||||
|
||||
String streamEncoding = null;
|
||||
if (fWriter instanceof OutputStreamWriter) {
|
||||
@ -1194,25 +1226,6 @@ public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamW
|
||||
+ encoding + "' do not match.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fWriter.write("<?xml version=\"");
|
||||
|
||||
if ((version == null) || version.equals("")) {
|
||||
fWriter.write(DEFAULT_XML_VERSION);
|
||||
} else {
|
||||
fWriter.write(version);
|
||||
}
|
||||
|
||||
if (!encoding.equals("")) {
|
||||
fWriter.write("\" encoding=\"");
|
||||
fWriter.write(encoding);
|
||||
}
|
||||
|
||||
fWriter.write("\"?>");
|
||||
} catch (IOException ex) {
|
||||
throw new XMLStreamException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2014, 2016, 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
|
||||
@ -20,21 +20,29 @@
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package stream.XMLStreamWriterTest;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.stream.XMLEventFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.stream.events.XMLEvent;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Listeners;
|
||||
import org.testng.annotations.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6347190
|
||||
* @bug 6347190 8139584
|
||||
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
|
||||
* @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.XMLStreamWriterTest
|
||||
* @run testng/othervm stream.XMLStreamWriterTest.XMLStreamWriterTest
|
||||
@ -42,6 +50,48 @@ import org.testng.annotations.Test;
|
||||
*/
|
||||
@Listeners({jaxp.library.BasePolicy.class})
|
||||
public class XMLStreamWriterTest {
|
||||
/**
|
||||
* @bug 8139584
|
||||
* Verifies that the resulting XML contains the standalone setting.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateStartDocument() throws XMLStreamException {
|
||||
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
XMLOutputFactory out = XMLOutputFactory.newInstance();
|
||||
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
|
||||
|
||||
XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter);
|
||||
|
||||
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
|
||||
eventWriter.add(event);
|
||||
eventWriter.flush();
|
||||
Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\""));
|
||||
Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\""));
|
||||
Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\""));
|
||||
}
|
||||
|
||||
/**
|
||||
* @bug 8139584
|
||||
* Verifies that the resulting XML contains the standalone setting.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateStartDocument_DOMWriter()
|
||||
throws ParserConfigurationException, XMLStreamException {
|
||||
|
||||
XMLOutputFactory xof = XMLOutputFactory.newInstance();
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
Document doc = db.newDocument();
|
||||
XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc));
|
||||
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
|
||||
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
|
||||
eventWriter.add(event);
|
||||
eventWriter.flush();
|
||||
Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15");
|
||||
Assert.assertEquals(doc.getXmlVersion(), "1.0");
|
||||
Assert.assertTrue(doc.getXmlStandalone());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of main method, of class TestXMLStreamWriter.
|
||||
|
Loading…
x
Reference in New Issue
Block a user