Search in sources :

Example 1 with WriterRecord

use of org.eclipse.persistence.oxm.record.WriterRecord in project eclipselink by eclipse-ee4j.

the class SDOXSDHelperDelegate method buildAppInfoMap.

/**
 * INTERNAL:
 */
@Override
public Map buildAppInfoMap(List appInfoElements) {
    HashMap appInfoMap = new HashMap();
    // Build AppInfo map
    if (appInfoElements != null) {
        for (int i = 0; i < appInfoElements.size(); i++) {
            Element nextElement = (Element) appInfoElements.get(i);
            if (nextElement.getNamespaceURI().equals(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) && nextElement.getLocalName().equals("appinfo")) {
                String key = nextElement.getAttribute(SDOConstants.APPINFO_SOURCE_ATTRIBUTE);
                String value = (String) appInfoMap.get(key);
                StringWriter sw = new StringWriter();
                WriterRecord wrec = new WriterRecord();
                wrec.setWriter(sw);
                wrec.node(nextElement, new NamespaceResolver());
                wrec.flush();
                appInfoMap.put(key, value == null ? sw.toString() : value + sw.toString());
            }
        }
    }
    return appInfoMap;
}
Also used : StringWriter(java.io.StringWriter) WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver)

Example 2 with WriterRecord

use of org.eclipse.persistence.oxm.record.WriterRecord in project eclipselink by eclipse-ee4j.

the class XMLMarshaller method marshal.

private void marshal(Object object, Writer writer, ABSTRACT_SESSION session, DESCRIPTOR xmlDescriptor) throws XMLMarshalException {
    if ((object == null) || (writer == 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;
        version = xroot.getXMLVersion() != null ? xroot.getXMLVersion() : version;
        encoding = xroot.getEncoding() != null ? xroot.getEncoding() : encoding;
    }
    MarshalRecord marshalRecord;
    writer = wrapWriter(writer);
    if (isFormattedOutput()) {
        if (isApplicationJSON()) {
            marshalRecord = new JSONFormattedWriterRecord(writer, callbackName);
        } else {
            marshalRecord = new FormattedWriterRecord();
            ((FormattedWriterRecord) marshalRecord).setWriter(writer);
        }
    } else {
        if (isApplicationJSON()) {
            marshalRecord = new JSONWriterRecord(writer, callbackName);
        } else {
            marshalRecord = new WriterRecord();
            ((WriterRecord) marshalRecord).setWriter(writer);
        }
    }
    marshalStreamOrWriter(object, marshalRecord, session, xmlDescriptor, isXMLRoot);
}
Also used : JSONWithPadding(org.eclipse.persistence.oxm.JSONWithPadding) JSONFormattedWriterRecord(org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord) WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) JSONWriterRecord(org.eclipse.persistence.oxm.record.JSONWriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.oxm.record.MarshalRecord) ValidatingMarshalRecord(org.eclipse.persistence.oxm.record.ValidatingMarshalRecord) JSONWriterRecord(org.eclipse.persistence.oxm.record.JSONWriterRecord) JSONFormattedWriterRecord(org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord) JSONFormattedWriterRecord(org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord)

Example 3 with WriterRecord

use of org.eclipse.persistence.oxm.record.WriterRecord in project eclipselink by eclipse-ee4j.

the class SDOXMLHelperDelegate method save.

private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller) throws IOException {
    if (xmlDocument == null) {
        throw new IllegalArgumentException(SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
    }
    // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML
    anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration());
    anXMLMarshaller.setEncoding(xmlDocument.getEncoding());
    anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation());
    anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation());
    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
        writerRecord = new FormattedWriterRecord();
    } else {
        writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(outputWriter);
    writerRecord.setMarshaller(anXMLMarshaller);
    SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
    listener.setMarshalledObject(xmlDocument.getRootObject());
    listener.setMarshalledObjectRootQName(new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
    listener.setRootMarshalRecord(writerRecord);
    try {
        anXMLMarshaller.marshal(xmlDocument, writerRecord);
        writerRecord.flush();
    } catch (XMLMarshalException xme) {
        if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
            if (aHelperContext != ((SDOType) xmlDocument.getRootObject().getType()).getHelperContext()) {
                throw SDOException.dataObjectNotFromHelperContext();
            }
        }
    } finally {
        listener.setMarshalledObject(null);
        listener.setMarshalledObjectRootQName(null);
        listener.setRootMarshalRecord(null);
    }
    outputWriter.flush();
}
Also used : WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) QName(javax.xml.namespace.QName) SDOType(org.eclipse.persistence.sdo.SDOType) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord)

Example 4 with WriterRecord

use of org.eclipse.persistence.oxm.record.WriterRecord in project eclipselink by eclipse-ee4j.

the class SDOXMLHelperDelegate method save.

/**
 * INTERNAL:
 * Saves the DataObject as an XML document with the specified root element.
 * Same as
 *   save(createDocument(dataObject, rootElementURI, rootElementName),
 *     writer, null);
 *
 * @param rootObject specifies DataObject to be saved
 * @param rootElementURI the Target Namespace URI of the root XML element
 * @param rootElementName the Name of the root XML element
 * @param writer specifies the Writer to write to.
 * @throws IllegalArgumentException if the dataObject tree
 *    is not closed or has no container.
 */
private void save(DataObject rootObject, String rootElementURI, String rootElementName, Writer writer, XMLMarshaller anXMLMarshaller) throws XMLMarshalException {
    SDOXMLDocument xmlDocument = (SDOXMLDocument) createDocument(rootObject, rootElementURI, rootElementName);
    // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML
    anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration());
    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
        writerRecord = new FormattedWriterRecord();
    } else {
        writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(writer);
    writerRecord.setMarshaller(anXMLMarshaller);
    SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
    listener.setMarshalledObject(rootObject);
    listener.setMarshalledObjectRootQName(new QName(rootElementURI, rootElementName));
    listener.setRootMarshalRecord(writerRecord);
    try {
        anXMLMarshaller.marshal(xmlDocument, writerRecord);
        writerRecord.flush();
    } catch (XMLMarshalException xme) {
        if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
            if (aHelperContext != ((SDOType) rootObject.getType()).getHelperContext()) {
                throw SDOException.dataObjectNotFromHelperContext();
            }
        }
    } finally {
        listener.setMarshalledObject(null);
        listener.setMarshalledObjectRootQName(null);
        listener.setRootMarshalRecord(null);
    }
    try {
        writer.flush();
    } catch (IOException ex) {
        throw XMLMarshalException.marshalException(ex);
    }
}
Also used : SDOXMLDocument(org.eclipse.persistence.sdo.SDOXMLDocument) WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) QName(javax.xml.namespace.QName) SDOType(org.eclipse.persistence.sdo.SDOType) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) IOException(java.io.IOException) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord)

Aggregations

FormattedWriterRecord (org.eclipse.persistence.oxm.record.FormattedWriterRecord)4 WriterRecord (org.eclipse.persistence.oxm.record.WriterRecord)4 QName (javax.xml.namespace.QName)2 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)2 SDOType (org.eclipse.persistence.sdo.SDOType)2 SDOMarshalListener (org.eclipse.persistence.sdo.helper.SDOMarshalListener)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 AbstractMarshalRecord (org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord)1 JSONWithPadding (org.eclipse.persistence.oxm.JSONWithPadding)1 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)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 ValidatingMarshalRecord (org.eclipse.persistence.oxm.record.ValidatingMarshalRecord)1 SDOXMLDocument (org.eclipse.persistence.sdo.SDOXMLDocument)1 Element (org.w3c.dom.Element)1