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.Date;
 import java.util.List;
 import java.util.Map;
 
 
 import  org.eclipse.emf.ecore.EAttribute;
 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.impl.DynamicEObjectImpl;
 import  org.eclipse.emf.ecore.util.FeatureMap;
 import  org.eclipse.emf.ecore.util.FeatureMapUtil;
Converts a set of EObject instances to objects managed by ModelPackage instances. Uses an internal map during the conversion process to map already converted EObjects to ModelObjects.

Author(s):
Martin Taal
See also:
ModelObject
DynamicEObjectImpl
 
 public class EMFModelConverter implements TexoComponent {
 
   private Map<EObject, ObjectobjectMapping = new HashMap<EObject, Object>();
 
   private ObjectResolver objectResolver = ComponentProvider.getInstance().newInstance(DefaultObjectResolver.class);
 
   private final List<EObject> toConvert = new ArrayList<EObject>();
 
   // keeps track of all objects which have been converted
   private final List<EObject> allConvertedEObjects = new ArrayList<EObject>();

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

Parameters:
eObjects the eObjects to convert, also the references/children are converted
Returns:
the created model objects
 
   public List<Objectconvert(final List<EObject> eObjects) {
     // 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<Objectresult = new ArrayList<Object>();
     for (final EObject eObject : eObjects) {
       result.add(createTarget(eObject));
     }
     while (!.isEmpty()) {
       final ArrayList<EObject> beingConverted = new ArrayList<EObject>();
       .addAll(beingConverted);
       .clear();
       for (EObject eObject : beingConverted) {
         convertContent(eObject);
       }
     }
 
     return result;
   }

  
Converts a single EObject. Internally a map from EObject to Object is kept so that an instance of an EObject is only converted once and the same Object is returned. If the EObject is a proxy then it is read from the getUriResolver().

Parameters:
eObject the object to convert
Returns:
the created model managed object
  protected Object createTarget(final EObject eObject) {
    Object target = .get(eObject);
    if (target != null) {
      return target;
    }
    target = .resolveFromEObject(eObject);
    if (!eObject.eIsProxy()) {
      .add(eObject);
    }
    .put(eObjecttarget);
    return target;
  }
  protected void convertContent(EObject eObject) {
    final Object target = .get(eObject);
    final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(target);
    for (final EStructuralFeature eStructuralFeature : modelObject.eClass().getEAllStructuralFeatures()) {
      if (!eStructuralFeature.isChangeable() || eStructuralFeature.isVolatile()) {
        continue;
      }
      // not set, not convert
      if (!eStructuralFeature.isMany() && !eObject.eIsSet(eStructuralFeature)) {
        continue;
      }
      if (FeatureMapUtil.isFeatureMap(eStructuralFeature)) {
        convertFeatureMap(eObjectmodelObjecteStructuralFeature);
      } else if (eStructuralFeature.isMany()) {
        if (eStructuralFeature instanceof EAttribute) {
          final EAttribute eAttribute = (EAttribute) eStructuralFeature;
          convertManyEAttribute(eObjectmodelObjecteAttribute);
        } else {
          final EReference eReference = (EReference) eStructuralFeature;
          if (eReference.isContainer()) {
            continue;
          }
          convertManyEReference(eObjectmodelObjecteReference);
        }
      } else {
        if (eStructuralFeature instanceof EAttribute) {
          final EAttribute eAttribute = (EAttribute) eStructuralFeature;
          convertSingleEAttribute(eObjectmodelObjecteAttribute);
        } else {
          final EReference eReference = (EReference) eStructuralFeature;
          if (eReference.isContainer()) {
            continue;
          }
          convertSingleEReference(eObjectmodelObjecteReference);
        }
      }
    }
  }

  
Converts the value of an EReference with isMany==false, the value is converted to a Object and set in the correct feature in the model managed object.

Parameters:
eObject the eObject from which the value is read
modelObject the Object in which the value is to be set
eReference the eReference which is converted
  protected void convertSingleEReference(final EObject eObjectfinal ModelObject<?> modelObject,
      final EReference eReference) {
    // bidirectional one-to-many are always set from the many side to preserve the order
    if (!eReference.isMany() && eReference.getEOpposite() != null && eReference.getEOpposite().isMany()) {
      return;
    }
    final EObject value = (EObject) eObject.eGet(eReference);
    if (value == null) {
      modelObject.eSet(eReferencenull);
    } else {
      modelObject.eSet(eReferencecreateTarget(value));
    }
  }

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

Parameters:
eObject the eObject from which the value is read
modelObject the Object in which the value is set
eReference the eReference which is converted
  protected void convertManyEReference(final EObject eObjectfinal ModelObject<?> modelObject,
      final EReference eReference) {
    @SuppressWarnings("unchecked")
    final Collection<EObject> eValues = (Collection<EObject>) eObject.eGet(eReference);
    if (ModelUtils.isEMap(eReference)) {
      @SuppressWarnings("unchecked")
      final Map<ObjectObjectmValues = (Map<ObjectObject>) modelObject.eGet(eReference);
      // clear as there can be current values if the target is read from the db
      mValues.clear();
      for (final Object eValue : eValues) {
        @SuppressWarnings("unchecked")
        final Map.Entry<ObjectObjectentry = (Map.Entry<ObjectObject>) eValue;
        // key and value can also be an EObject
        final Object key;
        if (entry.getKey() instanceof EObject) {
          key = createTarget((EObject) entry.getKey());
        } else {
          key = entry.getKey();
        }
        final Object value;
        if (entry.getValue() instanceof EObject) {
          value = createTarget((EObject) entry.getValue());
        } else {
          value = entry.getValue();
        }
        mValues.put(keyvalue);
      }
    } else {
      final Collection<?> mValues = (Collection<?>) modelObject.eGet(eReference);
      // clear as there can be current values if the target is read from the db
      // use forloop as the collection can be unmodifiable
      for (Object o : new ArrayList<Object>(mValues)) {
        modelObject.eRemoveFrom(eReferenceo);
      }
      for (final EObject eValue : eValues) {
        final Object target = createTarget(eValue);
        // first add to the many reference
        modelObject.eAddTo(eReferencetarget);
        // add to the other side, this is needed because the bi-directional
        // api is not always generated
        if (eReference.getEOpposite() != null && !eReference.getEOpposite().isMany()) {
          final ModelObject<?> modelObjectTarget = ModelResolver.getInstance().getModelObject(target);
          if (eReference.getEOpposite().isMany()) {
            final Collection<?> oppositeCollection = (Collection<?>) modelObjectTarget.eGet(eReference.getEOpposite());
            if (!oppositeCollection.contains(modelObject.getTarget())) {
              modelObjectTarget.eAddTo(eReference.getEOpposite(), modelObject.getTarget());
            }
          } else if (modelObjectTarget.eGet(eReference.getEOpposite()) != modelObject.getTarget()) {
            modelObjectTarget.eSet(eReference.getEOpposite(), modelObject.getTarget());
          }
        }
      }
    }
  }

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

Parameters:
eObject the eObject from which the value is read
modelObject the @[link ModelObject} in which the value is to be set
eAttribute the EAttribute which is converted
See also:
convertEAttributeValue(Object, EDataType)
  protected void convertSingleEAttribute(final EObject eObjectfinal ModelObject<?> modelObject,
      final EAttribute eAttribute) {
    if (!eObject.eIsSet(eAttribute)) {
      return;
    }
    final Object eValue = eObject.eGet(eAttribute);
    modelObject.eSet(eAttributeconvertEAttributeValue(eValueeAttribute.getEAttributeType()));
  }

  
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 EObject eObjectfinal ModelObject<?> modelObject,
      final EStructuralFeature eFeature) {
    final Collection<?> eValues = (Collection<?>) eObject.eGet(eFeature);
    @SuppressWarnings("unchecked")
    final Collection<Objectvalues = (Collection<Object>) modelObject.eGet(eFeature);
    // clear as the object may have been read from the db
    values.clear();
    for (final Object eValue : eValues) {
      final FeatureMap.Entry eEntry = (FeatureMap.Entry) eValue;
      final Object featureMapEntry = ModelResolver.getInstance().createFeatureMapEntry(eFeature);
      final ModelFeatureMapEntry<?> mEntry = ModelResolver.getInstance().getModelFeatureMapEntry(eFeature,
          featureMapEntry);
      mEntry.setEStructuralFeature(eEntry.getEStructuralFeature());
      final Object convertedValue;
      if (mEntry.getEStructuralFeature() instanceof EAttribute) {
        convertedValue = convertEAttributeValue(eEntry.getValue(),
            ((EAttribute) eEntry.getEStructuralFeature()).getEAttributeType());
      } else {
        convertedValue = createTarget((EObject) eEntry.getValue());
      }
      mEntry.setValue(convertedValue);
      values.add(featureMapEntry);
    }
  }

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

Parameters:
eObject the eObject from which the value is read
modelObject the ModelObject in which the value is to be set
eAttribute the EAttribute which is converted
See also:
convertEAttributeValue(Object, EDataType)
  protected void convertManyEAttribute(final EObject eObjectfinal ModelObject<?> modelObject,
      final EAttribute eAttribute) {
    final Collection<?> eValues = (Collection<?>) eObject.eGet(eAttribute);
    final EDataType eDataType = eAttribute.getEAttributeType();
    @SuppressWarnings("unchecked")
    final Collection<Objectvalues = (Collection<Object>) modelObject.eGet(eAttribute);
    // clear as the target may have been read from the db
    values.clear();
    for (final Object eValue : eValues) {
      values.add(convertEAttributeValue(eValueeDataType));
    }
  }

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

Parameters:
value the value to convert
eDataType its EDataType
Returns:
the converted value
  @SuppressWarnings({ "unchecked""rawtypes" })
  protected Object convertEAttributeValue(final Object valuefinal EDataType eDataType) {
    if (value instanceof Enum<?>) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(
          enumDataType.getEPackage().getNsURI());
      final Class<? extends EnumenumClass = (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType);
      return Enum.valueOf(enumClass, ((Enum<?>) value).name().toUpperCase(.));
    } else if (value instanceof EEnumLiteral) {
      final EDataType enumDataType = getDataTypeOrBaseType(eDataType);
      Check.isInstanceOf(enumDataType, EEnum.class);
      final EEnumLiteral eeNumLiteral = (EEnumLiteral) value;
      final ModelPackage modelPackage = ModelResolver.getInstance().getModelPackage(
          enumDataType.getEPackage().getNsURI());
      if (modelPackage == null) {
        // dynamic model
        return eeNumLiteral;
      }
      final Class<? extends EnumenumClass = (Class<? extends Enum>) modelPackage.getEClassifierClass(enumDataType);
      return Enum.valueOf(enumClasseeNumLiteral.getName().toUpperCase(.));
    }
    // convert these to a Date always
    if (value instanceof XMLGregorianCalendar) {
      final XMLGregorianCalendar xmlCalendar = (XMLGregorianCalendarvalue;
      final Date date = xmlCalendar.toGregorianCalendar().getTime();
      return date;
    }
    return value;
  }

  
See the javadoc in the ModelUtils.getEnumBaseDataTypeIfObject(EDataType) for details.

Parameters:
eDataType
Returns:
the passed EDataType or its base type if the base type is an EEnum
  private EDataType getDataTypeOrBaseType(EDataType eDataType) {
    final EDataType baseType = ModelUtils.getEnumBaseDataTypeIfObject(eDataType);
    if (baseType != null) {
      return baseType;
    }
    return eDataType;
  }
    return ;
  }
  public void setUriResolver(ObjectResolver uriResolver) {
     = uriResolver;
  }
  public List<EObject> getAllConvertedEObjects() {
    return ;
  }
    return .values();
  }
New to GrepCode? Check out our FAQ X