use of org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method marshal.
private void marshal(Object object, OutputStream outputStream, ABSTRACT_SESSION session, DESCRIPTOR xmlDescriptor) throws XMLMarshalException {
if ((object == null) || (outputStream == null)) {
throw XMLMarshalException.nullArgumentException();
}
boolean isXMLRoot = false;
String version = DEFAULT_XML_VERSION;
String encoding = getEncoding();
String callbackName = null;
if (object instanceof JSONWithPadding) {
callbackName = ((JSONWithPadding) object).getCallbackName();
object = ((JSONWithPadding) object).getObject();
if (object == null) {
throw XMLMarshalException.nullArgumentException();
}
}
if (object instanceof Root) {
isXMLRoot = true;
Root xroot = (Root) object;
encoding = xroot.getEncoding() != null ? xroot.getEncoding() : encoding;
}
if (!encoding.equals(Constants.DEFAULT_XML_ENCODING)) {
try {
OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding);
marshal(object, writer, session, xmlDescriptor);
writer.flush();
} catch (EclipseLinkException e) {
throw e;
} catch (Exception e) {
throw XMLMarshalException.marshalException(e);
}
return;
}
MarshalRecord marshalRecord;
if (isFormattedOutput()) {
if (isApplicationJSON()) {
marshalRecord = new JSONFormattedWriterRecord(outputStream, callbackName);
} else {
marshalRecord = new FormattedOutputStreamRecord();
((FormattedOutputStreamRecord) marshalRecord).setOutputStream(outputStream);
}
} else {
if (isApplicationJSON()) {
marshalRecord = new JSONWriterRecord(outputStream, callbackName);
} else {
marshalRecord = new OutputStreamRecord();
((OutputStreamRecord) marshalRecord).setOutputStream(outputStream);
}
}
marshalStreamOrWriter(object, marshalRecord, session, xmlDescriptor, isXMLRoot);
}
use of org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord in project eclipselink by eclipse-ee4j.
the class XMLMarshalTestCases method testMarshalWithCharacterEscapeHandlerToFormattedOutputStreamRecord.
/**
* 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);
assertEquals("<user-id>*user***</user-id><domain>*domain***</domain>", removeWhiteSpaceFromString(byteArrayOutputStream.toString()));
}
Aggregations