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) anXMLMarshaller.getMarshalListener())
          .setMarshalledObject(xmlDocument.getRootObject());
      ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
          .setMarshalledObjectRootQName(
              new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));

      if (result instanceof SAXResult) {
        ContentHandlerRecord marshalRecord = new ContentHandlerRecord();
        marshalRecord.setContentHandler(((SAXResult) result).getHandler());
        marshalRecord.setMarshaller(anXMLMarshaller);
        ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
            .setRootMarshalRecord(marshalRecord);
        anXMLMarshaller.marshal(xmlDocument, marshalRecord);
      } else if (result instanceof DOMResult) {
        NodeRecord marshalRecord = new NodeRecord();
        marshalRecord.setDOM(((DOMResult) result).getNode());
        marshalRecord.setMarshaller(anXMLMarshaller);
        ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
            .setRootMarshalRecord(marshalRecord);
        anXMLMarshaller.marshal(xmlDocument, marshalRecord);
      } 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);
      }
    }
  }