Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2009, 2010 Springsite BV (The Netherlands) and others All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - Initial API and implementation </copyright> $Id: EMFModelConverter.java,v 1.23 2011/08/29 05:16:04 mtaal Exp $
 
 
 package org.eclipse.emf.texo.converter;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.emf.ecore.EAttribute;
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
 import  org.eclipse.emf.ecore.util.FeatureMapUtil;
Capable of copying objects and their children and references. This object can be used for subsequent copy actions, internal data structures can/should be cleaned using the clear method in between copy actions. As a default referenced objects are not copied, use the setCopyChildren(boolean) or setCopyReferences(boolean) methods to enable copying of contained and not contained objects. If referenced objects are also copied then these referenced objects are at most copied once, so if multiple objects refer to another object (and that other object is also copied) then that other object is only copied once. The complete set of copied objects (including copied referenced objects) can be obtained through the getAllCopiedObjects() method.

Author(s):
Martin Taal
See also:
ModelObject
 
 public class ObjectCopier implements TexoComponent {
 
   private Map<ObjectObjectsourceTargetMap = new HashMap<ObjectObject>();
 
   private boolean copyChildren = false;
   private boolean copyReferences = false;

  
Copy all objects in the sourceList,

Parameters:
sourceList
Returns:
 
   public List<ObjectcopyAll(List<ObjectsourceList) {
     final List<Objectresult = new ArrayList<Object>();
     for (Object source : sourceList) {
       result.add(copy(source));
     }
     return result;
   }
 
   public Object copy(Object source) {
     if (source == null) {
       return null;
     }
     if (.containsKey(source)) {
       return .get(source);
     }
 
     if (source instanceof DynamicModelObject) {
       final DynamicModelObject target = ComponentProvider.getInstance().newInstance(DynamicModelObject.class);
       target.setData((DynamicModelObjectsource);
       .put(sourcetarget);
       return target;
     }
 
     @SuppressWarnings("unchecked")
     final ModelObject<ObjectsourceModelObject = (ModelObject<Object>) ModelResolver.getInstance().getModelObject(
         source);
     final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(
         sourceModelObject.eClass().getEPackage().getNsURI());
     final Object target = modelPackage.getModelFactory().create(sourceModelObject.eClass());
    @SuppressWarnings("unchecked")
    final ModelObject<ObjecttargetModelObject = (ModelObject<Object>) ModelResolver.getInstance().getModelObject(
        target);
    .put(sourcetarget);
    for (EStructuralFeature eFeature : sourceModelObject.eClass().getEAllStructuralFeatures()) {
      if (eFeature.isVolatile()) {
        continue;
      }
      if (FeatureMapUtil.isFeatureMap(eFeature)) {
        copyFeatureMap(sourceModelObjecttargetModelObjecteFeature);
      } else if (ModelUtils.isEMap(eFeature)) {
        copyMap(sourceModelObjecttargetModelObject, (EReference) eFeature);
      } else if (eFeature.isMany()) {
        if (eFeature instanceof EAttribute) {
          copyManyEAttribute(sourceModelObjecttargetModelObject, (EAttribute) eFeature);
        } else {
          copyManyEReference(sourceModelObjecttargetModelObject, (EReference) eFeature);
        }
      } else if (eFeature instanceof EAttribute) {
        copySingleEAttribute(sourceModelObjecttargetModelObject, (EAttribute) eFeature);
      } else {
        copySingleEReference(sourceModelObjecttargetModelObject, (EReference) eFeature);
      }
    }
    return target;
  }
  protected void copyManyEAttribute(ModelObject<ObjectsourceModelObject<Objecttarget, EAttribute eAttribute) {
    for (Object value : (Collection<?>) source.eGet(eAttribute)) {
      target.eAddTo(eAttributevalue);
    }
  }
  protected void copySingleEAttribute(ModelObject<ObjectsourceModelObject<Objecttarget, EAttribute eAttribute) {
    target.eSet(eAttributesource.eGet(eAttribute));
  }
  protected void copyManyEReference(ModelObject<ObjectsourceModelObject<Objecttarget, EReference eReference) {
    for (Object value : (Collection<?>) source.eGet(eReference)) {
      if (eReference.isContainment() && ) {
        target.eAddTo(eReferencecopy(value));
      } else if (!eReference.isContainment() && ) {
        target.eAddTo(eReferencecopy(value));
      } else {
        target.eAddTo(eReferencevalue);
      }
    }
  }
  protected void copySingleEReference(ModelObject<ObjectsourceModelObject<Objecttarget, EReference eReference) {
    final Object value = source.eGet(eReference);
    if (eReference.isContainment() && ) {
      target.eSet(eReferencecopy(value));
    } else if (!eReference.isContainment() && ) {
      target.eSet(eReferencecopy(value));
    } else {
      target.eSet(eReferencevalue);
    }
  }
  protected void copyMap(ModelObject<ObjectsourceModelObject<Objecttarget, EReference eReference) {
    final Map<?, ?> sourceMap = (Map<?, ?>) source.eGet(eReference);
    @SuppressWarnings("unchecked")
    final Map<ObjectObjecttargetMap = (Map<ObjectObject>) target.eGet(eReference);
    final EClass mapEClass = eReference.getEReferenceType();
    final EStructuralFeature valueFeature = mapEClass.getEStructuralFeature("value"); //$NON-NLS-1$
    final EStructuralFeature keyFeature = mapEClass.getEStructuralFeature("key"); //$NON-NLS-1$
    final boolean copyKey = keyFeature instanceof EReference && copyReference((EReference) keyFeature);
    final boolean copyValue = valueFeature instanceof EReference && copyReference((EReference) valueFeature);
    for (Object key : sourceMap.keySet()) {
      Object value = sourceMap.get(key);
      if (copyKey) {
        key = copy(key);
      }
      if (copyValue) {
        value = copy(value);
      }
      targetMap.put(keyvalue);
    }
  }
  private boolean copyReference(EReference eReference) {
    return  && eReference.isContainment() ||  && !eReference.isContainment();
  }
  protected void copyFeatureMap(ModelObject<ObjectsourceModelObject<Objecttarget, EStructuralFeature eFeature) {
    @SuppressWarnings("unchecked")
    final Collection<ObjecttargetCollection = (Collection<Object>) target.eGet(eFeature);
    for (Object sourceEntry : (Collection<?>) source.eGet(eFeature)) {
      final ModelFeatureMapEntry<?> sourceModelEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeature,
          sourceEntry);
      final Object targetEntry = ModelResolver.getInstance().createFeatureMapEntry(eFeature);
      final ModelFeatureMapEntry<?> targetModelEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeature,
          targetEntry);
      final EStructuralFeature eFMFeature = sourceModelEntry.getEStructuralFeature();
      Object value = sourceModelEntry.getValue();
      if (eFMFeature instanceof EReference && copyReference((EReference) eFMFeature)) {
        value = copy(value);
      }
      targetModelEntry.setEStructuralFeature(eFMFeature);
      targetModelEntry.setValue(value);
      targetCollection.add(targetEntry);
    }
  }
  public void clear() {
  }
    return .values();
  }
  public Map<ObjectObjectgetSourceTargetMap() {
    return ;
  }
  public void setSourceTargetMap(Map<ObjectObjectsourceTargetMap) {
    this. = sourceTargetMap;
  }

  
If true then also children (referenced using an EReference with EReference.isContainment() true) are also copied.
  public boolean isCopyChildren() {
    return ;
  }
  public void setCopyChildren(boolean copyChildren) {
    this. = copyChildren;
  }

  
If true then also non-containment references (referenced using an EReference with EReference.isContainment() false) are also copied.
  public boolean isCopyReferences() {
    return ;
  }
  public void setCopyReferences(boolean copyReferences) {
    this. = copyReferences;
  }
New to GrepCode? Check out our FAQ X