Search in sources :

Example 1 with ValidatingMarshalRecord

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

the class XMLMarshaller method marshal.

/**
 * Convert the given object to XML and update the given marshal record with
 * that XML Document.
 * @param object the object to marshal
 * @param marshalRecord the marshalRecord to marshal the object to
 * @param descriptor the XMLDescriptor for the object being marshalled
 */
protected void marshal(Object object, MarshalRecord marshalRecord, ABSTRACT_SESSION session, DESCRIPTOR descriptor, boolean isXMLRoot) {
    SessionLog logger = AbstractSessionLog.getLog();
    if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
        logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_start_marshalling", new Object[] { (object != null) ? object.getClass().getName() : "N/A", this.mediaType });
    }
    if (object != null && logPayload != null && this.isLogPayload()) {
        AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.MOXY, object.toString(), new Object[0], false);
    }
    if (null != schema) {
        marshalRecord = new ValidatingMarshalRecord(marshalRecord, this);
    }
    if (this.attachmentMarshaller != null) {
        marshalRecord.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
    }
    marshalRecord.setMarshaller(this);
    Root root = null;
    if (isXMLRoot) {
        root = (Root) object;
    }
    Node node = getNode(object, marshalRecord.getDOM(), session, descriptor, isXMLRoot);
    if (this.mapper == null) {
        if (null == node) {
            addDescriptorNamespacesToXMLRecord(descriptor, marshalRecord);
        }
    } else {
        if (descriptor == null || null != node) {
            marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper, null));
        } else {
            marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper, descriptor.getNamespaceResolver()));
        }
        marshalRecord.setCustomNamespaceMapper(true);
    }
    if (this.getMarshalAttributeGroup() != null) {
        if (marshalAttributeGroup.getClass() == CoreClassConstants.STRING) {
            CoreAttributeGroup group = descriptor.getAttributeGroup((String) marshalAttributeGroup);
            if (group != null) {
                marshalRecord.pushAttributeGroup(group);
            } else {
                throw XMLMarshalException.invalidAttributeGroupName((String) marshalAttributeGroup, descriptor.getJavaClassName());
            }
        } else if (marshalAttributeGroup instanceof CoreAttributeGroup) {
            marshalRecord.pushAttributeGroup((CoreAttributeGroup) marshalAttributeGroup);
        } else {
        // Error case
        }
    }
    NamespaceResolver nr = marshalRecord.getNamespaceResolver();
    if (node != null) {
        if (isXMLRoot) {
            if (isFragment()) {
                marshalRecord.node(node, null, root.getNamespaceURI(), root.getLocalName());
            } else {
                String encoding = root.getEncoding();
                if (null == encoding) {
                    encoding = Constants.DEFAULT_XML_ENCODING;
                }
                String version = root.getXMLVersion();
                if (null == version) {
                    version = DEFAULT_XML_VERSION;
                }
                marshalRecord.startDocument(encoding, version);
                marshalRecord.node(node, marshalRecord.getNamespaceResolver(), root.getNamespaceURI(), root.getLocalName());
                marshalRecord.endDocument();
            }
        } else {
            marshalRecord.node(node, nr);
        }
        marshalRecord.flush();
        return;
    }
    if (isXMLRoot) {
        if (descriptor != null) {
            marshalRecord.beforeContainmentMarshal(root.getObject());
        }
    } else {
        marshalRecord.beforeContainmentMarshal(object);
    }
    if (!isFragment()) {
        String encoding = getEncoding();
        String version = DEFAULT_XML_VERSION;
        if (!isXMLRoot && descriptor != null) {
            marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
        } else {
            if (root.getEncoding() != null) {
                encoding = root.getEncoding();
            }
            if (root.getXMLVersion() != null) {
                version = root.getXMLVersion();
            }
        }
        marshalRecord.startDocument(encoding, version);
    }
    if (getXmlHeader() != null) {
        marshalRecord.writeHeader();
    }
    if (isXMLRoot) {
        if (root.getObject() instanceof Node) {
            marshalRecord.node((Node) root.getObject(), new NamespaceResolver(), root.getNamespaceURI(), root.getLocalName());
            marshalRecord.endDocument();
            return;
        }
    }
    XPathFragment rootFragment = buildRootFragment(object, descriptor, isXMLRoot, marshalRecord);
    String schemaLocation = getSchemaLocation();
    String noNsSchemaLocation = getNoNamespaceSchemaLocation();
    boolean isNil = false;
    if (isXMLRoot) {
        object = root.getObject();
        if (root.getSchemaLocation() != null) {
            schemaLocation = root.getSchemaLocation();
        }
        if (root.getNoNamespaceSchemaLocation() != null) {
            noNsSchemaLocation = root.getNoNamespaceSchemaLocation();
        }
        marshalRecord.setLeafElementType(root.getSchemaType());
        isNil = root.isNil();
    }
    String xsiPrefix = null;
    if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || (isNil)) {
        xsiPrefix = nr.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        if (null == xsiPrefix) {
            xsiPrefix = Constants.SCHEMA_INSTANCE_PREFIX;
            nr.put(Constants.SCHEMA_INSTANCE_PREFIX, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        }
    }
    OBJECT_BUILDER treeObjectBuilder = null;
    if (descriptor != null) {
        treeObjectBuilder = (OBJECT_BUILDER) descriptor.getObjectBuilder();
    }
    if (session == null) {
        session = (ABSTRACT_SESSION) context.getSession();
    }
    marshalRecord.setSession(session);
    if (null != rootFragment && !(rootFragment.getLocalName().equals(Constants.EMPTY_STRING))) {
        marshalRecord.startPrefixMappings(nr);
        if (!isXMLRoot && descriptor != null && descriptor.getNamespaceResolver() == null && rootFragment.hasNamespace()) {
            // throw an exception if the name has a : in it but the namespaceresolver is null
            throw XMLMarshalException.namespaceResolverNotSpecified(rootFragment.getShortName());
        }
        if (isIncludeRoot()) {
            marshalRecord.openStartElement(rootFragment, nr);
        }
        if (null != schemaLocation) {
            marshalRecord.attributeWithoutQName(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_LOCATION, xsiPrefix, schemaLocation);
        }
        if (null != noNsSchemaLocation) {
            marshalRecord.attributeWithoutQName(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.NO_NS_SCHEMA_LOCATION, xsiPrefix, noNsSchemaLocation);
        }
        if (isNil) {
            marshalRecord.nilSimple(nr);
        }
        marshalRecord.namespaceDeclarations(nr);
        if (descriptor != null && !isNil) {
            marshalRecord.addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true);
            treeObjectBuilder.marshalAttributes(marshalRecord, object, session);
        }
        if (isIncludeRoot()) {
            marshalRecord.closeStartElement();
        }
    } else {
        // no rootfragment
        marshalRecord.marshalWithoutRootElement(treeObjectBuilder, object, descriptor, root, isXMLRoot);
    }
    if (treeObjectBuilder != null && !isNil) {
        treeObjectBuilder.buildRow(marshalRecord, object, session, this, rootFragment);
    } else if (isXMLRoot) {
        if (object != null && !isNil) {
            if (root.getDeclaredType() != null && root.getObject() != null && root.getDeclaredType() != root.getObject().getClass()) {
                QName type = marshalRecord.getConversionManager().schemaType(object.getClass());
                if (type != null) {
                    xsiPrefix = nr.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                    if (null == xsiPrefix) {
                        xsiPrefix = Constants.SCHEMA_INSTANCE_PREFIX;
                        marshalRecord.namespaceDeclaration(xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                    }
                    marshalRecord.namespaceDeclaration(Constants.SCHEMA_PREFIX, javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    String typeValue = type.getLocalPart();
                    if (marshalRecord.isNamespaceAware() && (mediaType.isApplicationXML() || getJsonTypeConfiguration().useXsdTypesWithPrefix())) {
                        typeValue = Constants.SCHEMA_PREFIX + marshalRecord.getNamespaceSeparator() + typeValue;
                    }
                    marshalRecord.attribute(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE, xsiPrefix + Constants.COLON + Constants.SCHEMA_TYPE_ATTRIBUTE, typeValue);
                }
            }
            marshalRecord.characters(root.getSchemaType(), object, null, false);
        }
    }
    if (null != rootFragment && !(rootFragment.getLocalName().equals(Constants.EMPTY_STRING)) && isIncludeRoot()) {
        marshalRecord.endElement(rootFragment, nr);
        marshalRecord.endPrefixMappings(nr);
    }
    if (!isFragment()) {
        marshalRecord.endDocument();
    }
    if (isXMLRoot) {
        if (descriptor != null) {
            marshalRecord.afterContainmentMarshal(null, root.getObject());
        }
    } else {
        marshalRecord.afterContainmentMarshal(null, object);
    }
    marshalRecord.flush();
}
Also used : ValidatingMarshalRecord(org.eclipse.persistence.oxm.record.ValidatingMarshalRecord) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver)

Aggregations

QName (javax.xml.namespace.QName)1 CoreAttributeGroup (org.eclipse.persistence.core.queries.CoreAttributeGroup)1 PrefixMapperNamespaceResolver (org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver)1 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)1 SessionLog (org.eclipse.persistence.logging.SessionLog)1 ValidatingMarshalRecord (org.eclipse.persistence.oxm.record.ValidatingMarshalRecord)1 Node (org.w3c.dom.Node)1