Java Code Examples for org.eclipse.emf.ecore.EPackage#eResource()

The following examples show how to use org.eclipse.emf.ecore.EPackage#eResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FragmentFakingEcoreResource.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private void calculateURIFragment(EPackage ePackage, StringBuilder result, Set<EPackage> visited) {
	if (!visited.add(ePackage)) {
		throw new IllegalStateException();
	}
	if (ePackage.getESuperPackage() != null) {
		if (ePackage.eResource() == ePackage.getESuperPackage().eResource()) {
			calculateURIFragment(ePackage.getESuperPackage(), result, visited);
			if (!ePackage.getEClassifiers().isEmpty()) {
				if (result.length() != 0)
					result.append(ePackage.getName()).append('/');
				else
					result.append("//");
			}
		} else {
			result.append("//");
		}
	} else {
		result.append("//");
	}
}
 
Example 2
Source File: EClasses.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Find the {@link EReference} for a given {@link URI}.
 *
 * @param uri
 *          of the EReference to find.
 * @return the EReference, if found, or {@code null} otherwise.
 */
public static EReference getEReference(final URI uri) {
  EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
  if (ePackage == null) {
    return null;
  }
  if (ePackage.eResource() != null) {
    EObject eObject = ePackage.eResource().getEObject(uri.fragment());
    return eObject instanceof EReference ? (EReference) eObject : null; // NOPMD Null assignment
  }
  for (EClassifier eClassifier : ePackage.getEClassifiers()) {
    if (eClassifier instanceof EClass) {
      for (EReference ref : ((EClass) eClassifier).getEReferences()) {
        if (EcoreUtil.getURI(ref).equals(uri)) {
          return ref;
        }
      }
    }
  }
  return null;
}
 
Example 3
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void moveSubpackagesToNewResource(final EPackage pack, final ResourceSet set) {
  for (int i = (pack.getESubpackages().size() - 1); (i >= 0); i--) {
    {
      final EPackage sub = pack.getESubpackages().get(i);
      Resource _eResource = sub.eResource();
      Resource _eResource_1 = pack.eResource();
      boolean _tripleEquals = (_eResource == _eResource_1);
      if (_tripleEquals) {
        boolean _isEmpty = sub.getEClassifiers().isEmpty();
        if (_isEmpty) {
          this.moveSubpackagesToNewResource(sub, set);
        } else {
          this.movePackageToNewResource(sub, set);
          pack.getESubpackages().remove(i);
        }
      }
    }
  }
}
 
Example 4
Source File: FragmentFakingEcoreResource.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void calculateURIFragment(EPackage ePackage, StringBuilder result, Set<EPackage> visited) {
	if (!visited.add(ePackage)) {
		throw new IllegalStateException();
	}
	if (ePackage.getESuperPackage() != null) {
		if (ePackage.eResource() == ePackage.getESuperPackage().eResource()) {
			calculateURIFragment(ePackage.getESuperPackage(), result, visited);
			if (!ePackage.getEClassifiers().isEmpty()) {
				if (result.length() != 0) {
					result.append(ePackage.getName()).append("/");
				} else {
					result.append("//");
				}
			}
		} else {
			result.append("//");
		}
	} else {
		result.append("//");
	}
}
 
Example 5
Source File: XtextReferableElementsUnloader.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void caseEPackage(EPackage ePackage) {
	// guard against infinite recursion
	// EPackage.eSetProxyURI and friends tries to be smart thus
	// we have to make sure to compute all URIs before they are
	// set
	Resource resource = ePackage.eResource();
	URI resourceURI = resource.getURI();
	List<EClassifier> classifiers = ePackage.getEClassifiers();
	List<URI> uris = new ArrayList<URI>(classifiers.size());
	for(int i = 0, size = classifiers.size(); i < size; i++) {
		uris.add(resourceURI.appendFragment(resource.getURIFragment(classifiers.get(i))));
	}
	// and we have to set them in a proper order
	unload(ePackage);
	for(int i = 0, size = classifiers.size(); i < size; i++) {
		InternalEObject classifier = (InternalEObject) classifiers.get(i);
		classifier.eSetProxyURI(uris.get(i));
	}
}
 
Example 6
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void processMetamodelDeclarations(Collection<AbstractMetamodelDeclaration> declarations, ResourceSet resourceSet,
		Collection<Resource> resourcesToRemove, Collection<Resource> resourcesToUnload,
		Collection<Resource> referencedResources) {
	for (AbstractMetamodelDeclaration declaration : declarations) {
		EPackage pack = (EPackage) declaration.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
		if (pack != null && !pack.eIsProxy()) {
			Resource packResource = pack.eResource();
			if (packResource != null) {
				resourcesToRemove.add(packResource);
				if (declaration instanceof ReferencedMetamodel) {
						resourcesToUnload.add(packResource);
				}
				if (isPackageReferenced(resourceSet, pack, declarations)) {
					referencedResources.add(packResource);
				}
			}
		}
	}
}
 
Example 7
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
void discardGeneratedPackages(EObject root) {
	if (root instanceof Grammar) {
		// unload generated metamodels as they will be recreated during linking 
		for (AbstractMetamodelDeclaration metamodelDeclaration : ((Grammar) root).getMetamodelDeclarations()) {
			if (metamodelDeclaration instanceof GeneratedMetamodel) {
				EPackage ePackage = (EPackage) metamodelDeclaration.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
				if (ePackage != null && !ePackage.eIsProxy()) {
					Resource resource = ePackage.eResource();
					if (resource != null && resource.getResourceSet() != null) {
						if (unloader != null) {
							for (EObject content : resource.getContents()) {
								unloader.unloadRoot(content);
							}
						}
						resource.getResourceSet().getResources().remove(resource);
					}
				}
			}
		}
	}
}
 
Example 8
Source File: EPackageChooser.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected List<EPackageInfo> createEPackageInfosFromGenModel(URI genModelURI) {
	ResourceSet resourceSet = createResourceSet(genModelURI);
	Resource resource = resourceSet.getResource(genModelURI, true);
	List<EPackageInfo> ePackageInfos = Lists.newArrayList();
	for (TreeIterator<EObject> i = resource.getAllContents(); i.hasNext();) {
		EObject next = i.next();
		if (next instanceof GenPackage) {
			GenPackage genPackage = (GenPackage) next;
			EPackage ePackage = genPackage.getEcorePackage();
			URI importURI;
			if(ePackage.eResource() == null) {
				importURI = URI.createURI(ePackage.getNsURI());
			} else {
				importURI = ePackage.eResource().getURI();
			}
			EPackageInfo ePackageInfo = new EPackageInfo(ePackage, importURI, genModelURI, genPackage
					.getQualifiedPackageInterfaceName(), genPackage.getGenModel().getModelPluginID());
			ePackageInfos.add(ePackageInfo);
		} else if (!(next instanceof GenModel)) {
			i.prune();
		}
	}
	return ePackageInfos;
}
 
Example 9
Source File: GenModelAccess.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public static GenPackage getGenPackage(EPackage pkg, ResourceSet resourceSet) {
	String nsURI = pkg.getNsURI();
	String location = null;
	if (pkg.eResource() != null && pkg.eResource().getURI() != null)
		location = pkg.eResource().getURI().toString();
	Resource genModelResource = getGenModelResource(location, nsURI, resourceSet);
	if (genModelResource != null) {
		for (EObject model : genModelResource.getContents()) {
			if (model instanceof GenModel) {
				GenPackage genPkg = ((GenModel) model).findGenPackage(pkg);
				if (genPkg != null) {
					genPkg.getEcorePackage().getEClassifiers();
					return genPkg;
				}
			}
		}
		throw new RuntimeException("No GenPackage for NsURI " + nsURI + " found in " + genModelResource.getURI());
	}
	throw new RuntimeException("No GenPackage for NsURI " + nsURI + ".");
}
 
Example 10
Source File: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void replaceResourceURIsWithNsURIs(Grammar grammar, ResourceSet set) {
	for (AbstractMetamodelDeclaration metamodelDecl : grammar.getMetamodelDeclarations()) {
		EPackage pack = metamodelDecl.getEPackage();
		Resource packResource = pack.eResource();
		if (!packResource.getURI().toString().equals(pack.getNsURI())) {
			ResourceSet packResourceSet = packResource.getResourceSet();
			if (packResourceSet != null) {
				EPackage topMost = pack;
				// we need to be aware of empty subpackages
				while (topMost.getESuperPackage() != null
						&& topMost.getESuperPackage().eResource() == topMost.eResource())
					topMost = topMost.getESuperPackage();
				if (packResource.getContents().contains(topMost) && packResource.getContents().size() == 1) {
					if (!topMost.getEClassifiers().isEmpty())
						packResource.setURI(URI.createURI(topMost.getNsURI()));
					else
						moveSubpackagesToNewResource(topMost, set);
				}
				if (!topMost.eResource().getURI().toString().equals(topMost.getNsURI())) 
					movePackageToNewResource(topMost, set);
			}
		}
	}
}
 
Example 11
Source File: SerializationExtensions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends ENamedElement> T readEcoreElement(ObjectInput in) throws IOException {
	URI uri = readURI(in);
	EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
	if (ePackage == null)
		return null;
	Resource eResource = ePackage.eResource();
	if (eResource == null)
		return null;
	return (T) eResource.getEObject(uri.fragment());
}
 
Example 12
Source File: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void moveSubpackagesToNewResource(EPackage pack, ResourceSet set) {
	for (int i = pack.getESubpackages().size() - 1; i >= 0; i--) {
		EPackage sub = pack.getESubpackages().get(i);
		if (sub.eResource() == pack.eResource()) {
			if (sub.getEClassifiers().isEmpty()) {
				moveSubpackagesToNewResource(sub, set);
			} else {
				movePackageToNewResource(sub, set);
				pack.getESubpackages().remove(i);
			}
		}
	}
}
 
Example 13
Source File: GenModelUtil2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static GenPackage getGenPackage(final EPackage pkg, final ResourceSet resourceSet) {
  final String nsURI = pkg.getNsURI();
  String location = null;
  Resource _eResource = pkg.eResource();
  URI _uRI = null;
  if (_eResource!=null) {
    _uRI=_eResource.getURI();
  }
  boolean _tripleNotEquals = (_uRI != null);
  if (_tripleNotEquals) {
    location = pkg.eResource().getURI().toString();
  }
  final Resource genModelResource = GenModelUtil2.getGenModelResource(location, nsURI, resourceSet);
  if ((genModelResource != null)) {
    EList<EObject> _contents = genModelResource.getContents();
    for (final EObject model : _contents) {
      if ((model instanceof GenModel)) {
        final GenPackage genPkg = ((GenModel)model).findGenPackage(pkg);
        if ((genPkg != null)) {
          genPkg.getEcorePackage().getEClassifiers();
          return genPkg;
        }
      }
    }
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("No GenPackage for NsURI ");
    _builder.append(nsURI);
    _builder.append(" found in ");
    URI _uRI_1 = genModelResource.getURI();
    _builder.append(_uRI_1);
    throw new RuntimeException(_builder.toString());
  }
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("No GenPackage for NsURI ");
  _builder_1.append(nsURI);
  _builder_1.append(".");
  throw new RuntimeException(_builder_1.toString());
}
 
Example 14
Source File: EClasses.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find the {@link EClass} for a given {@link URI}.
 *
 * @param uri
 *          of the EClass to find.
 * @return the EClass, if found, or {@code null} otherwise.
 */
public static EClass getEClass(final URI uri) {
  EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
  if (ePackage != null) {
    if (ePackage.eResource() != null) {
      return (EClass) ePackage.eResource().getEObject(uri.fragment());
    }
    for (EClassifier eClassifier : ePackage.getEClassifiers()) {
      if (EcoreUtil.getURI(eClassifier).equals(uri) && eClassifier instanceof EClass) {
        return (EClass) eClassifier;
      }
    }
  }
  return null;
}
 
Example 15
Source File: ProjectStatePersister.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private ENamedElement readEcoreElement(DataInput input) throws IOException {
	URI uri = URI.createURI(input.readUTF());
	EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
	if (ePackage != null) {
		Resource resource = ePackage.eResource();
		return (ENamedElement) resource.getEObject(uri.fragment());
	}
	return null;
}
 
Example 16
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isRegisteredPackage(EPackage ePackage) {
	return ePackage != null && (ePackage.eResource() == null || ePackage.getNsURI().equals(ePackage.eResource().getURI().toString()));
}
 
Example 17
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) {
  boolean _add = visitedGrammars.add(grammar);
  boolean _not = (!_add);
  if (_not) {
    return;
  }
  resource.getContents().add(grammar);
  EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations();
  for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) {
    {
      final EPackage pack = metamodelDecl.getEPackage();
      final Resource packResource = pack.eResource();
      String _string = packResource.getURI().toString();
      String _nsURI = pack.getNsURI();
      boolean _notEquals = (!Objects.equal(_string, _nsURI));
      if (_notEquals) {
        final ResourceSet packResourceSet = packResource.getResourceSet();
        if ((packResourceSet != null)) {
          EPackage topMost = pack;
          while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) {
            topMost = topMost.getESuperPackage();
          }
          if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) {
            boolean _isEmpty = topMost.getEClassifiers().isEmpty();
            boolean _not_1 = (!_isEmpty);
            if (_not_1) {
              packResource.setURI(URI.createURI(topMost.getNsURI()));
            } else {
              this.moveSubpackagesToNewResource(topMost, resource.getResourceSet());
            }
          }
          boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI());
          boolean _not_2 = (!_equals);
          if (_not_2) {
            this.movePackageToNewResource(topMost, resource.getResourceSet());
          }
        }
      }
    }
  }
  EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
  for (final Grammar usedGrammar : _usedGrammars) {
    this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars);
  }
}