Search in sources :

Example 1 with NodeRecord

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

the class SDOXMLHelperDelegate method save.

@Override
public void save(XMLDocument xmlDocument, Result result, Object options) throws IOException {
    if (xmlDocument == null) {
        throw new IllegalArgumentException(SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
    }
    if (result instanceof StreamResult) {
        StreamResult streamResult = (StreamResult) result;
        Writer writer = streamResult.getWriter();
        if (null == writer) {
            save(xmlDocument, streamResult.getOutputStream(), options);
        } else {
            save(xmlDocument, writer, options);
        }
    } else {
        // get XMLMarshaller once - as we may create a new instance if this helper isDirty=true
        XMLMarshaller anXMLMarshaller = getXmlMarshaller(options);
        // 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());
        SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
        listener.setMarshalledObject(xmlDocument.getRootObject());
        listener.setMarshalledObjectRootQName(new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
        if (result instanceof SAXResult) {
            ContentHandlerRecord marshalRecord = new ContentHandlerRecord();
            marshalRecord.setContentHandler(((SAXResult) result).getHandler());
            marshalRecord.setMarshaller(anXMLMarshaller);
            listener.setRootMarshalRecord(marshalRecord);
            anXMLMarshaller.marshal(xmlDocument, marshalRecord);
            marshalRecord.flush();
        } else if (result instanceof DOMResult) {
            NodeRecord marshalRecord = new NodeRecord();
            marshalRecord.setDOM(((DOMResult) result).getNode());
            marshalRecord.setMarshaller(anXMLMarshaller);
            listener.setRootMarshalRecord(marshalRecord);
            anXMLMarshaller.marshal(xmlDocument, marshalRecord);
            marshalRecord.flush();
        } else {
            StringWriter writer = new StringWriter();
            this.save(xmlDocument, writer, options);
            String xml = writer.toString();
            StreamSource source = new StreamSource(new java.io.StringReader(xml));
            anXMLMarshaller.getTransformer().transform(source, result);
        }
        listener.setMarshalledObject(null);
        listener.setMarshalledObjectRootQName(null);
        listener.setRootMarshalRecord(null);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord) SAXResult(javax.xml.transform.sax.SAXResult) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) ContentHandlerRecord(org.eclipse.persistence.oxm.record.ContentHandlerRecord)

Example 2 with NodeRecord

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

the class XMLMarshaller method objectToXML.

/**
 * INTERNAL:
 * Convert the given object to an XML Document
 * @param object the object to marshal
 * @return the document which the specified object has been marshalled to
 * @param descriptor the XMLDescriptor for the object being marshalled
 * @throws XMLMarshalException if an error occurred during marshalling
 */
protected Document objectToXML(Object object, DESCRIPTOR descriptor, boolean isXMLRoot) throws XMLMarshalException {
    ABSTRACT_SESSION session = context.getSession(descriptor);
    MarshalRecord marshalRecord = new NodeRecord();
    marshalRecord.setMarshaller(this);
    marshal(object, marshalRecord, session, descriptor, isXMLRoot);
    return marshalRecord.getDocument();
}
Also used : NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.oxm.record.MarshalRecord) ValidatingMarshalRecord(org.eclipse.persistence.oxm.record.ValidatingMarshalRecord)

Example 3 with NodeRecord

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

the class XMLMarshaller method marshal.

/**
 * PUBLIC:
 * @param object the object to marshal
 * @param node the node which the specified object should be marshalled to
 * @throws XMLMarshalException if an error occurred during marshalling
 */
public void marshal(Object object, Node node) throws XMLMarshalException {
    if (object instanceof JSONWithPadding && !isApplicationJSON()) {
        object = ((JSONWithPadding) object).getObject();
    }
    if ((object == null) || (node == null)) {
        throw XMLMarshalException.nullArgumentException();
    }
    ABSTRACT_SESSION session = null;
    DESCRIPTOR xmlDescriptor = null;
    boolean isXMLRoot = (object instanceof Root);
    if (isXMLRoot) {
        try {
            session = context.getSession(((Root) object).getObject());
            if (session != null) {
                xmlDescriptor = getDescriptor(((Root) object).getObject(), session);
            }
        } catch (XMLMarshalException marshalException) {
            if (!isSimpleXMLRoot((Root) object)) {
                throw marshalException;
            }
        }
    } else {
        Class<?> objectClass = object.getClass();
        session = context.getSession(objectClass);
        xmlDescriptor = getDescriptor(objectClass, session);
    }
    NodeRecord contentHandlerRecord = new NodeRecord(node);
    contentHandlerRecord.setMarshaller(this);
    if (!isXMLRoot) {
        if ((null == xmlDescriptor.getDefaultRootElement()) && (node.getNodeType() == Node.ELEMENT_NODE) && (xmlDescriptor.getSchemaReference() != null) && (xmlDescriptor.getSchemaReference().getType() == XMLSchemaReference.COMPLEX_TYPE)) {
            Attr typeAttr = ((Element) node).getAttributeNodeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE);
            if (typeAttr == null) {
                NamespaceResolver namespaceResolver = xmlDescriptor.getNonNullNamespaceResolver();
                String xsiPrefix = namespaceResolver.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                if (null == xsiPrefix) {
                    xsiPrefix = namespaceResolver.generatePrefix(Constants.SCHEMA_INSTANCE_PREFIX);
                }
                String value = xmlDescriptor.getSchemaReference().getSchemaContext();
                ((Element) node).setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                ((Element) node).setAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, xsiPrefix + Constants.COLON + Constants.SCHEMA_TYPE_ATTRIBUTE, value);
            } else {
                String value = xmlDescriptor.getSchemaReference().getSchemaContext();
                typeAttr.setValue(value);
            }
        }
    }
    marshal(object, contentHandlerRecord, session, xmlDescriptor, isXMLRoot);
}
Also used : NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord) JSONWithPadding(org.eclipse.persistence.oxm.JSONWithPadding) Element(org.w3c.dom.Element) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) Attr(org.w3c.dom.Attr)

Example 4 with NodeRecord

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

the class TreeObjectBuilder method createRecord.

/**
 * Create a new row/record for the object builder with the given name.
 * This allows subclasses to define different record types.
 */
@Override
public AbstractMarshalRecord createRecord(String rootName, Node parent, AbstractSession session) {
    NodeRecord nRec = new NodeRecord(rootName, getNamespaceResolver(), parent);
    nRec.setSession(session);
    return nRec;
}
Also used : NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord)

Example 5 with NodeRecord

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

the class XMLMarshaller method objectToXML.

public Document objectToXML(Object object, Node parent, DocumentPreservationPolicy docPresPolicy) {
    boolean isXMLRoot = (object instanceof Root);
    AbstractSession session = null;
    XMLDescriptor descriptor = null;
    if (isXMLRoot) {
        try {
            session = context.getSession(((Root) object).getObject());
            if (session != null) {
                descriptor = getDescriptor(((Root) object).getObject(), session);
            }
        } catch (XMLMarshalException marshalException) {
            if (!isSimpleXMLRoot((Root) object)) {
                throw marshalException;
            }
        }
    } else {
        Class<?> objectClass = object.getClass();
        session = context.getSession(objectClass);
        descriptor = getDescriptor(objectClass, session);
    }
    String localRootName = descriptor.getDefaultRootElement();
    if (null == localRootName) {
        throw XMLMarshalException.defaultRootElementNotSpecified(descriptor);
    }
    if (docPresPolicy == null) {
        docPresPolicy = context.getDocumentPreservationPolicy(session);
    }
    if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) {
        XMLRecord xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecord(localRootName, parent, session);
        xmlRow.setMarshaller(this);
        if (this.attachmentMarshaller != null) {
            xmlRow.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
        }
        return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy);
    }
    MarshalRecord marshalRecord = new NodeRecord(localRootName, parent);
    marshalRecord.setMarshaller(this);
    marshal(object, marshalRecord, session, descriptor, isXMLRoot);
    return marshalRecord.getDocument();
}
Also used : NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord) Root(org.eclipse.persistence.internal.oxm.Root) MarshalRecord(org.eclipse.persistence.oxm.record.MarshalRecord) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Aggregations

NodeRecord (org.eclipse.persistence.oxm.record.NodeRecord)7 MarshalRecord (org.eclipse.persistence.oxm.record.MarshalRecord)3 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)2 AbstractMarshalRecord (org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord)2 ValidatingMarshalRecord (org.eclipse.persistence.oxm.record.ValidatingMarshalRecord)2 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 QName (javax.xml.namespace.QName)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)1 Root (org.eclipse.persistence.internal.oxm.Root)1 PrefixMapperNamespaceResolver (org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 JSONWithPadding (org.eclipse.persistence.oxm.JSONWithPadding)1 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)1