Skip to content

Commit

Permalink
Bugfix for - Indention not working if custom CharacterEscapeHandler i…
Browse files Browse the repository at this point in the history
…s set and encoding is set to 'UTF-8 (eclipse-ee4j#1146)


Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
  • Loading branch information
rfelcman committed May 23, 2021
1 parent 310080a commit 7f68c9e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -821,6 +821,7 @@ protected void marshal(Object object, MarshalRecord marshalRecord, ABSTRACT_SESS
}else{
marshalRecord.afterContainmentMarshal(null, object);
}
marshalRecord.flush();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "";
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -437,6 +439,24 @@ public void testMarshalNonRootObjectToWriter() {
this.assertEquals("<user-id>user</user-id><domain>domain</domain>", 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-id>*user***</user-id><domain>*domain***</domain>", removeWhiteSpaceFromString(byteArrayOutputStream.toString()));
}

//Null Test Cases=========================================================================================
public void testMarshalObjectToNullContentHandler() {
SAXDocumentBuilder output = null;
Expand Down

0 comments on commit 7f68c9e

Please sign in to comment.