Search in sources :

Example 1 with FormattedOutputStreamRecord

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);
}
Also used : JSONWithPadding(org.eclipse.persistence.oxm.JSONWithPadding) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.oxm.record.MarshalRecord) ValidatingMarshalRecord(org.eclipse.persistence.oxm.record.ValidatingMarshalRecord) FormattedOutputStreamRecord(org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord) JSONWriterRecord(org.eclipse.persistence.oxm.record.JSONWriterRecord) OutputStreamWriter(java.io.OutputStreamWriter) JSONFormattedWriterRecord(org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) MalformedURLException(java.net.MalformedURLException) OutputStreamRecord(org.eclipse.persistence.oxm.record.OutputStreamRecord) FormattedOutputStreamRecord(org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord)

Example 2 with FormattedOutputStreamRecord

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()));
}
Also used : FormattedOutputStreamRecord(org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord) CharacterEscapeHandler(org.eclipse.persistence.oxm.CharacterEscapeHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

FormattedOutputStreamRecord (org.eclipse.persistence.oxm.record.FormattedOutputStreamRecord)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 MalformedURLException (java.net.MalformedURLException)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 AbstractMarshalRecord (org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord)1 CharacterEscapeHandler (org.eclipse.persistence.oxm.CharacterEscapeHandler)1 JSONWithPadding (org.eclipse.persistence.oxm.JSONWithPadding)1 JSONFormattedWriterRecord (org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord)1 JSONWriterRecord (org.eclipse.persistence.oxm.record.JSONWriterRecord)1 MarshalRecord (org.eclipse.persistence.oxm.record.MarshalRecord)1 OutputStreamRecord (org.eclipse.persistence.oxm.record.OutputStreamRecord)1 ValidatingMarshalRecord (org.eclipse.persistence.oxm.record.ValidatingMarshalRecord)1