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: ModelEMFConverter.java,v 1.27 2011/09/14 15:35:53 mtaal Exp $
 
 
 package org.eclipse.emf.texo.converter;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.emf.common.util.EList;
 import  org.eclipse.emf.common.util.URI;
 import  org.eclipse.emf.ecore.EAttribute;
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EDataType;
 import  org.eclipse.emf.ecore.EEnum;
 import  org.eclipse.emf.ecore.EEnumLiteral;
 import  org.eclipse.emf.ecore.EObject;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
 import  org.eclipse.emf.ecore.InternalEObject;
 import  org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
 import  org.eclipse.emf.ecore.util.EcoreUtil;
 import  org.eclipse.emf.ecore.util.FeatureMap;
 import  org.eclipse.emf.ecore.util.FeatureMapUtil;
 import  org.eclipse.emf.ecore.xml.type.XMLTypePackage;
 import  org.eclipse.emf.ecore.xml.type.internal.XMLCalendar;
Converts a set of model objects to a set of DynamicEObjectImpl instances. Internally a map from ModelObject to EObject is maintained so that an object is at most converted once. Note: keeps state, to prevent side effects always create new converters for new conversion actions.

Author(s):
Martin Taal
See also:
ModelObject
DynamicEObjectImpl
 
 public class ModelEMFConverter extends BaseModelConverter<Object> {
 
   private Object lastObject = null;
   private ModelObject<?> lastModelObject = null;
 
   private Map<Object, InternalEObject> objectMapping = new HashMap<Object, InternalEObject>();
 
   // list of objects for which the content needs to be converted
   private List<ObjecttoConvert = new ArrayList<Object>();
 
   // list of objects which have been converted
   private List<Objectconverted = new ArrayList<Object>();
 
   // keeps track of all many-to-many associations, for these the order of
   // elements has to be repaired
   private List<ManyToManytoRepairManyToMany = new ArrayList<ManyToMany>();

  
Converts a set of model managed objects and all the objects they reference to a collection of EObjects.

Parameters:
objects the model objects to convert, also the references/children are converted
Returns:
the created EObjects
 
   public List<EObject> convert(final List<Objectobjects) {
     doBaseActions(objects);
 
     // the process creates the new target objects and then converts the content
     // this multi-step process prevents stack overflow with large object graphs
     final List<EObject> result = new ArrayList<EObject>();
     Check.isNotNullArgument(objects"objects"); //$NON-NLS-1$
     for (final Object object : objects) {
       result.add(createTarget(object));
     }
 
    while (!.isEmpty()) {
      final ArrayList<ObjectbeingConverted = new ArrayList<Object>();
      .clear();
      for (Object object : beingConverted) {
        convertContent(object);
        .add(object);
      }
    }
    // for (ManyToMany mtm : toRepairManyToMany) {
    // final ModelObject<?> modelObject = mtm.getOwner();
    // final EObject eObject = objectMapping.get(modelObject.getTarget());
    // for (EReference eReference : eObject.eClass().getEAllReferences()) {
    // if (eReference.isMany()) {
    // final Collection<?> coll1 = (Collection<?>) modelObject.eGet(eReference);
    // final Collection<?> coll2 = (Collection<?>) eObject.eGet(eReference);
    // if (coll1.size() != coll2.size()) {
    //            throw new IllegalStateException("Unequal sizes of collection for EReference " //$NON-NLS-1$
    // + eReference);
    // }
    // }
    // }
    // }
    // now repair the ManyToMany
    for (ManyToMany mtm : ) {
      mtm.repair();
    }
    return result;
  }

  
Converts a single model managed object.

Parameters:
modelObject the object to convert
Returns:
the created EObject
  protected EObject createTarget(final Object target) {
    InternalEObject eObject = .get(target);
    if (eObject == null) {
      // not found, create it and add a new entry to the mapping
      eObject = (InternalEObject) getObjectResolver().resolveToEObject(target);
      .put(targeteObject);
    }
    if (!.contains(target)) {
      .add(target);
    }
    return eObject;
  }
  protected void convertContent(Object target) {
    // if a proxy then do no feature conversions as this may load
    // the object
    InternalEObject eObject = .get(target);
    final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(target);
    if (getProxyObjects().contains(target)) {
      final URI proxyURI = getProxyId(modelObject);
      if (proxyURI != null) {
        eObject.eSetProxyURI(proxyURI);
      }
      return;
    }
    for (final EStructuralFeature eStructuralFeature : eObject.eClass().getEAllStructuralFeatures()) {
      if (!eStructuralFeature.isChangeable() || eStructuralFeature.isVolatile()) {
        continue;
      }
      if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) {
        convertFeatureMap(modelObjecteObjecteStructuralFeature);
      } else if (eStructuralFeature.isMany()) {
        if (eStructuralFeature instanceof EAttribute) {
          final EAttribute eAttribute = (EAttribute) eStructuralFeature;
          convertManyEAttribute(modelObjecteObjecteAttribute);
        } else {
          final EReference eReference = (EReference) eStructuralFeature;
          convertManyEReference(modelObjecteObjecteReference);
        }
      } else {
        if (eStructuralFeature instanceof EAttribute) {
          final EAttribute eAttribute = (EAttribute) eStructuralFeature;
          convertSingleEAttribute(modelObjecteObjecteAttribute);
        } else {
          final EReference eReference = (EReference) eStructuralFeature;
          convertSingleEReference(modelObjecteObjecteReference);
        }
      }
    }
  }

  
Converts the values of an FeatureMap, the values of the collection are converted to and added to the list in the correct feature in the modelObject.

Parameters:
eObject the eObject from which the value is read
modelObject the ModelObject in which the value is to be set
eFeature the eFeature which is converted
  protected void convertFeatureMap(final ModelObject<?> modelObjectfinal EObject eObject,
      final EStructuralFeature eFeature) {
    final Collection<?> mValues = (Collection<?>) modelObject.eGet(eFeature);
    @SuppressWarnings("unchecked")
    final Collection<Objectvalues = (Collection<Object>) eObject.eGet(eFeature);
    for (final Object mValue : mValues) {
      final ModelFeatureMapEntry<?> mEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeaturemValue);
      EStructuralFeature entryFeature = mEntry.getEStructuralFeature();
      Object entryValue = mEntry.getValue();
      // flatten the tree of feature map entries, feature maps maybe nested
      // EMF uses a flattened api, while Texo builds this actual tree of
      // featuremaps, the findFeature and findValue find the deepest featureMap entry
      if (FeatureMapUtil.isFeatureMap(entryFeature)) {
        final ModelFeatureMapEntry<?> modelFeatureMapEntry = ModelResolver.getInstance().getModelFeatureMapEntry(
            entryFeatureentryValue);
        entryFeature = ModelUtils.findFeature(modelFeatureMapEntry);
        entryValue = ModelUtils.findValue(modelFeatureMapEntry);
      }
      final Object convertedValue;
      if (entryFeature instanceof EAttribute) {
        convertedValue = convertEAttributeValue(entryValue, ((EAttribute) entryFeature).getEAttributeType());
      } else {
        convertedValue = createTarget(entryValue);
      }
      final FeatureMap.Entry eEntry = FeatureMapUtil.createEntry(entryFeatureconvertedValue);
      values.add(eEntry);
    }
  }

  
Converts the value of an EReference with isMany==false, the value is converted to an EObject and set in the correct feature in the eObject.

Parameters:
modelObject the modelObject from which the value is retrieved.
eObject the eObject in which the value is set (after it has been converted)
eReference the eReference which is converted
  protected void convertSingleEReference(final ModelObject<?> modelObjectfinal EObject eObject,
      final EReference eReference) {
    // containment/container features are always set from the
    // containment side
    if (eReference.isContainer()) {
      return;
    }
    if (eObject.eIsSet(eReference)) {
      return;
    }
    final Object value = modelObject.eGet(eReference);
    if (value == null) {
      eObject.eSet(eReferencenull);
    } else {
      final InternalEObject eValue = (InternalEObject) createTarget(value);
      eObject.eSet(eReferenceeValue);
    }
  }

  
Converts the value of an EReference with isMany==true, the values of the collection are converted to EObjects and added to the list in the correct feature in the eObject.

Parameters:
modelObject the modelObject from which the value is retrieved.
eObject the eObject in which the value is set (after it has been converted)
eReference the eReference which is converted
  protected void convertManyEReference(final ModelObject<?> modelObjectfinal EObject eObject,
      final EReference eReference) {
    // container feature is always set from the other side, the containment
    // side
    if (eReference.isContainer()) {
      return;
    }
    final Object manyValue = modelObject.eGet(eReference);
    final Collection<EObject> newValues = new ArrayList<EObject>();
    boolean isMap = Map.class.isAssignableFrom(manyValue.getClass());
    EStructuralFeature valueFeature = null;
    EStructuralFeature keyFeature = null;
    if (isMap) {
      final EClass mapEClass = eReference.getEReferenceType();
      valueFeature = mapEClass.getEStructuralFeature("value"); //$NON-NLS-1$
      keyFeature = mapEClass.getEStructuralFeature("key"); //$NON-NLS-1$
      Check.isTrue(ModelUtils.isEMap(eReference), "Expected emap EReference, but th// case for EReference " //$NON-NLS-1$
          + eReference);
      final Map<?, ?> map = (Map<?, ?>) manyValue;
      for (final Object key : map.keySet()) {
        final Object value = map.get(key);
        final EObject mapEntryEObject = EcoreUtil.create(mapEClass);
        // key and value maybe primitive types but can also be
        // references to model objects.
        if (valueFeature instanceof EReference) {
          mapEntryEObject.eSet(valueFeaturecreateTarget(value));
        } else {
          mapEntryEObject.eSet(valueFeaturevalue);
        }
        if (keyFeature instanceof EReference) {
          mapEntryEObject.eSet(keyFeaturecreateTarget(key));
        } else {
          mapEntryEObject.eSet(keyFeaturekey);
        }
        newValues.add(mapEntryEObject);
      }
    } else {
      // a many to many
      if (eReference.getEOpposite() != null && eReference.getEOpposite().isMany()) {
        final ManyToMany mtm = new ManyToMany();
        mtm.setOwner(modelObject);
        mtm.setEReference(eReference);
        .add(mtm);
      }
      @SuppressWarnings("unchecked")
      final Collection<Objectvalues = (Collection<Object>) manyValue;
      for (final Object value : values) {
        if (value == null) {
          newValues.add(null);
        } else {
          final InternalEObject eValue = (InternalEObject) createTarget(value);
          if (!newValues.contains(eValue)) {
            newValues.add(eValue);
          }
        }
      }
    }
    @SuppressWarnings("unchecked")
    final Collection<EObject> eValues = (Collection<EObject>) eObject.eGet(eReference);
    boolean updateList = false;
    if (newValues.size() == eValues.size()) {
      final Iterator<?> it = eValues.iterator();
      for (Object newValue : newValues) {
        final Object oldValue = it.next();
        if (isMap) {
          final EObject oldMapEntry = (EObject) oldValue;
          final EObject newMapEntry = (EObject) newValue;
          final Object oldMapValue = oldMapEntry.eGet(valueFeature);
          final Object oldMapKey = oldMapEntry.eGet(keyFeature);
          final Object newMapValue = newMapEntry.eGet(valueFeature);
          final Object newMapKey = newMapEntry.eGet(keyFeature);
          if (valueFeature instanceof EReference) {
            updateList = oldMapValue == newMapValue;
          } else {
            updateList = oldMapValue != null ? !oldMapValue.equals(newMapValue) : newMapValue != null ? !newMapValue
                .equals(oldMapValue) : false;
          }
          if (keyFeature instanceof EReference) {
            updateList = updateList || oldMapKey == newMapKey;
          } else {
            updateList = updateList
                || (oldMapKey != null ? !oldMapKey.equals(newMapKey) : newMapKey != null ? !newMapKey.equals(oldMapKey)
                    : false);
          }
        } else {
          updateList = oldValue != newValue;
        }
        if (updateList) {
          break;
        }
      }
    } else {
      updateList = true;
    }
    if (updateList) {
      // clear the evalues so that an empty tag is created in the xml
      eValues.clear();
      eValues.addAll(newValues);
    }
  }

  
Converts the value of an EAttribute with isMany==false, the value is converted ( convertEAttributeValue(Object, EDataType)) and set in the correct feature in the eObject.

Parameters:
modelObject the modelObject from which the value is retrieved.
eObject the eObject in which the value is set (after it has been converted)
eAttribute the EAttribute which is converted
See also:
convertEAttributeValue(Object, EDataType)
  protected void convertSingleEAttribute(final ModelObject<?> modelObjectfinal EObject eObject,
      final EAttribute eAttribute) {
    final Object value = modelObject.eGet(eAttribute);
    // don't set the eObject if the value is null and the attribute is unsettable.
    // unsettable is modelled with null in Texo.
    if (value == null && ModelUtils.isUnsettable(eAttribute)) {
      eObject.eUnset(eAttribute);
      return;
    }
    final Object newValue = convertEAttributeValue(valueeAttribute.getEAttributeType());
    ((InternalEObject) eObject).eSet(eAttributenewValue);
  }

  
Converts the value of an EAttribute with isMany==true, the values of the collection are converted and added to the list in the correct feature in the eObject.

Parameters:
modelObject the modelObject from which the value is retrieved.
eObject the eObject in which the value is set (after it has been converted)
eAttribute the EAttribute which is converted
See also:
convertEAttributeValue(Object, EDataType)
  protected void convertManyEAttribute(final ModelObject<?> modelObjectfinal EObject eObject,
      final EAttribute eAttribute) {
    final Collection<?> values = (Collection<?>) modelObject.eGet(eAttribute);
    final EDataType eDataType = eAttribute.getEAttributeType();
    @SuppressWarnings("unchecked")
    final List<ObjecteValues = (List<Object>) eObject.eGet(eAttribute);
    final List<ObjectnewValues = new ArrayList<Object>();
    for (final Object value : values) {
      newValues.add(convertEAttributeValue(valueeDataType));
    }
    boolean updateList = false;
    if (values.size() == eValues.size()) {
      final Iterator<?> it = eValues.iterator();
      for (Object newValue : newValues) {
        final Object oldValue = it.next();
        if (newValue != null && oldValue == null) {
          updateList = true;
        } else if (oldValue == null && newValue != null) {
          updateList = true;
        } else if (oldValue != null && newValue != null) {
          updateList = !oldValue.equals(newValue);
        }
        if (updateList) {
          break;
        }
      }
    } else {
      updateList = true;
    }
    if (updateList) {
      // clear the evalues so that an empty tag is created in the xml
      eValues.clear();
      eValues.addAll(newValues);
    }
  }

  
Converts a primitive type value. For EEnum: this implementation only converts an Enum to an EEnum value.

Parameters:
value the value to convert
eDataType its EDataType
Returns:
the converted value
  protected Object convertEAttributeValue(final Object valuefinal EDataType eDataType) {
    if (value instanceof Enum<?>) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final EEnum eeNum = (EEnum) enumDataType;
      for (final EEnumLiteral enumLiteral : eeNum.getELiterals()) {
        // the code generation template uppercases enum
        if (enumLiteral.getName().toUpperCase(.).equals(((Enum<?>) value).name())) {
          return enumLiteral.getInstance();
        }
      }
    }
    if (value instanceof Date && eDataType == XMLTypePackage.eINSTANCE.getDate()) {
      final Date date = (Datevalue;
      final XMLCalendar xmlCalendar = new XMLCalendar(date, XMLCalendar.DATE);
      final Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      xmlCalendar.clear();
      xmlCalendar.setYear(calendar.get(.));
      xmlCalendar.setMonth(1 + calendar.get(.));
      xmlCalendar.setDay(calendar.get(.));
      // note xmlcalendar expects minutes, calendar gives millis
      xmlCalendar.setTimezone(calendar.get(.) / 60000);
      return xmlCalendar;
    }
    if (value instanceof Date && eDataType == XMLTypePackage.eINSTANCE.getDateTime()) {
      final Date date = (Datevalue;
      return new XMLCalendar(date, XMLCalendar.DATETIME);
    }
    return value;
  }

  
Class is used to keep track of all the many to many for which the order has to be repaired.

Author(s):
mtaal
  private class ManyToMany {
    private ModelObject<?> owner;
    private EReference eReference;
    public void repair() {
      final EObject eObjectOwner = .get(.getTarget());
      final ModelObject<?> modelObjectOwner = ModelResolver.getInstance().getModelObject();
      final Object listObject = modelObjectOwner.eGet();
      if (!(listObject instanceof List<?>)) {
        // no order maintained anyway
        return;
      }
      final EList<?> eList = (EList<?>) eObjectOwner.eGet();
      final List<?> list = (List<?>) listObject;
      int currentIndex = 0;
      for (Object objectElement : list) {
        final EObject eObjectElement = .get(objectElement);
        final int newIndex = eList.indexOf(eObjectElement);
        if (newIndex != currentIndex) {
          eList.move(currentIndexnewIndex);
        }
        currentIndex++;
      }
    }
    public void setOwner(ModelObject<?> owner) {
      this. = owner;
    }
    public void setEReference(EReference eReference) {
      this. = eReference;
    }
  }
  public Map<Object, InternalEObject> getObjectMapping() {
    return ;
  }
  public void setObjectMapping(Map<Object, InternalEObject> objectMapping) {
    this. = objectMapping;
  }
  public List<ObjectgetConverted() {
    return ;
  }
  public void setConverted(List<Objectconverted) {
    this. = converted;
  }
  private ModelObject<?> getModelObject(Object target) {
    if (target == ) {
      return ;
    }
     = target;
     = ModelResolver.getInstance().getModelObject(target);
    return ;
  }
  protected EClass eClass(Object target) {
    return getModelObject(target).eClass();
  }
  protected Object eGet(Object target, EStructuralFeature eFeature) {
    return getModelObject(target).eGet(eFeature);
  }
  protected boolean isModelEnabled(Object target) {
    return ModelResolver.getInstance().isModelEnabled(target);
  }
New to GrepCode? Check out our FAQ X