protected void save()

in impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java [176:234]


  protected void save(OutputStream outputStream, Document document, Object options) throws IOException
  {
    EObject oldContainer = null;
    Resource oldResource = null;
    EReference oldContainmentReference = null;
    int oldContainmentIndex = -1;

    if (documentRoot != null)
    {
      oldContainer = rootObject.eContainer();
      if (oldContainer != null)
        oldContainmentReference = rootObject.eContainmentFeature();
      else
        oldResource = rootObject.eResource();
      if (oldContainer != documentRoot || oldContainmentReference != rootElement)
      {
        if (oldResource != null)
        {
          oldContainmentIndex = oldResource.getContents().indexOf(rootObject);
          oldResource.getContents().remove(oldContainmentIndex);
        }
        else if (oldContainmentReference != null && FeatureMapUtil.isMany(oldContainer, oldContainmentReference))
          oldContainmentIndex = ((List)oldContainer.eGet(oldContainmentReference)).indexOf(rootObject);
        
        Object rootValue =
          rootElement instanceof EAttribute && rootObject instanceof SimpleAnyTypeDataObject ?
            ((SimpleAnyTypeDataObject)rootObject).getValue() : rootObject;
            
        documentRoot.eSet(rootElement, rootValue);
      }
    }

    if (outputStream != null)
      resource.save(outputStream, (Map)options);
    else // if (document != null)
      resource.save(document, (Map)options, null);

    if (oldResource != null)
    {
      oldResource.getContents().add(oldContainmentIndex, rootObject);
    }
    if (rootElement instanceof EReference)
    {
      if (oldContainer != null)
      {
        if (oldContainer != documentRoot || oldContainmentReference != rootElement)
        {
          if (FeatureMapUtil.isMany(oldContainer, oldContainmentReference))
            ((List)oldContainer.eGet(oldContainmentReference)).add(oldContainmentIndex, rootObject);
          else
            oldContainer.eSet(oldContainmentReference, rootObject);
        }
      }
      else if (documentRoot != null)
      {
        documentRoot.eSet(rootElement, null);
      }
    }
  }