## org.eclipse.emf.compare.util
Class ModelUtils

    java.lang.Object
      org.eclipse.emf.compare.util.ModelUtils

    public final class ModelUtils

extends java.lang.Object

Utility class for model loading/saving and serialization.

Method Summary
`static org.eclipse.emf.ecore.resource.Resource` `attachResource(org.eclipse.emf.common.util.URI resourceURI, org.eclipse.emf.ecore.EObject root)` Attaches the given `EObject` to a new resource created in a new `ResourceSet` with the given URI.
`static org.eclipse.emf.ecore.resource.Resource` `attachResource(org.eclipse.emf.common.util.URI resourceURI, org.eclipse.emf.ecore.resource.ResourceSet resourceSet, org.eclipse.emf.ecore.EObject root)` Attaches the given `EObject` to a new resource created in the given `ResourceSet` with the given URI.
`static boolean` `contains(org.eclipse.emf.ecore.resource.Resource resource, org.eclipse.emf.ecore.EObject eObject)` Checks whether the given resource contains the given object by searching its complete contents ( `Resource.getAllContents()`).
`static org.eclipse.emf.ecore.resource.Resource` `createResource(org.eclipse.emf.common.util.URI modelURI)` This will create a `Resource` given the model extension it is intended for.
`static org.eclipse.emf.ecore.resource.Resource` `createResource(org.eclipse.emf.common.util.URI modelURI, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` This will create a `Resource` given the model extension it is intended for and a ResourceSet.
`static java.lang.String` `getCommonExtension(org.eclipse.emf.common.util.URI... uris)` This will try and find the common file extension for the compared models.
`static java.lang.String` `getCommonNamespace(org.eclipse.emf.ecore.resource.Resource... resources)` This will try to find the common namespace of the given resources.
`static java.util.List<org.eclipse.emf.ecore.EObject>` `getModelsFrom(java.io.File directory, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Loads the models contained by the given directory in the given ResourceSet.
`static java.util.List<org.eclipse.emf.ecore.EObject>` `getModelsFrom(java.io.File directory, java.lang.String extension, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Loads the files with the given extension contained by the given directory as EObjects in the given ResourceSet.
`static org.eclipse.emf.ecore.EObject` `load(java.io.File file, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Loads a model from a `File` in a given `ResourceSet`.
`static org.eclipse.emf.ecore.EObject` `load(java.io.InputStream stream, java.lang.String fileName, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Load a model from an `InputStream` in a given `ResourceSet`.
`static org.eclipse.emf.ecore.EObject` `load(java.lang.String path, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Loads a model from the String representing the location of a model.
`static org.eclipse.emf.ecore.EObject` `load(org.eclipse.emf.common.util.URI modelURI, org.eclipse.emf.ecore.resource.ResourceSet resourceSet)` Loads a model from an `URI` in a given `ResourceSet`.
`static void` `save(org.eclipse.emf.ecore.EObject root, java.lang.String path)` Saves a model as a file to the given path.
`static java.lang.String` `serialize(org.eclipse.emf.ecore.EObject root)` Serializes the given EObjet as a String.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### attachResource

    public static org.eclipse.emf.ecore.resource.Resource attachResource(org.eclipse.emf.common.util.URI resourceURI,
                                                                         org.eclipse.emf.ecore.EObject root)

  Attaches the given `EObject` to a new resource created in a new `ResourceSet` with the given URI.

  Parameters:
    `resourceURI` - URI of the new resource to create.
    `root` - EObject to attach to a new resource.
  Returns:
    The resource `root` has been attached to.

### attachResource

    public static org.eclipse.emf.ecore.resource.Resource attachResource(org.eclipse.emf.common.util.URI resourceURI,
                                                                         org.eclipse.emf.ecore.resource.ResourceSet resourceSet,
                                                                         org.eclipse.emf.ecore.EObject root)

  Attaches the given `EObject` to a new resource created in the given `ResourceSet` with the given URI.

  Parameters:
    `resourceURI` - URI of the new resource to create.
    `resourceSet` - ResourceSet in which to create the resource.
    `root` - EObject to attach to a new resource.
  Returns:
    The resource `root` has been attached to.

### createResource

    public static org.eclipse.emf.ecore.resource.Resource createResource(org.eclipse.emf.common.util.URI modelURI)

  This will create a `Resource` given the model extension it is intended for.

  Parameters:
    `modelURI` - `URI` where the model is stored.
  Returns:
    The `Resource` given the model extension it is intended for.

### createResource

    public static org.eclipse.emf.ecore.resource.Resource createResource(org.eclipse.emf.common.util.URI modelURI,
                                                                         org.eclipse.emf.ecore.resource.ResourceSet resourceSet)

  This will create a `Resource` given the model extension it is intended for and a ResourceSet.

  Parameters:
    `modelURI` - `URI` where the model is stored.
    `resourceSet` - The `ResourceSet` to load the model in.
  Returns:
    The `Resource` given the model extension it is intended for.

### getCommonExtension

    public static java.lang.String getCommonExtension(org.eclipse.emf.common.util.URI... uris)

  This will try and find the common file extension for the compared models.

  Parameters:
    `uris` - The resource URIs that will be compared.
  Returns:
    The file extension to consider when searching for a match engine or `null` if file extensions are distinct.

### getCommonNamespace

    public static java.lang.String getCommonNamespace(org.eclipse.emf.ecore.resource.Resource... resources)

  This will try to find the common namespace of the given resources.

  Parameters:
    `resources` - The resources that will be compared.
  Returns:
    The namespace to consider when searching for a match engine or `null` if namespaces are distinct.
  Since:
    1.1

### getModelsFrom

    public static java.util.List<org.eclipse.emf.ecore.EObject> getModelsFrom(java.io.File directory,
                                                                              org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                                                       throws java.io.IOException

  Loads the models contained by the given directory in the given ResourceSet.

  If `resourceSet` is `null`, all models will be loaded in a new resourceSet.

  Parameters:
    `directory` - The directory from which to load the models.
    `resourceSet` - The `ResourceSet` to load the model in. If `null`, all models will be loaded in a new resourceSet.
  Returns:
    The models contained by the given directory.
  Throws:
    `java.io.IOException` - Thrown if an I/O operation has failed or been interrupted.

### getModelsFrom

    public static java.util.List<org.eclipse.emf.ecore.EObject> getModelsFrom(java.io.File directory,
                                                                              java.lang.String extension,
                                                                              org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                                                       throws java.io.IOException

  Loads the files with the given extension contained by the given directory as EObjects in the given ResourceSet.

  If `resourceSet` is `null`, all models will be loaded in a new resourceSet.

  The argument `extension` is in fact the needed suffix for its name in order for a file to be loaded. If it is equal to "rd", a file named "model.aird" will be loaded, but so would be a file named "Shepherd".

  The empty String or `null` will result in all the files of the given directory to be loaded, and would then be equivalent to `#getModelsFrom(File)`.

  Parameters:
    `directory` - The directory from which to load the models.
    `extension` - File extension of the files to load. If `null`, will consider all extensions.
    `resourceSet` - The `ResourceSet` to load the model in. If `null`, all models will be loaded in a new resourceSet.
  Returns:
    The models contained by the given directory.
  Throws:
    `java.io.IOException` - Thrown if an I/O operation has failed or been interrupted.

### load

    public static org.eclipse.emf.ecore.EObject load(java.io.File file,
                                                     org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                              throws java.io.IOException

  Loads a model from a `File` in a given `ResourceSet`.

  This will return the first root of the loaded model, other roots can be accessed via the resource's content.

  Parameters:
    `file` - `File` containing the model to be loaded.
    `resourceSet` - The `ResourceSet` to load the model in.
  Returns:
    The model loaded from the file.
  Throws:
    `java.io.IOException` - If the given file does not exist.

### load

    public static org.eclipse.emf.ecore.EObject load(java.io.InputStream stream,
                                                     java.lang.String fileName,
                                                     org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                              throws java.io.IOException

  Load a model from an `InputStream` in a given `ResourceSet`.

  This will return the first root of the loaded model, other roots can be accessed via the resource's content.

  Parameters:
    `stream` - The inputstream to load from
    `fileName` - The original filename
    `resourceSet` - The `ResourceSet` to load the model in.
  Returns:
    The loaded model
  Throws:
    `java.io.IOException` - If the given file does not exist.

### load

    public static org.eclipse.emf.ecore.EObject load(java.lang.String path,
                                                     org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                              throws java.io.IOException

  Loads a model from the String representing the location of a model.

  This can be called with pathes of the form

  - `/pluginID/path`
  - `platform:/plugin/pluginID/path`
  - `platform:/resource/pluginID/path`

  This will return the first root of the loaded model, other roots can be accessed via the resource's content.

  Parameters:
    `path` - Location of the model.
    `resourceSet` - The `ResourceSet` to load the model in.
  Returns:
    The model loaded from the path.
  Throws:
    `java.io.IOException` - If the path doesn't resolve to a reachable location.

### load

    public static org.eclipse.emf.ecore.EObject load(org.eclipse.emf.common.util.URI modelURI,
                                                     org.eclipse.emf.ecore.resource.ResourceSet resourceSet)
                                              throws java.io.IOException

  Loads a model from an `URI` in a given `ResourceSet`.

  This will return the first root of the loaded model, other roots can be accessed via the resource's content.

  Parameters:
    `modelURI` - `URI` where the model is stored.
    `resourceSet` - The `ResourceSet` to load the model in.
  Returns:
    The model loaded from the URI.
  Throws:
    `java.io.IOException` - If the given file does not exist.

### save

    public static void save(org.eclipse.emf.ecore.EObject root,
                            java.lang.String path)
                     throws java.io.IOException

  Saves a model as a file to the given path.

  Parameters:
    `root` - Root of the objects to be serialized in a file.
    `path` - File where the objects have to be saved.
  Throws:
    `java.io.IOException` - Thrown if an I/O operation has failed or been interrupted during the saving process.

### serialize

    public static java.lang.String serialize(org.eclipse.emf.ecore.EObject root)
                                      throws java.io.IOException

  Serializes the given EObjet as a String.

  Parameters:
    `root` - Root of the objects to be serialized.
  Returns:
    The given EObjet serialized as a String.
  Throws:
    `java.io.IOException` - Thrown if an I/O operation has failed or been interrupted during the saving process.

### contains

    public static boolean contains(org.eclipse.emf.ecore.resource.Resource resource,
                                   org.eclipse.emf.ecore.EObject eObject)

  Checks whether the given resource contains the given object by searching its complete contents ( `Resource.getAllContents()`).

  Parameters:
    `resource` - The resource whose contents is to be processed.
    `eObject` - The object to be evaluated.
  Returns:
    `true` if the resource contains the eObject, `false` otherwise.

Copyright 2006 IBM Corporation and others. All Rights Reserved.