/**
  * Serializes an XMLDocument as an XML document into the outputStream. If the DataObject's Type
  * was defined by an XSD, the serialization will follow the XSD. Otherwise the serialization will
  * follow the format as if an XSD were generated as defined by the SDO specification. The
  * OutputStream will be flushed after writing. Does not perform validation to ensure compliance
  * with an XSD.
  *
  * @param xmlDocument specifies XMLDocument to be saved
  * @param outputStream specifies the OutputStream to write to.
  * @param options implementation-specific options.
  * @throws IOException for stream exceptions.
  * @throws IllegalArgumentException if the dataObject tree is not closed or has no container.
  */
 public void save(XMLDocument xmlDocument, OutputStream outputStream, Object options)
     throws IOException {
   if (xmlDocument == null) {
     throw new IllegalArgumentException(
         SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
   }
   XMLMarshaller xmlMarshaller = getXmlMarshaller(options);
   String encoding = xmlMarshaller.getEncoding();
   if (xmlDocument.getEncoding() != null) {
     encoding = xmlDocument.getEncoding();
   }
   OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding);
   save(xmlDocument, writer, xmlMarshaller);
 }
 protected void verifyDocument(XMLDocument document) {
   super.verifyDocument(document);
   assertEquals(CONTROL_ROOT_NAME, document.getRootElementName());
   assertEquals(CONTROL_ROOT_URI, document.getRootElementURI());
   assertEquals(null, document.getEncoding());
   assertEquals(null, document.getXMLVersion());
   assertEquals(null, document.getNoNamespaceSchemaLocation());
   assertEquals(null, document.getSchemaLocation());
   java.util.List properties = document.getRootObject().getInstanceProperties();
   assertEquals(5, properties.size());
   for (int i = 0; i < properties.size(); i++) {
     Property property = (Property) properties.get(i);
     boolean isSet = document.getRootObject().isSet(property);
     if (property.getName().equals(CUSTOMERID)) {
       assertTrue(isSet);
       Object value = document.getRootObject().get(property);
       assertEquals(CONTROL_CUSTOMERID, value);
     } else if (property.getName().equals(SIN)) {
       assertTrue(isSet);
       Object value = document.getRootObject().get(property);
       assertEquals(CONTROL_SIN, value);
     } else {
       assertFalse(isSet);
     }
   }
 }
  private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller)
      throws IOException {
    if (xmlDocument == null) {
      throw new IllegalArgumentException(
          SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
    }

    // 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());

    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
      writerRecord = new FormattedWriterRecord();
    } else {
      writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(outputWriter);
    writerRecord.setMarshaller(anXMLMarshaller);

    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
        .setMarshalledObject(xmlDocument.getRootObject());
    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
        .setMarshalledObjectRootQName(
            new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()).setRootMarshalRecord(writerRecord);

    anXMLMarshaller.marshal(xmlDocument, writerRecord);
    outputWriter.flush();
  }
  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);
      }
    }
  }
  private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller)
      throws IOException {
    if (xmlDocument == null) {
      throw new IllegalArgumentException(
          SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
    }

    // 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());

    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
      writerRecord = new FormattedWriterRecord();
    } else {
      writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(outputWriter);
    writerRecord.setMarshaller(anXMLMarshaller);

    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
        .setMarshalledObject(xmlDocument.getRootObject());
    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener())
        .setMarshalledObjectRootQName(
            new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
    ((SDOMarshalListener) anXMLMarshaller.getMarshalListener()).setRootMarshalRecord(writerRecord);

    try {
      anXMLMarshaller.marshal(xmlDocument, writerRecord);
    } catch (XMLMarshalException xme) {
      if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
        if (aHelperContext
            != ((SDOType) xmlDocument.getRootObject().getType()).getHelperContext()) {
          throw SDOException.dataObjectNotFromHelperContext();
        }
      }
    }
    outputWriter.flush();
  }