Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2012 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.List;
 import java.util.Map;
 
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EDataType;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
Base class for model to another format converter.

Author(s):
Martin Taal
See also:
ModelObject
 
 public abstract class BaseModelConverter<T extends Objectimplements TexoComponent {
 
   // list of objects that are to be proxied
   private List<T> proxyObjects = new ArrayList<T>();
   private List<T> nonProxiedObjects = new ArrayList<T>();

  
If true then referenced non contained objects are also converted and added to the conversion stack.
 
   private boolean convertNonContainedReferencedObjects = false;

  
How many levels of child objects should be traversed before using proxy objects and stop converting
 
   private int maxChildLevelsToConvert = .;

  
The object resolver responsible for creating uri's and objects.
 
   private ObjectResolver objectResolver = ComponentProvider.getInstance().newInstance(DefaultObjectResolver.class);
 
   protected void doBaseActions(List<T> objects) {
     if ( != null
       computeProxyObjects(objects);
     }
     // make sure that the non proxied objects don't get proxied
   }
 
   protected void computeProxyObjects(final List<T> objects) {
     for (T o : objects) {
     }
   }
 
   @SuppressWarnings("unchecked")
   protected void traverseEReferencesForProxyDetermination(T objectint level) {
 
     if (.contains(object) || .contains(object)) {
       // been here, go away
       return;
     }
 
     // special case, a query can return an array, allows jsonizing this
     // to be send to the client
     if (object.getClass().isArray()) {
       for (int i = 0; i < Array.getLength(object); i++) {
         final Object value = Array.get(objecti);
         if (isModelEnabled(value)) {
           traverseEReferencesForProxyDetermination((T) valuelevel);
         }
       }
       return;
     }
 
     .add(object);
    final boolean proxyChildObjects = level == ;
    for (EReference eReference : eClass(object).getEAllReferences()) {
      if (eReference.isVolatile() || eReference.isTransient()) {
        continue;
      }
      final T value = (T) eGet(objecteReference);
      if (value == null) {
        continue;
      }
      if (eReference.isContainment()) {
        if (!proxyChildObjects) {
          if (value instanceof Collection<?>) {
            for (T o : (Collection<T>) value) {
              traverseEReferencesForProxyDetermination(olevel + 1);
            }
          } else if (value instanceof Map<?, ?>) {
            final Map<?, ?> map = (Map<?, ?>) value;
            for (Object key : map.keySet()) {
              final Object keyValue = map.get(key);
              if (isModelEnabled(key)) {
                traverseEReferencesForProxyDetermination((T) keylevel + 1);
              }
              if (isModelEnabled(keyValue)) {
                traverseEReferencesForProxyDetermination((T) keyValuelevel + 1);
              }
            }
          } else {
            traverseEReferencesForProxyDetermination(valuelevel + 1);
          }
          continue;
        }
      } else if () {
        if (value instanceof Collection<?>) {
          for (T o : (Collection<T>) value) {
            traverseEReferencesForProxyDetermination(olevel);
          }
        } else if (value instanceof Map<?, ?>) {
          final Map<?, ?> map = (Map<?, ?>) value;
          for (Object key : map.keySet()) {
            final Object keyValue = map.get(key);
            if (isModelEnabled(key)) {
              traverseEReferencesForProxyDetermination((T) keylevel + 1);
            }
            if (isModelEnabled(keyValue)) {
              traverseEReferencesForProxyDetermination((T) keyValuelevel + 1);
            }
          }
        } else {
          traverseEReferencesForProxyDetermination(valuelevel);
        }
        continue;
      }
      if (value instanceof Collection<?>) {
        .addAll((Collection<T>) value);
      } else {
        .add(value);
      }
    }
  }
  protected abstract EClass eClass(T target);
  protected abstract Object eGet(T target, EStructuralFeature eFeature);
  // final ModelObject<?> modelObject = ModelResolver.getInstance().getModelObject(object);
  protected abstract boolean isModelEnabled(Object target);
  // ModelResolver.getInstance().isModelEnabled(key)

  
If a non-null value is returned then the content of the modelObject is not converted. The default implementation returns null.

Parameters:
modelObject the modelObject to get the proxy id for
Returns:
the proxy uri, should encode the type of the object as well as its id
  protected org.eclipse.emf.common.util.URI getProxyId(T target) {
    return getObjectResolver().toUri(target);
  }

  
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
  protected EDataType getDataTypeOrBaseType(EDataType eDataType) {
    final EDataType baseType = ModelUtils.getEnumBaseDataTypeIfObject(eDataType);
    if (baseType != null) {
      return baseType;
    }
    return eDataType;
  }
  }
  public void setConvertNonContainedReferencedObjects(boolean convertNonContainedReferencedObjects) {
    this. = convertNonContainedReferencedObjects;
  }
  public int getMaxChildLevelsToConvert() {
  }
  public void setMaxChildLevelsToConvert(int maxChildLevelsToConvert) {
    this. = maxChildLevelsToConvert;
  }
    return ;
  }
  public void setObjectResolver(ObjectResolver uriResolver) {
     = uriResolver;
  }
  public List<T> getProxyObjects() {
    return ;
  }
  public void setProxyObjects(List<T> proxyObjects) {
    this. = proxyObjects;
  }
  public List<T> getNonProxiedObjects() {
    return ;
  }
  public void setNonProxiedObjects(List<T> nonProxiedObjects) {
    this. = nonProxiedObjects;
  }
New to GrepCode? Check out our FAQ X