From 7f68c9ec5a6f822c496cc68b0a05e3b5ecb52e8e Mon Sep 17 00:00:00 2001 From: Radek Felcman Date: Sun, 23 May 2021 21:11:48 +0200 Subject: [PATCH] Bugfix for - Indention not working if custom CharacterEscapeHandler is set and encoding is set to 'UTF-8 (#1146) Signed-off-by: Radek Felcman --- .../internal/oxm/XMLMarshaller.java | 3 +- .../CustomCharacterEscapeHandler.java | 36 +++++++++++++++++++ .../xmlmarshaller/XMLMarshalTestCases.java | 22 +++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/CustomCharacterEscapeHandler.java diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/oxm/XMLMarshaller.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/oxm/XMLMarshaller.java index 7d71de6ecf1..6775b328e83 100644 --- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/oxm/XMLMarshaller.java +++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/oxm/XMLMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -821,6 +821,7 @@ protected void marshal(Object object, MarshalRecord marshalRecord, ABSTRACT_SESS }else{ marshalRecord.afterContainmentMarshal(null, object); } + marshalRecord.flush(); } /** diff --git a/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/CustomCharacterEscapeHandler.java b/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/CustomCharacterEscapeHandler.java new file mode 100644 index 00000000000..72f6f67a582 --- /dev/null +++ b/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/CustomCharacterEscapeHandler.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, + * or the Eclipse Distribution License v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +// Contributors: +// Oracle - initial API and implementation +package org.eclipse.persistence.testing.oxm.xmlmarshaller; + +import org.eclipse.persistence.oxm.CharacterEscapeHandler; + +import java.io.IOException; +import java.io.Writer; + +public class CustomCharacterEscapeHandler implements CharacterEscapeHandler { + + @Override + public void escape(char[] buf, int start, int len, boolean isAttValue, Writer out) throws IOException { + for (int i = start; i < start + len; i++) { + char ch = buf[i]; + if (ch == '%') { + out.write("*"); + continue; + } + out.write(ch); + } + String end = ""; + } +} \ No newline at end of file diff --git a/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/XMLMarshalTestCases.java b/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/XMLMarshalTestCases.java index 2dfc9641440..ae40f5c5365 100644 --- a/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/XMLMarshalTestCases.java +++ b/moxy/org.eclipse.persistence.moxy/src/test/java/org/eclipse/persistence/testing/oxm/xmlmarshaller/XMLMarshalTestCases.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at @@ -30,9 +30,11 @@ import javax.xml.transform.stream.StreamResult; import org.eclipse.persistence.exceptions.XMLMarshalException; +import org.eclipse.persistence.oxm.CharacterEscapeHandler; import org.eclipse.persistence.oxm.XMLContext; import org.eclipse.persistence.oxm.XMLDescriptor; import org.eclipse.persistence.oxm.XMLMarshaller; +import org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord; import org.eclipse.persistence.platform.xml.SAXDocumentBuilder; import org.eclipse.persistence.testing.oxm.OXTestCase; import org.w3c.dom.Attr; @@ -437,6 +439,24 @@ public void testMarshalNonRootObjectToWriter() { this.assertEquals("userdomain", writer.toString()); } + /** + * Test for custom CharacterEscapeHandler and FormattedOutputStreamRecord with custom ByteArrayOutputStream output. + */ + public void testMarshalWithCharacterEscapeHandlerToFormattedOutputStreamRecord() { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + FormattedOutputStreamRecord record = new FormattedOutputStreamRecord(); + record.setOutputStream(byteArrayOutputStream); + EmailAddress emailAddress = new EmailAddress(); + emailAddress.setDomain("%domain%%%"); + emailAddress.setUserID("%user%%%"); + marshaller.setFormattedOutput(false); + marshaller.setFragment(true); + CharacterEscapeHandler characterEscapeHandler = new CustomCharacterEscapeHandler(); + marshaller.setCharacterEscapeHandler(characterEscapeHandler); + marshaller.marshal(emailAddress, record); + this.assertEquals("*user****domain***", removeWhiteSpaceFromString(byteArrayOutputStream.toString())); + } + //Null Test Cases========================================================================================= public void testMarshalObjectToNullContentHandler() { SAXDocumentBuilder output = null;