public boolean marshal(
      XPathFragment xPathFragment,
      MarshalRecord marshalRecord,
      Object object,
      AbstractSession session,
      NamespaceResolver namespaceResolver) {
    if (xmlBinaryDataCollectionMapping.isReadOnly()) {
      return false;
    }
    Object collection =
        xmlBinaryDataCollectionMapping.getAttributeAccessor().getAttributeValueFromObject(object);
    if (null == collection) {
      return false;
    }
    String xopPrefix = null;
    // If the field's resolver is non-null and has an entry for XOP,
    // use it - otherwise, create a new resolver, set the XOP entry,
    // on it, and use it instead.
    // We do this to avoid setting the XOP namespace declaration on
    // a given field or descriptor's resolver, as it is only required
    // on the current element
    if (namespaceResolver != null) {
      xopPrefix = namespaceResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
    }
    if (xopPrefix == null || namespaceResolver == null) {
      xopPrefix = XMLConstants.XOP_PREFIX;
      marshalRecord.getNamespaceResolver().put(xopPrefix, XMLConstants.XOP_URL);
    }

    ContainerPolicy cp = getContainerPolicy();
    Object iterator = cp.iteratorFor(collection);
    while (cp.hasNext(iterator)) {
      Object objectValue = cp.next(iterator, session);
      marshalSingleValue(
          xPathFragment,
          marshalRecord,
          object,
          objectValue,
          session,
          namespaceResolver,
          ObjectMarshalContext.getInstance());
    }

    marshalRecord.getNamespaceResolver().removeNamespace(XMLConstants.XOP_PREFIX);
    return true;
  }
 public ContainerPolicy getContainerPolicy() {
   return xmlBinaryDataCollectionMapping.getContainerPolicy();
 }
  public boolean marshalSingleValue(
      XPathFragment xPathFragment,
      MarshalRecord marshalRecord,
      Object object,
      Object objectValue,
      AbstractSession session,
      NamespaceResolver namespaceResolver,
      MarshalContext marshalContext) {
    if (objectValue == null) {
      return false;
    }
    boolean addDeclaration = false;
    boolean removePrefix = false;
    String xopPrefix = null;
    if (namespaceResolver != null) {
      xopPrefix = namespaceResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
    }
    if (xopPrefix == null) {
      // check for it in the MarshalRecord's NamespaceResolver
      addDeclaration = true;
      xopPrefix = marshalRecord.getNamespaceResolver().resolveNamespaceURI(XMLConstants.XOP_URL);
      if (xopPrefix == null) {
        // if it's still null, add it, and make a note to remove it later
        removePrefix = true;
        xopPrefix = XMLConstants.XOP_PREFIX;
        marshalRecord.getNamespaceResolver().put(xopPrefix, XMLConstants.XOP_URL);
      }
      namespaceResolver = marshalRecord.getNamespaceResolver();
    }

    XMLMarshaller marshaller = marshalRecord.getMarshaller();
    if (xmlBinaryDataCollectionMapping.getValueConverter() != null) {
      Converter converter = xmlBinaryDataCollectionMapping.getValueConverter();
      if (converter instanceof XMLConverter) {
        objectValue =
            ((XMLConverter) converter)
                .convertObjectValueToDataValue(objectValue, session, marshaller);
      } else {
        objectValue = converter.convertObjectValueToDataValue(objectValue, session);
      }
    }
    XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
    marshalRecord.closeStartGroupingElements(groupingFragment);
    marshalRecord.openStartElement(xPathFragment, namespaceResolver);
    marshalRecord.closeStartElement();

    if (xmlBinaryDataCollectionMapping.isSwaRef() && marshaller.getAttachmentMarshaller() != null) {
      // object value should be a DataHandler
      String c_id = null;
      byte[] bytes = null;
      if (xmlBinaryDataCollectionMapping.getAttributeElementClass()
          == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
        c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) objectValue);
        if (c_id == null) {
          bytes =
              XMLBinaryDataHelper.getXMLBinaryDataHelper()
                  .getBytesForBinaryValue( //
                      objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object))
                  .getData();
        }
      } else {
        XMLBinaryDataHelper.EncodedData data =
            XMLBinaryDataHelper.getXMLBinaryDataHelper()
                .getBytesForBinaryValue( //
                    objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object));
        bytes = data.getData();
        c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment(bytes, 0, bytes.length);
      }
      if (c_id != null) {
        marshalRecord.characters(c_id);
      } else {
        String value =
            getValueToWrite(
                ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(),
                bytes,
                session);
        marshalRecord.characters(value);
      }
    } else {
      if (marshalRecord.isXOPPackage()
          && !xmlBinaryDataCollectionMapping.shouldInlineBinaryData()) {
        XPathFragment lastFrag =
            ((XMLField) xmlBinaryDataCollectionMapping.getField()).getLastXPathFragment();
        String c_id = XMLConstants.EMPTY_STRING;
        byte[] bytes = null;
        if (objectValue.getClass() == ClassConstants.APBYTE) {
          bytes = (byte[]) objectValue;
          c_id =
              marshaller
                  .getAttachmentMarshaller()
                  .addMtomAttachment(
                      bytes,
                      0,
                      bytes.length,
                      this.xmlBinaryDataCollectionMapping.getMimeType(object),
                      lastFrag.getLocalName(),
                      lastFrag.getNamespaceURI());
        } else if (xmlBinaryDataCollectionMapping.getAttributeClassification()
            == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
          c_id =
              marshaller
                  .getAttachmentMarshaller()
                  .addMtomAttachment(
                      (DataHandler) objectValue,
                      lastFrag.getLocalName(),
                      lastFrag.getNamespaceURI());
          if (c_id == null) {
            bytes =
                XMLBinaryDataHelper.getXMLBinaryDataHelper()
                    .getBytesForBinaryValue( //
                        objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object))
                    .getData();
          }
        } else {
          XMLBinaryDataHelper.EncodedData data =
              XMLBinaryDataHelper.getXMLBinaryDataHelper()
                  .getBytesForBinaryValue( //
                      objectValue,
                      marshaller,
                      xmlBinaryDataCollectionMapping.getMimeTypePolicy().getMimeType(object));
          bytes = data.getData();
          c_id =
              marshaller
                  .getAttachmentMarshaller()
                  .addMtomAttachment(
                      bytes,
                      0,
                      bytes.length, //
                      data.getMimeType(),
                      lastFrag.getLocalName(),
                      lastFrag.getNamespaceURI());
        }
        if (c_id == null) {
          String value =
              getValueToWrite(
                  ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(),
                  bytes,
                  session);
          marshalRecord.characters(value);
        } else {
          XPathFragment xopInclude = new XPathFragment(xopPrefix + ":Include");
          xopInclude.setNamespaceURI(XMLConstants.XOP_URL);
          marshalRecord.openStartElement(xopInclude, namespaceResolver);
          marshalRecord.attribute("", "href", "href", c_id);
          if (addDeclaration) {
            marshalRecord.attribute(
                XMLConstants.XMLNS_URL,
                xopPrefix,
                XMLConstants.XMLNS + XMLConstants.COLON + xopPrefix,
                XMLConstants.XOP_URL);
            // marshalRecord.attribute(new XPathFragment("@xmlns:" + xopPrefix), namespaceResolver,
            // XMLConstants.XOP_URL);
          }
          marshalRecord.closeStartElement();
          marshalRecord.endElement(xPathFragment, namespaceResolver);
          // marshal as an attachment.
        }
      } else {
        String value = XMLConstants.EMPTY_STRING;
        if (objectValue.getClass() == ClassConstants.ABYTE
            || objectValue.getClass() == ClassConstants.APBYTE) {
          value =
              getValueToWrite(
                  ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(),
                  objectValue,
                  session);
        } else {
          byte[] bytes =
              XMLBinaryDataHelper.getXMLBinaryDataHelper()
                  .getBytesForBinaryValue( //
                      objectValue,
                      marshaller,
                      xmlBinaryDataCollectionMapping.getMimeTypePolicy().getMimeType(object))
                  .getData();
          value =
              getValueToWrite(
                  ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(),
                  bytes,
                  session);
        }
        marshalRecord.characters(value);
      }
    }
    marshalRecord.endElement(xPathFragment, namespaceResolver);
    if (removePrefix) {
      marshalRecord.getNamespaceResolver().removeNamespace(XMLConstants.XOP_PREFIX);
    }
    return true;
  }
 public void setContainerInstance(Object object, Object containerInstance) {
   xmlBinaryDataCollectionMapping.setAttributeValueInObject(object, containerInstance);
 }
 public boolean startElement(
     XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
   try {
     XMLField xmlField = (XMLField) xmlBinaryDataCollectionMapping.getField();
     XPathFragment lastFragment = xmlField.getLastXPathFragment();
     if (!lastFragment.isAttribute()) {
       // set a new content handler to deal with the Include element's event.
       BinaryMappingContentHandler handler =
           new BinaryMappingContentHandler(
               unmarshalRecord, this, this.xmlBinaryDataCollectionMapping);
       String qnameString = xPathFragment.getLocalName();
       if (xPathFragment.getPrefix() != null) {
         qnameString = xPathFragment.getPrefix() + XMLConstants.COLON + qnameString;
       }
       handler.startElement(
           xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
       unmarshalRecord.getXMLReader().setContentHandler(handler);
     } else if (lastFragment.isAttribute()) {
       // handle swaRef and inline attribute cases here:
       String value = atts.getValue(lastFragment.getNamespaceURI(), lastFragment.getLocalName());
       Object fieldValue = null;
       if (xmlBinaryDataCollectionMapping.isSwaRef()) {
         if (unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller() != null) {
           if (xmlBinaryDataCollectionMapping.getAttributeClassification()
               == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
             fieldValue =
                 unmarshalRecord
                     .getUnmarshaller()
                     .getAttachmentUnmarshaller()
                     .getAttachmentAsDataHandler(value);
           } else {
             fieldValue =
                 unmarshalRecord
                     .getUnmarshaller()
                     .getAttachmentUnmarshaller()
                     .getAttachmentAsByteArray(value);
           }
           xmlBinaryDataCollectionMapping.setAttributeValueInObject(
               unmarshalRecord.getCurrentObject(),
               XMLBinaryDataHelper.getXMLBinaryDataHelper()
                   .convertObject(
                       fieldValue,
                       xmlBinaryDataCollectionMapping.getAttributeClassification(),
                       unmarshalRecord.getSession()));
         }
       } else {
         // value should be base64 binary string
         fieldValue =
             ((XMLConversionManager)
                     unmarshalRecord.getSession().getDatasourcePlatform().getConversionManager())
                 .convertSchemaBase64ToByteArray(value);
         xmlBinaryDataCollectionMapping.setAttributeValueInObject(
             unmarshalRecord.getCurrentObject(),
             XMLBinaryDataHelper.getXMLBinaryDataHelper()
                 .convertObject(
                     fieldValue,
                     xmlBinaryDataCollectionMapping.getAttributeClassification(),
                     unmarshalRecord.getSession()));
       }
     }
     return true;
   } catch (SAXException ex) {
     throw XMLMarshalException.unmarshalException(ex);
   }
 }