Search in sources :

Example 31 with NamespaceResolver

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

the class XMLObjectBuilder method createRecord.

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

Example 32 with NamespaceResolver

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

the class XMLBinaryDataCollectionMapping method writeSingleValue.

@Override
public void writeSingleValue(Object value, Object parent, XMLRecord record, AbstractSession session) {
    XMLField field = (XMLField) getField();
    NamespaceResolver resolver = field.getNamespaceResolver();
    boolean isAttribute = field.getLastXPathFragment().isAttribute();
    String prefix = null;
    XMLField includeField = null;
    if (!isAttribute) {
        if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
            field = (XMLField) getField();
            // on the current element
            if (resolver != null) {
                prefix = resolver.resolveNamespaceURI(XMLConstants.XOP_URL);
            }
            if (prefix == null) {
                // "xop";
                prefix = XMLConstants.XOP_PREFIX;
                resolver = new NamespaceResolver();
                resolver.put(prefix, XMLConstants.XOP_URL);
            }
            includeField = new XMLField(prefix + XMLConstants.COLON + INCLUDE + "/@href");
            includeField.setNamespaceResolver(resolver);
        }
    }
    XMLField textField = new XMLField(field.getXPath() + '/' + XMLConstants.TEXT);
    textField.setNamespaceResolver(field.getNamespaceResolver());
    textField.setSchemaType(field.getSchemaType());
    Object valueToWrite = getValueToWrite(value, parent, record, field, includeField, session);
    if (!isAttribute && valueToWrite.getClass() == ClassConstants.ABYTE) {
        // if the returned value is a byte[] and not an XMLRecord, just write it inline
        record.add(textField, valueToWrite);
    }
    record.add(field, valueToWrite);
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver)

Example 33 with NamespaceResolver

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

the class XMLBinaryDataCollectionMapping method valueFromRow.

@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) {
    ContainerPolicy cp = this.getContainerPolicy();
    Object fieldValue = row.getValues(this.getField());
    if (fieldValue == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
        if (reuseContainer) {
            Object currentObject = ((XMLRecord) row).getCurrentObject();
            Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
            return container != null ? container : cp.containerInstance();
        } else {
            return cp.containerInstance();
        }
    }
    Object result = null;
    if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        result = container != null ? container : cp.containerInstance();
    } else {
        result = cp.containerInstance(fieldValues.size());
    }
    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
        Object element = stream.nextElement();
        // PERF: Direct variable access.
        // Object value = row.get(field);
        // Object fieldValue = null;
        XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
        if (element instanceof String) {
            if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
                fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element);
            } else if (!this.isSwaRef()) {
                // should be base64
                byte[] bytes = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(element);
                fieldValue = bytes;
            }
        } else {
            // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
            XMLRecord record = (XMLRecord) element;
            if (getNullPolicy().valueIsNull((Element) record.getDOM())) {
                fieldValue = null;
            } else {
                record.setSession(executionSession);
                if ((unmarshaller.getAttachmentUnmarshaller() != null) && unmarshaller.getAttachmentUnmarshaller().isXOPPackage() && !this.isSwaRef() && !this.shouldInlineBinaryData()) {
                    // look for the include element:
                    String xpath = XMLConstants.EMPTY_STRING;
                    // need a prefix for XOP
                    String prefix = null;
                    NamespaceResolver descriptorResolver = ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
                    if (descriptorResolver != null) {
                        prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
                    }
                    if (prefix == null) {
                        prefix = XMLConstants.XOP_PREFIX;
                    }
                    NamespaceResolver tempResolver = new NamespaceResolver();
                    tempResolver.put(prefix, XMLConstants.XOP_URL);
                    xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href";
                    XMLField field = new XMLField(xpath);
                    field.setNamespaceResolver(tempResolver);
                    String includeValue = (String) record.get(field);
                    if (element != null && includeValue != null) {
                        if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) {
                            fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
                        } else {
                            fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
                        }
                    } else {
                        // If we didn't find the Include element, check for inline
                        fieldValue = record.get(XMLConstants.TEXT);
                        // should be a base64 string
                        fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
                    }
                } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
                    String refValue = (String) record.get(XMLConstants.TEXT);
                    if (refValue != null) {
                        fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
                    }
                } else {
                    fieldValue = record.get(XMLConstants.TEXT);
                    // should be a base64 string
                    fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
                }
            }
        }
        Object attributeValue = convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
        cp.addInto(attributeValue, result, query.getSession());
    }
    return result;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Enumeration(java.util.Enumeration) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) Vector(java.util.Vector) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 34 with NamespaceResolver

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

the class XMLBinaryDataCollectionMapping method getValueToWrite.

public Object getValueToWrite(Object value, Object parent, XMLRecord record, XMLField field, XMLField includeField, AbstractSession session) {
    XMLMarshaller marshaller = record.getMarshaller();
    Object element = convertObjectValueToDataValue(value, session, record.getMarshaller());
    boolean isAttribute = ((XMLField) getField()).getLastXPathFragment().isAttribute();
    if (element == null) {
        return null;
    }
    if (isAttribute) {
        if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
            // should be a DataHandler here
            try {
                String id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) element);
                element = id;
            } catch (ClassCastException cce) {
                throw XMLMarshalException.invalidSwaRefAttribute(getAttributeClassification().getName());
            }
        } else {
            // inline case
            XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element, record.getMarshaller(), mimeTypePolicy.getMimeType(parent));
            String base64Value = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).buildBase64StringFromBytes(data.getData());
            element = base64Value;
        }
    } else {
        if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
            // write as attachment
            String c_id = XMLConstants.EMPTY_STRING;
            byte[] bytes = null;
            if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) {
                if (getAttributeElementClass() == ClassConstants.ABYTE) {
                    element = session.getDatasourcePlatform().getConversionManager().convertObject(element, ClassConstants.APBYTE);
                }
                bytes = (byte[]) element;
                c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(bytes, 0, bytes.length, this.mimeTypePolicy.getMimeType(parent), field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI());
            } else if (getAttributeElementClass() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
                c_id = marshaller.getAttachmentMarshaller().addMtomAttachment((DataHandler) element, field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI());
                if (c_id == null) {
                    XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element, marshaller, this.mimeTypePolicy.getMimeType(parent));
                    bytes = data.getData();
                }
            } else {
                XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element, marshaller, this.mimeTypePolicy.getMimeType(parent));
                bytes = data.getData();
                c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(bytes, 0, bytes.length, data.getMimeType(), field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI());
            }
            if (c_id == null) {
                element = bytes;
            } else {
                DOMRecord include = new DOMRecord(field.getLastXPathFragment().getLocalName());
                include.setSession(session);
                include.put(includeField, c_id);
                element = include;
                // Need to call setAttributeNS on the record, unless the xop prefix
                // is defined on the descriptor's resolver already
                NamespaceResolver resolver = ((XMLField) getField()).getNamespaceResolver();
                if (resolver == null || resolver.resolveNamespaceURI(XMLConstants.XOP_URL) == null) {
                    resolver = new NamespaceResolver();
                    resolver.put(XMLConstants.XOP_PREFIX, XMLConstants.XOP_URL);
                    String xpath = XMLConstants.XOP_PREFIX + XMLConstants.COLON + INCLUDE;
                    XMLField incField = new XMLField(xpath);
                    incField.setNamespaceResolver(resolver);
                    Object obj = include.getIndicatingNoEntry(incField);
                    if (obj != null && obj instanceof DOMRecord) {
                        if (((DOMRecord) obj).getDOM().getNodeType() == Node.ELEMENT_NODE) {
                            ((Element) ((DOMRecord) obj).getDOM()).setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + XMLConstants.XOP_PREFIX, XMLConstants.XOP_URL);
                        }
                    }
                }
            }
        } else if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
            // element should be a data-handler
            try {
                String c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) element);
                element = c_id;
            } catch (Exception ex) {
            }
        } else {
            // inline
            if (!((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE))) {
                element = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element, marshaller, this.mimeTypePolicy.getMimeType(parent)).getData();
            }
        }
    }
    return element;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLBinaryDataHelper(org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element) DataHandler(jakarta.activation.DataHandler) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 35 with NamespaceResolver

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

the class XMLBinaryDataMapping method valueFromRow.

@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) {
    // PERF: Direct variable access.
    Object value = row.get(this.field);
    if (value == null) {
        return value;
    }
    Object fieldValue = null;
    XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
    if (value instanceof String) {
        if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
            if (getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
                fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) value);
            } else {
                fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray((String) value);
            }
        } else if (!this.isSwaRef()) {
            // should be base64
            byte[] bytes = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(value);
            fieldValue = bytes;
        }
    } else if (value instanceof byte[] || value instanceof Byte[]) {
        fieldValue = value;
    } else {
        // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
        XMLRecord record = (XMLRecord) value;
        if (getNullPolicy().valueIsNull((Element) record.getDOM())) {
            return null;
        }
        record.setSession(executionSession);
        if ((unmarshaller.getAttachmentUnmarshaller() != null) && unmarshaller.getAttachmentUnmarshaller().isXOPPackage() && !this.isSwaRef() && !this.shouldInlineBinaryData()) {
            // look for the include element:
            String xpath = XMLConstants.EMPTY_STRING;
            // need a prefix for XOP
            String prefix = null;
            NamespaceResolver descriptorResolver = ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
            // 20061023: handle NPE on null NSR
            if (descriptorResolver != null) {
                prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
            }
            if (prefix == null) {
                prefix = XMLConstants.XOP_PREFIX;
            }
            NamespaceResolver tempResolver = new NamespaceResolver();
            tempResolver.put(prefix, XMLConstants.XOP_URL);
            xpath = prefix + include;
            XMLField field = new XMLField(xpath);
            field.setNamespaceResolver(tempResolver);
            String includeValue = (String) record.get(field);
            if (includeValue != null) {
                if ((getAttributeClassification() == ClassConstants.ABYTE) || (getAttributeClassification() == ClassConstants.APBYTE)) {
                    fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
                } else {
                    fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
                }
            } else {
                // If we didn't find the Include element, check for inline
                fieldValue = record.get(XMLConstants.TEXT);
                // should be a base64 string
                fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
            }
        } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
            String refValue = (String) record.get(XMLConstants.TEXT);
            if (refValue != null) {
                fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
            }
        } else {
            fieldValue = record.get(XMLConstants.TEXT);
            // should be a base64 string
            if (fieldValue != null) {
                fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(fieldValue);
            } else {
                fieldValue = new byte[0];
            }
        }
    }
    Object attributeValue = convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
    attributeValue = XMLBinaryDataHelper.getXMLBinaryDataHelper().convertObject(attributeValue, getAttributeClassification(), executionSession, null);
    return attributeValue;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) Element(org.w3c.dom.Element) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Aggregations

NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)227 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)142 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)74 XMLField (org.eclipse.persistence.oxm.XMLField)36 XMLSchemaClassPathReference (org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)36 Document (org.w3c.dom.Document)35 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)23 QName (javax.xml.namespace.QName)20 InputStream (java.io.InputStream)17 StringReader (java.io.StringReader)17 StringWriter (java.io.StringWriter)17 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)16 InputSource (org.xml.sax.InputSource)14 Element (org.w3c.dom.Element)13 XMLCollectionReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)12 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)12 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)10 HashMap (java.util.HashMap)9 Project (org.eclipse.persistence.sessions.Project)9 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)8