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);
     }
   }
 }
  public void testBinaryConversionForBuiltinType() throws Exception {
    xsdHelper.define(getSchema(getSchemaNameForBuiltinType()));

    FileInputStream inputStream = new FileInputStream(getControlFileName());
    XMLDocument document = xmlHelper.load(inputStream, null, null);

    byte[] bytesFromDocument = (byte[]) document.getRootObject().get("value");

    String controlString = "Hello World!";
    String testString = new String(bytesFromDocument);

    assertEquals(controlString, testString);
  }
 /**
  * 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 verifyAfterLoad(XMLDocument document) {
    super.verifyAfterLoad(document);
    // replace global object with one from xml file (with cs pre-populated)
    rootObject = document.getRootObject();
    salesPO1CS = rootObject.getDataObject("sales/purchaseOrder[1]").getChangeSummary();
    salesPO2CS = rootObject.getDataObject("sales/purchaseOrder[2]").getChangeSummary();
    developmentPO1CS = rootObject.getDataObject("development/purchaseOrder[1]").getChangeSummary();
    developmentPO2CS =
        null; // rootObject.getDataObject("development/purchaseOrder[2]").getChangeSummary();
    stock1CS = rootObject.getDataObject("stock[1]").getChangeSummary();
    stock2CS = rootObject.getDataObject("stock[2]").getChangeSummary();
    stock3CS = rootObject.getDataObject("stock[3]").getChangeSummary();
    DataObject itemsDO = rootObject.getDataObject("sales/purchaseOrder[1]/items");
    DataObject item1DO = null; // itemsDO.getDataObject("item[1]");
    DataObject item1ProductDO = null; // item1DO.getDataObject("product");
    DataObject item1ProductPrice1DO = null; // item1ProductDO.getDataObject("price[1]");
    DataObject item1ProductPrice2DO = null; // item1ProductDO.getDataObject("price[2]");

    assertDeleteDetachUnsetComplexSingleBelowRoot(
        true, //
        false, //
        itemsDO, //
        item1DO, //
        item1ProductDO, //
        item1ProductPrice1DO, //
        item1ProductPrice2DO);
  }
 protected void verifyAfterLoad(XMLDocument document) {
   super.verifyAfterLoad(document);
   ChangeSummary teamCS = document.getRootObject().getChangeSummary();
   assertNotNull(teamCS);
   DataObject manager = document.getRootObject().getDataObject("manager");
   assertNotNull(manager);
   DataObject address = manager.getDataObject("address");
   assertNotNull(address);
   ChangeSummary addressCS = address.getChangeSummary();
   assertEquals(teamCS, addressCS);
   assertTrue(teamCS.isLogging());
   assertTrue(((SDOChangeSummary) teamCS).isLogging());
   assertTrue(teamCS.isCreated(address));
   assertFalse(teamCS.isCreated(manager));
   assertEquals(0, teamCS.getOldValues(address).size());
 }
  protected void verifyAfterLoad(XMLDocument doc) {
    super.verifyAfterLoad(doc);
    Object value = doc.getRootObject().get(ID_NAME);
    boolean isSet = doc.getRootObject().isSet(ID_NAME);
    // verify defaults
    assertNotNull(value);
    assertNotSame(ID_DEFAULT, value);
    assertEquals(99, value);
    assertTrue(isSet);

    value = doc.getRootObject().get(FIRSTNAME_NAME);
    isSet = doc.getRootObject().isSet(FIRSTNAME_NAME);
    assertNotNull(value);
    assertNotSame(FIRSTNAME_DEFAULT, value);
    assertEquals("nonnull", value);
    assertTrue(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();
  }
  protected void verifyAfterLoad(XMLDocument document) {
    assertNull(document.getRootObject().getContainer());
    DataObject po = document.getRootObject();
    assertNotNull(po);
    DataObject shipTo = po.getDataObject("shipTo");
    assertNotNull(shipTo);
    assertNotNull(shipTo.get("street"));

    assertEquals("123 Maple Street", shipTo.get("street"));
    assertEquals(po, shipTo.getContainer());
    DataObject billTo = po.getDataObject("billTo");

    Property extraProp = billTo.getInstanceProperty("testExtra");
    assertNotNull(extraProp);

    List extraContentList = billTo.getList(extraProp);
    assertEquals(1, extraContentList.size());
    assertEquals("extraContext", extraContentList.get(0));

    Setting setting = ((SDODataObject) billTo).getSettings().get(4);
    assertNotNull(setting);
    assertNotNull(billTo);
    assertEquals(po, billTo.getContainer());
    DataObject items = po.getDataObject("items");
    assertNotNull(items);
    assertEquals(po, items.getContainer());
    DataObject item1 = (DataObject) items.getList("item").get(0);
    assertNotNull(item1);
    assertEquals(items, item1.getContainer());
    DataObject item2 = (DataObject) items.getList("item").get(1);
    assertNotNull(item2);
    assertEquals(items, item2.getContainer());
    // unmapped content

    List phones = po.getList("phone");
    assertEquals(2, phones.size());
    DataObject phone = (DataObject) phones.get(0);
    assertNotNull(phone);
    assertEquals(po, phone.getContainer());
    List addrs = phone.getList("addr");
    assertEquals(1, addrs.size());
    DataObject addr = (DataObject) addrs.get(0);
    assertNotNull(addr);
    assertEquals(phone, addr.getContainer());

    List itemList = addr.getList("item");
    assertEquals(1, itemList.size());
    DataObject item = (DataObject) itemList.get(0);
    assertNotNull(item);
    assertEquals(addr, item.getContainer());

    List dwellings = addr.getList("dwelling");
    assertEquals(1, dwellings.size());
    DataObject dwelling = (DataObject) dwellings.get(0);
    assertNotNull(dwelling);
    assertEquals(addr, dwelling.getContainer());

    DataObject phone2 = (DataObject) phones.get(1);
    assertNotNull(phone2);
    List numList = phone2.getList("number");
    assertEquals(1, numList.size());
    assertEquals("12345678", numList.get(0));
    List extList = phone2.getList("ext");

    assertEquals(2, extList.size());
    assertEquals("234", extList.get(0));
    assertEquals("456", extList.get(1));

    List companyNames = po.getList("companyName");
    assertNotNull(companyNames);

    String companyName = (String) companyNames.get(0);

    Property attrProp = xsdHelper.getGlobalProperty("http://www.example.org", "globalTest", true);
    assertNotNull(attrProp);
    Property elemProp = xsdHelper.getGlobalProperty("http://www.example.org", "globalTest", false);
    assertNotNull(elemProp);

    String attrValue = phone.getString(attrProp);

    // assertEquals("globalAttributeTest", attrValue);
    String elemValue = phone.getString(elemProp);

    // assertEquals("globalElementTest", elemValue);
  }
  public XMLDocument load(Source source, String locationURI, Object options) throws IOException {
    // get XMLUnmarshaller once - as we may create a new instance if this helper isDirty=true
    XMLUnmarshaller anXMLUnmarshaller = getXmlUnmarshaller(options);
    Object unmarshalledObject = null;
    if (options == null) {
      try {
        unmarshalledObject = anXMLUnmarshaller.unmarshal(source);
      } catch (XMLMarshalException xmlException) {
        handleXMLMarshalException(xmlException);
      }
    } else {
      try {
        DataObject optionsDataObject = (DataObject) options;
        try {
          SDOType theType = (SDOType) optionsDataObject.get(SDOConstants.TYPE_LOAD_OPTION);
          try {
            if (theType != null) {
              unmarshalledObject = anXMLUnmarshaller.unmarshal(source, theType.getImplClass());
            } else {
              unmarshalledObject = anXMLUnmarshaller.unmarshal(source);
            }
          } catch (XMLMarshalException xmlException) {
            handleXMLMarshalException(xmlException);
          }
        } catch (ClassCastException ccException) {
          throw SDOException.typePropertyMustBeAType(ccException);
        }
      } catch (ClassCastException ccException) {
        throw SDOException.optionsMustBeADataObject(
            ccException, SDOConstants.ORACLE_SDO_URL, SDOConstants.XMLHELPER_LOAD_OPTIONS);
      }
    }

    if (unmarshalledObject instanceof XMLRoot) {
      XMLRoot xmlRoot = (XMLRoot) unmarshalledObject;
      XMLDocument xmlDocument =
          createDocument(
              (DataObject) ((XMLRoot) unmarshalledObject).getObject(),
              ((XMLRoot) unmarshalledObject).getNamespaceURI(),
              ((XMLRoot) unmarshalledObject).getLocalName());
      if (xmlRoot.getEncoding() != null) {
        xmlDocument.setEncoding(xmlRoot.getEncoding());
      }
      if (xmlRoot.getXMLVersion() != null) {
        xmlDocument.setXMLVersion(xmlRoot.getXMLVersion());
      }
      xmlDocument.setSchemaLocation(xmlRoot.getSchemaLocation());
      xmlDocument.setNoNamespaceSchemaLocation(xmlRoot.getNoNamespaceSchemaLocation());
      return xmlDocument;
    } else if (unmarshalledObject instanceof DataObject) {
      DataObject unmarshalledDataObject = (DataObject) unmarshalledObject;
      String localName =
          ((SDOType) ((DataObject) unmarshalledObject).getType())
              .getXmlDescriptor()
              .getDefaultRootElement();
      if (localName == null) {
        localName = ((SDOType) ((DataObject) unmarshalledObject).getType()).getXsdLocalName();
      }
      return createDocument(
          unmarshalledDataObject, unmarshalledDataObject.getType().getURI(), localName);
    } else if (unmarshalledObject instanceof XMLDocument) {
      return (XMLDocument) unmarshalledObject;
    }
    return null;
  }
 protected void verifyDocument(XMLDocument xmlDocument) {
   assertNull(xmlDocument.getRootObject().getContainer());
   assertNull(xmlDocument.getRootObject().getContainmentProperty());
 }