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

The following examples show how to use org.eclipse.emf.ecore.EPackage#setNsURI() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@Test public void testNestedPackage() throws Exception {
	Resource resource = new XMIResourceImpl();
	EPackage parent = EcoreFactory.eINSTANCE.createEPackage();
	parent.setName("parent");
	parent.setNsURI("http://parent");
	EPackage child = EcoreFactory.eINSTANCE.createEPackage();
	child.setName("child");
	child.setNsURI("http://child");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	child.getEClassifiers().add(eClass);
	parent.getESubpackages().add(child);
	resource.getContents().add(parent);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, parent, false, "parent");
	checkEntry(index, child, false, "parent", "child");
	checkEntry(index, eClass, false, "parent", "child", "Test");
	checkEntry(index, parent, true, "http://parent");
	checkEntry(index, child, true, "http://child");
	checkEntry(index, eClass, true, "http://child", "Test");
	assertEquals(6,index.size());
}
 
Example 2
@Test public void testMissingMiddleName() {
	Resource resource = new XMIResourceImpl();
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsURI("http://test");
	EClass unnamedEClass = EcoreFactory.eINSTANCE.createEClass();
	EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	eAttribute.setName("test");
	unnamedEClass.getEStructuralFeatures().add(eAttribute);
	ePackage.getEClassifiers().add(unnamedEClass);
	resource.getContents().add(ePackage);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, ePackage, false, "test");
	checkEntry(index, ePackage, true, "http://test");
	assertEquals(2,index.size());
	
	unnamedEClass.setName("Test");
	index = createIndex(resource);
	checkEntry(index, ePackage, false, "test");
	checkEntry(index, ePackage, true, "http://test");
	checkEntry(index, unnamedEClass, false, "test", "Test");
	checkEntry(index, unnamedEClass, true, "http://test", "Test");
	checkEntry(index, eAttribute, false, "test", "Test", "test");
	checkEntry(index, eAttribute, true, "http://test", "Test", "test");
	assertEquals(6,index.size());
}
 
Example 3
private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) {
	final String nsURI = getMetamodelNsURI(text);
	final URI uri = URI.createURI(nsURI);
	if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI))
		return Collections.emptyList();
	EPackage pack = ((GeneratedMetamodelImpl)generatedMetamodel).basicGetEPackage();
	if (pack != null && !pack.eIsProxy())
		return singletonList((EObject)pack);
	final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage();
	generatedEPackage.setName(generatedMetamodel.getName());
	generatedEPackage.setNsPrefix(generatedMetamodel.getName());
	generatedEPackage.setNsURI(nsURI);
	final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri);
	XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet();
	if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) {
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	} else {
		generatedPackageResource.setURI(URI.createURI(nsURI+"_"+generatedMetamodel.hashCode()));
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	}
	generatedPackageResource.getContents().add(generatedEPackage);
	return Collections.<EObject>singletonList(generatedEPackage);
}
 
Example 4
@Test public void testGetAllReferencedObjects() {
	EClass a = createEClass("a");
	EClass b = createEClass("b");
	
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setName("empty");
	pack.setNsPrefix("empty");
	pack.setNsURI("empty");
	pack.getEClassifiers().add(a);
	pack.getEClassifiers().add(b);
	
	EReference ref = EcoreFactory.eINSTANCE.createEReference();
	a.getEStructuralFeatures().add(ref);
	ref.setUpperBound(1);
	ref.setEType(b);
	EObject objA = pack.getEFactoryInstance().create(a);
	EObject objB = pack.getEFactoryInstance().create(b);
	List<EObject> res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	assertTrue(res.isEmpty());
	res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	objA.eSet(ref, objB);
}
 
Example 5
@Test public void testMissingName() {
	Resource resource = new XMIResourceImpl();
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setNsURI("http://test");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);
	resource.getContents().add(ePackage);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, ePackage, true, "http://test");
	checkEntry(index, eClass, true, "http://test", "Test");
	assertEquals(2,index.size());
}
 
Example 6
@Test public void testRefactorEcoreCrossLanguage() throws Exception {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("http://test");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName(TEST_CLASS);
	ePackage.getEClassifiers().add(eClass);
	Resource ecoreResource = new ResourceSetImpl().createResource(URI.createPlatformResourceURI(TEST_PROJECT + "/Test.ecore", true));
	ecoreResource.getContents().add(ePackage);
	ecoreResource.save(null);
	ecoreResource.unload();
	project.refreshLocal(IResource.DEPTH_INFINITE, null);
	
	String model = "ref test." + TEST_CLASS;
	IFile file = createFile(TEST_PROJECT + "/ref.referringtestlanguage", model);
	waitForBuild();
	final XtextEditor editor = openEditor(file);
	final TextSelection selection = new TextSelection(model.indexOf(TEST_CLASS), TEST_CLASS.length());
	editor.getSelectionProvider().setSelection(selection);
	waitForDisplay();
	IRenameElementContext context = editor.getDocument().readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
		@Override
		public IRenameElementContext exec(XtextResource state) throws Exception {
			Reference ref = (Reference) state.getContents().get(0).eContents().get(0);
			EObject referenced = ref.getReferenced();
			assertNotNull(referenced);
			return new IRenameElementContext.Impl(EcoreUtil.getURI(referenced), referenced.eClass(), editor, selection, state.getURI());
		}
	});
	renameRefactoringController.startRefactoring(context);
	waitForDisplay();
	pressKeys(editor, "NewTestClass\n");
	waitForReconciler(editor);
	waitForDisplay();
	waitForBuild();
	ecoreResource.load(null);
	assertEquals("NewTestClass", ((EPackage)ecoreResource.getContents().get(0)).getEClassifiers().get(0).getName());
}
 
Example 7
@Before
public void setUp() throws Exception {
	errorAcceptorMock = new FailOnErrorAcceptor();
	metamodel = XtextFactory.eINSTANCE.createGeneratedMetamodel();
	Grammar grammar = XtextFactory.eINSTANCE.createGrammar();
	grammar.getMetamodelDeclarations().add(metamodel);
	infos = new EClassifierInfos(grammar);
	INT = EcoreFactory.eINSTANCE.createEDataType();
	STRING = EcoreFactory.eINSTANCE.createEDataType();
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setNsURI("myURI");
	pack.setName("myName");
	pack.setNsPrefix("myPrefix");
	metamodel.setEPackage(pack);
}
 
Example 8
public EPackage createEPackage(String name) {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName(name);
	ePackage.setNsPrefix(name);
	ePackage.setNsURI(name);
	packages.put(ePackage.getName(), ePackage);
	return ePackage;
}
 
Example 9
Source Project: uima-uimaj   File: UimaTypeSystem2Ecore.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets or creates an EPackage for a UIMA namespace. Actually will create a whole chain of nested
 * EPackages, one for each component of the UIMA namespace, but only the leaf node of the chain
 * will be returned.
 * 
 * @param uimaNamespace
 *          UIMA namespace
 * @return EPackage corresponding to this namespace.
 */
private static EPackage uimaNamespace2EPackage(String uimaNamespace) {
  // convert UIMA namespace (dotted string) to namespace URI
  String nsUri = uimaNamespace2NamespaceUri(uimaNamespace);
  // see if package already exists for this URI
  EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsUri);
  if (ePackage == null) {
    // package name is last component of namespace.
    // all other components form the parent namespace
    String parentNamespace = null;
    String packageName;
    if (uimaNamespace != null) {
      int lastDot = uimaNamespace.lastIndexOf('.');
      packageName = uimaNamespace.substring(lastDot + 1);
      if (lastDot > 0) {
        parentNamespace = uimaNamespace.substring(0, lastDot);
      }
    } else {
      packageName = "noNamespace";
    }

    // create Package
    ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setNsURI(nsUri);
    ePackage.setName(packageName);
    EPackage.Registry.INSTANCE.put(nsUri, ePackage);

    // get or create SuperPackage if any
    if (parentNamespace != null) {
      EPackage superPackage = uimaNamespace2EPackage(parentNamespace);
      superPackage.getESubpackages().add(ePackage);
    }
  }
  return ePackage;
}
 
Example 10
@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
	ResourceSet rs = resourceSetProvider.get();
	EcoreFactory eInstance = EcoreFactory.eINSTANCE;
	Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
	EClass greetingClass = getGreetingClass(ecoreModelResource);
	EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
	String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
	String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();

	EPackage refPackage = eInstance.createEPackage();
	refPackage.setName("myDsl2");
	refPackage.setNsPrefix("myDsl2");
	refPackage.setNsURI("http://testrefactoring2");
	EClass modelRefClass = eInstance.createEClass();
	refPackage.getEClassifiers().add(modelRefClass);
	modelRefClass.setName("ModelRef");
	EReference reference = eInstance.createEReference();
	reference.setName("ref");
	reference.setLowerBound(0);
	reference.setUpperBound(-1);
	reference.setEType(greetingClass);
	modelRefClass.getEStructuralFeatures().add(reference);
	Resource refToGreetingResource = createEcoreModel(rs,
			URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true),
			refPackage);
	refToGreetingResource.unload();
	ecoreModelResource.unload();
	waitForBuild();
	waitForDisplay();
	XtextEditor editor = openEditor(grammarFile);
	doRefactoring(editor);
	waitForBuild();
	checkConsistenceOfGrammar(editor);
	ecoreModelResource.load(null);
	String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
	EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
	assertNotNull(renamedGreetingClass);
	assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
	EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
	EClassifier eType = greetingReference.getEType();
	assertFalse(eType.eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());

	refToGreetingResource.load(null);
	EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
	assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());

}
 
Example 11
@Test public void testSpecialReferences() {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	eClass.getESuperTypes().add(EcorePackage.Literals.EPACKAGE);
	ePackage.getEClassifiers().add(eClass);

	EReference eReference1 = EcoreFactory.eINSTANCE.createEReference();
	eReference1.setContainment(false);
	eReference1.setName("onlyExportedRef");
	eReference1.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference1);

	EReference eReference2 = EcoreFactory.eINSTANCE.createEReference();
	eReference2.setContainment(true);
	eReference2.setName("containmentRef");
	eReference2.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference2);

	EReference eReference3 = EcoreFactory.eINSTANCE.createEReference();
	eReference3.setContainment(false);
	eReference3.setTransient(true);
	eReference3.setName("transientRef");
	eReference3.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference3);

	EReference eReference4 = EcoreFactory.eINSTANCE.createEReference();
	eReference4.setContainment(false);
	eReference4.setVolatile(true);
	eReference4.setName("volatileRef");
	eReference4.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference4);

	EReference eReference5 = EcoreFactory.eINSTANCE.createEReference();
	eReference5.setContainment(false);
	eReference5.setDerived(true);
	eReference5.setName("derivedRef");
	eReference5.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference5);

	EObject object = ePackage.getEFactoryInstance().create(eClass);
	object.eSet(EcorePackage.Literals.ENAMED_ELEMENT__NAME, "testname");
	object.eSet(eReference1, EcorePackage.eINSTANCE);
	object.eSet(eReference2, ePackage.getEFactoryInstance().create(eClass));
	object.eSet(eReference3, EcorePackage.eINSTANCE);
	object.eSet(eReference4, EcorePackage.eINSTANCE);
	object.eSet(eReference5, EcorePackage.eINSTANCE);

	Resource testResource = new XMIResourceImpl(URI.createPlatformResourceURI("test.ecore", true));
	testResource.getContents().add(object);
	IResourceDescription resourceDescription = createResourceDescription(testResource);
	assertEquals("Only one external reference expected", 1, size(resourceDescription.getReferenceDescriptions()));
	IReferenceDescription referenceDescription = resourceDescription.getReferenceDescriptions().iterator().next();
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(object), referenceDescription.getSourceEObjectUri());
	assertEquals(eReference1, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.eINSTANCE), referenceDescription.getTargetEObjectUri());
	assertEquals(EcoreUtil.getURI(object), referenceDescription.getContainerEObjectURI());
}
 
Example 12
@Test public void testCrossResourceContainment() {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);
	
	EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	nameAttribute.setName("name");
	nameAttribute.setID(true);
	nameAttribute.setEType(EcorePackage.Literals.ESTRING);
	eClass.getEStructuralFeatures().add(nameAttribute);

	EReference containmentRef = EcoreFactory.eINSTANCE.createEReference();
	containmentRef.setContainment(true);
	containmentRef.setName("crossResourceContainment");
	containmentRef.setEType(eClass);
	containmentRef.setResolveProxies(true);
	eClass.getEStructuralFeatures().add(containmentRef);
	
	EReference containerRef = EcoreFactory.eINSTANCE.createEReference();
	containerRef.setName("containerRef");
	containerRef.setEType(eClass);
	containerRef.setResolveProxies(true);
	containerRef.setEOpposite(containmentRef);
	containmentRef.setEOpposite(containerRef);
	eClass.getEStructuralFeatures().add(containerRef);

	EObject container = ePackage.getEFactoryInstance().create(eClass);
	EObject child = ePackage.getEFactoryInstance().create(eClass);
	
	Resource containerResource = new XMIResourceImpl(URI.createPlatformResourceURI("container.ecore", true));
	Resource childResource = new XMIResourceImpl(URI.createPlatformResourceURI("child.ecore", true));
	ResourceSet resourceSet = new ResourceSetImpl();
	resourceSet.getResources().add(containerResource);
	resourceSet.getResources().add(childResource);
	
	containerResource.getContents().add(container);
	childResource.getContents().add(child);
	
	container.eSet(containmentRef, child);
	assertTrue(container.eResource() != child.eResource());
	
	{ 
		IResourceDescription containerDescription = createResourceDescription(containerResource);
		IReferenceDescription onlyContainerElement = Iterables.getOnlyElement(containerDescription.getReferenceDescriptions());
		assertEquals(-1, onlyContainerElement.getIndexInList());
		assertEquals(EcoreUtil.getURI(container), onlyContainerElement.getSourceEObjectUri());
		assertEquals(containmentRef, onlyContainerElement.getEReference());
		assertEquals(EcoreUtil.getURI(child), onlyContainerElement.getTargetEObjectUri());
	}
	{
		IResourceDescription childDescription = createResourceDescription(childResource);
		IReferenceDescription onlyChildElement = Iterables.getOnlyElement(childDescription.getReferenceDescriptions());
		assertEquals(-1, onlyChildElement.getIndexInList());
		assertEquals(EcoreUtil.getURI(child), onlyChildElement.getSourceEObjectUri());
		assertEquals(containerRef, onlyChildElement.getEReference());
		assertEquals(EcoreUtil.getURI(container), onlyChildElement.getTargetEObjectUri());
	}
}
 
Example 13
/** @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=330812 */
@Test public void testLazyLinkingProxyReferences() {
	URI resourceUri = URI.createPlatformResourceURI("test.ecore", true);
	LazyURIEncoder lazyURIEncoder = new LazyURIEncoder();

	ResourceSet resourceSet = new ResourceSetImpl();
	Resource testResource = resourceSet.createResource(resourceUri);

	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");
	testResource.getContents().add(ePackage);

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);

	EClass multiRefType = EcoreFactory.eINSTANCE.createEClass();
	((InternalEObject) multiRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE));
	eClass.getESuperTypes().add(multiRefType);

	EClass multiRefType2 = EcoreFactory.eINSTANCE.createEClass();
	URI dummyProxyUri = resourceUri.appendFragment(lazyURIEncoder.encode(eClass, EcorePackage.Literals.ECLASS__ESUPER_TYPES, null));
	((InternalEObject) multiRefType2).eSetProxyURI(dummyProxyUri);
	eClass.getESuperTypes().add(multiRefType2);

	EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	nameAttribute.setName("name");
	eClass.getEStructuralFeatures().add(nameAttribute);
	EDataType singleRefType = EcoreFactory.eINSTANCE.createEDataType();
	((InternalEObject) singleRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.ESTRING));
	nameAttribute.setEType(singleRefType);

	assertTrue(multiRefType.eIsProxy());
	assertTrue(multiRefType2.eIsProxy());
	assertTrue(lazyURIEncoder.isCrossLinkFragment(testResource, EcoreUtil.getURI(multiRefType2).fragment()));
	assertTrue(singleRefType.eIsProxy());

	IResourceDescription resourceDescription = createResourceDescription(testResource);
	Iterable<IReferenceDescription> referenceDescriptions = resourceDescription.getReferenceDescriptions();
	assertEquals("Unexpected additional resources were loaded", 1, resourceSet.getResources().size());
	assertEquals("Unexpected reference was exported", 3, Iterables.size(referenceDescriptions));

	IReferenceDescription referenceDescription = Iterables.get(referenceDescriptions, 0);
	assertEquals(0, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(eClass), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.ECLASS__ESUPER_TYPES, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());

	referenceDescription = Iterables.get(referenceDescriptions, 1);
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(nameAttribute.getEGenericType()), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.ESTRING), referenceDescription.getTargetEObjectUri());

	referenceDescription = Iterables.get(referenceDescriptions, 2);
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(eClass.getEGenericSuperTypes().get(0)), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());
}