Search in sources :

Example 11 with XMLConversionManager

use of org.eclipse.persistence.internal.oxm.XMLConversionManager 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 12 with XMLConversionManager

use of org.eclipse.persistence.internal.oxm.XMLConversionManager 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)

Example 13 with XMLConversionManager

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

the class DateAndTimeTestCases method testUtilDateToString_time_dstTimeZone.

public void testUtilDateToString_time_dstTimeZone() {
    XMLConversionManager xmlConversionManager = (XMLConversionManager) XMLConversionManager.getDefaultXMLManager().clone();
    xmlConversionManager.setTimeZone(TimeZone.getTimeZone(CONTROL_DST_TIME_ZONE));
    xmlConversionManager.setTimeZoneQualified(true);
    java.util.Date utilDate = xmlConversionManager.convertObject(CONTROL_DST_INPUT_DATE_TIME, java.util.Date.class);
    String testString = xmlConversionManager.convertObject(utilDate, String.class, XMLConstants.TIME_QNAME);
    assertEquals("03:00:00" + controlXmlConversionTimeSuffix + "-04:00", testString);
}
Also used : XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 14 with XMLConversionManager

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

the class DateAndTimeTestCases method testSqlDateToString_time_dstTimeZone.

public void testSqlDateToString_time_dstTimeZone() {
    XMLConversionManager xmlConversionManager = (XMLConversionManager) XMLConversionManager.getDefaultXMLManager().clone();
    xmlConversionManager.setTimeZone(TimeZone.getTimeZone(CONTROL_DST_TIME_ZONE));
    xmlConversionManager.setTimeZoneQualified(true);
    java.sql.Date sqlDate = xmlConversionManager.convertObject(CONTROL_DST_INPUT_DATE_TIME, java.sql.Date.class, XMLConstants.DATE_TIME_QNAME);
    String testString = xmlConversionManager.convertObject(sqlDate, String.class, XMLConstants.TIME_QNAME);
    assertEquals("03:00:00" + controlXmlConversionTimeSuffix + "-04:00", testString);
}
Also used : XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 15 with XMLConversionManager

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

the class DateAndTimeTestCases method testSqlTimeToString_time_dstTimeZone.

public void testSqlTimeToString_time_dstTimeZone() {
    XMLConversionManager xmlConversionManager = (XMLConversionManager) XMLConversionManager.getDefaultXMLManager().clone();
    xmlConversionManager.setTimeZone(TimeZone.getTimeZone(CONTROL_DST_TIME_ZONE));
    xmlConversionManager.setTimeZoneQualified(true);
    java.sql.Time sqlTime = xmlConversionManager.convertObject(CONTROL_DST_INPUT_DATE_TIME, Time.class, XMLConstants.DATE_TIME_QNAME);
    String testString = xmlConversionManager.convertObject(sqlTime, String.class, XMLConstants.TIME_QNAME);
    assertEquals("03:00:00" + controlXmlConversionTimeSuffix + "-04:00", testString);
}
Also used : Time(java.sql.Time) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Aggregations

XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)34 XMLField (org.eclipse.persistence.oxm.XMLField)12 QName (javax.xml.namespace.QName)10 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)8 ArrayList (java.util.ArrayList)6 Element (org.w3c.dom.Element)6 Time (java.sql.Time)4 Timestamp (java.sql.Timestamp)4 Vector (java.util.Vector)3 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)3 SDOProperty (org.eclipse.persistence.sdo.SDOProperty)3 SDOType (org.eclipse.persistence.sdo.SDOType)3 DataObject (commonj.sdo.DataObject)2 Property (commonj.sdo.Property)2 Type (commonj.sdo.Type)2 DataHandler (jakarta.activation.DataHandler)2 List (java.util.List)2 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)2 XMLBinaryDataHelper (org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2