Java Code Examples for org.eclipse.emf.ecore.resource.Resource#getAllContents()

The following examples show how to use org.eclipse.emf.ecore.resource.Resource#getAllContents() . 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: SolidityImportedNamespaceAwareLocalScopeProvider.java    From solidity-ide with Eclipse Public License 1.0 6 votes vote down vote up
protected List<ImportNormalizer> getSuperTypeImports(Resource res, EReference reference) {
	List<ImportNormalizer> result = Lists.newArrayList();
	TreeIterator<EObject> allContents = res.getAllContents();
	while (allContents.hasNext()) {
		EObject next = allContents.next();
		if (next instanceof ContractDefinition) {
			ContractDefinition contract = (ContractDefinition) next;
			EList<TypeSpecifier> superTypes = contract.getSuperTypes();
			for (TypeSpecifier superType : superTypes) {
				ImportNormalizer resolver = createImportedNamespaceResolver(superType.getType().getName() + ".*", false);
				result.add(resolver);
			}
			allContents.prune();
		}
	}
	return result;
}
 
Example 2
Source File: GenerateImplementations.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Find any languages used and add them to the languages information.
 * 
 * @param model     the crossflow model
 * @param languages the languages information
 */
// FIXME Languages should be an enumeration at the meta-metamodel level so we
// can control
// what languages we support and is less error prone
private Map<String, String[]> findLanguages(EmfModel model) {
	final Map<String, String[]> languages = new HashMap<>();
	Resource r = model.getResource();
	EClass languageClass = (EClass) r.getContents().get(0).eClass().getEPackage().getEClassifier("Language");
	EAttribute nameAttr = languageClass.getEAllAttributes().stream().filter(a -> a.getName().equals("name"))
			.findFirst().get();
	EAttribute outfolderAttr = languageClass.getEAllAttributes().stream()
			.filter(a -> a.getName().equals("outputFolder")).findFirst().get();
	EAttribute genOutfolderAttr = languageClass.getEAllAttributes().stream()
			.filter(a -> a.getName().equals("genOutputFolder")).findFirst().get();
	for (Iterator<EObject> it = r.getAllContents(); it.hasNext();) {
		EObject o;
		if ((o = it.next()).eClass().equals(languageClass)) {
			String[] value = new String[2];
			value[0] = ((String) o.eGet(outfolderAttr)).toLowerCase();
			value[1] = ((String) o.eGet(genOutfolderAttr)).toLowerCase();
			languages.put(((String) o.eGet(nameAttr)).toLowerCase(), value);
		}
	}
	return languages;
}
 
Example 3
Source File: GenerateImplementations.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Find any scripting languages used and add them to the languages information.
 * 
 * @param model     the crossflow model
 * @param languages the languages information
 */
private static Map<String, String[]> findScriptingLanguages(EmfModel model) {
	final Map<String, String[]> languages = new HashMap<>();
	Resource r = model.getResource();
	EClass scriptingTask = (EClass) r.getContents().get(0).eClass().getEPackage().getEClassifier("ScriptedTask");
	EAttribute scriptingLanguage = scriptingTask.getEAllAttributes().stream()
			.filter(a -> a.getName().equals("scriptingLanguage")).findFirst().get();
	for (Iterator<EObject> it = r.getAllContents(); it.hasNext();) {
		EObject o;
		String language;
		if ((o = it.next()).eClass().equals(scriptingTask))
			if (o.eIsSet(scriptingLanguage) && (language = (String) o.eGet(scriptingLanguage)).trim().length() > 0)
				languages.put(language, new String[] { "src", "src-gen" });
	}
	return languages;
}
 
Example 4
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 5
Source File: MyGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	TreeIterator<EObject> allContents = input.getAllContents();
	while (allContents.hasNext()) {
		EObject next = allContents.next();
		if (next instanceof Element) {
			Element ele = (Element) next;
			String fileName = ele.getName() + ".txt";
			if (fsa instanceof IFileSystemAccess2) {
				IFileSystemAccess2 fileSystemAccess2 = (IFileSystemAccess2) fsa;
				if (fileSystemAccess2.isFile(fileName)) {
					fileSystemAccess2.readTextFile(fileName);
				}
			}
			fsa.generateFile(fileName, "object " + ele.getName());
		}
	}
}
 
Example 6
Source File: EcoreUtil2.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public static final EPackage loadEPackage(String uriAsString, ClassLoader classLoader) {
	if (EPackage.Registry.INSTANCE.containsKey(uriAsString))
		return EPackage.Registry.INSTANCE.getEPackage(uriAsString);
	URI uri = URI.createURI(uriAsString);
	uri = new ClassloaderClasspathUriResolver().resolve(classLoader, uri);
	Resource resource = new ResourceSetImpl().getResource(uri, true);
	for (TreeIterator<EObject> allContents = resource.getAllContents(); allContents.hasNext();) {
		EObject next = allContents.next();
		if (next instanceof EPackage) {
			EPackage ePackage = (EPackage) next;
			// if (ePackage.getNsURI() != null &&
			// ePackage.getNsURI().equals(uriAsString)) {
			return ePackage;
			// }
		}
	}
	log.error("Could not load EPackage with nsURI" + uriAsString);
	return null;
}
 
Example 7
Source File: IndexTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	TreeIterator<EObject> iter = input.getAllContents();
	while (iter.hasNext()) {
		EObject e = iter.next();
		if (e instanceof Entity) {
			Entity entity = (Entity) e;
			StringConcatenation builder = new StringConcatenation();
			builder.append("Hello ");
			builder.append(entity.getName());
			builder.append("!");
			builder.newLineIfNotEmpty();
			fsa.generateFile(entity.getName() + ".txt", builder);
		}
	}
}
 
Example 8
Source File: ForwardConverter.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/** Create a node for each EMF model element */
protected void initElements(ResourceSet resourceSet) {
	for (final Resource resource : resourceSet.getResources()) {
		for (final TreeIterator<EObject> i = resource.getAllContents(); i
			.hasNext();) {
			final EObject eObject = i.next();
			if (mapping.containsKey(eObject)) {
				i.prune();
			} else {
				final Instance instance = newInstance(eObject, eObject.eIsProxy());
				final String uuid = EcoreUtils.getUUID(eObject);
				instance.setUuid(uuid);
			}
		}
	}
}
 
Example 9
Source File: ForwardConverter.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/** Initialize the properties of the nodes. */
protected void initProperties(ResourceSet resourceSet) {
	final Set<EObject> done = new HashSet<EObject>();
	for (final Resource resource : resourceSet.getResources()) {
		for (final TreeIterator<EObject> i = resource.getAllContents(); i
			.hasNext();) {
			final EObject eObject = i.next();
			if (done.contains(eObject)) {
				i.prune();
			} else {
				initInstance(eObject);
				done.add(eObject);
			}
		}
	}
}
 
Example 10
Source File: XtextComparisonExpressionLoaderTest.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Test
@Ignore("can't make failing the test with the known bug... sometimes it seems to work")
public void testLoadResourceWIthUTF8() throws ComparisonExpressionLoadException {
    final Injector injector = ConditionModelActivator.getInstance().getInjector(ConditionModelActivator.ORG_BONITASOFT_STUDIO_CONDITION_CONDITIONMODEL);
    final XtextComparisonExpressionLoader xtextComparisonExpressionLoader = Mockito
            .spy(new XtextComparisonExpressionLoader(injector.getInstance(ConditionModelGlobalScopeProvider.class),
                    new ModelSearch(Collections::emptyList), new ProjectXtextResourceProvider(injector)));
    Mockito.doReturn(Collections.singletonList("管理者")).when(xtextComparisonExpressionLoader).getAccessibleReferences(Mockito.any(EObject.class));
    final Resource resource = xtextComparisonExpressionLoader.loadResource("管理者 == \"test\"", null);
    final TreeIterator<EObject> allContents = resource.getAllContents();
    while (allContents.hasNext()) {
        final EObject current = allContents.next();
        if (current instanceof Operation_Equals) {
            final Expression_ProcessRef left = (Expression_ProcessRef) ((Operation_Equals) current).getLeft();
            final EObject value = left.getValue();
            Assert.assertEquals("管理者", value);
            return;
        }
    }
    fail("Condition Expression not loaded");
}
 
Example 11
Source File: UimaTypeSystem2Ecore.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Load uima builtins ecore.
 *
 * @param resourceSet the resource set
 * @param aSchemaLocationMap the a schema location map
 * @return the resource
 */
private static Resource loadUimaBuiltinsEcore(ResourceSet resourceSet, Map aSchemaLocationMap) {
  // load Ecore model for UIMA built-in types (use classloader to locate)
  URL uimaEcoreUrl = UimaTypeSystem2Ecore.class.getResource("/uima.ecore");
  if (uimaEcoreUrl == null) {
    throw new UIMARuntimeException(UIMARuntimeException.UIMA_ECORE_NOT_FOUND, new Object[0]);
  }
  Resource uimaEcoreResource = resourceSet.getResource(URI.createURI(uimaEcoreUrl.toString()),
          true);
  // register core UIMA packages (I'm surprised I need to do this manually)
  TreeIterator iter = uimaEcoreResource.getAllContents();
  while (iter.hasNext()) {
    Object current = iter.next();
    if (current instanceof EPackage) {
      EPackage pkg = (EPackage) current;
      EPackage.Registry.INSTANCE.put(pkg.getNsURI(), pkg);
      if (aSchemaLocationMap != null) {
        String schemaLoc = uimaEcoreResource.getURI() + "#"
                + uimaEcoreResource.getURIFragment(pkg);
        aSchemaLocationMap.put(pkg.getNsURI(), schemaLoc);
      }
    }
  }
  return uimaEcoreResource;
}
 
Example 12
Source File: N4JSCrossReferenceComputer.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collects all Types, TVariables, TLiterals and IdentifiableElements that are directly referenced somewhere in the
 * given resource and aren't contained in this resource. References between AST element to its defined type and vice
 * versa as well as references to built-in and primitive types are ignored.
 *
 * @param resource
 *            the given fully resolved resource
 * @param acceptor
 *            the logic that collects the passed EObject found in a cross reference
 */
public void computeCrossRefs(Resource resource, IAcceptor<EObject> acceptor) {
	TreeIterator<EObject> allASTContentsIter;
	if (resource instanceof N4JSResource) {
		Script script = ((N4JSResource) resource).getScript();
		// We traverse the AST but not the TModule tree
		allASTContentsIter = script.eAllContents();
	} else {
		allASTContentsIter = resource.getAllContents();
	}
	while (allASTContentsIter.hasNext()) {
		EObject eObject = allASTContentsIter.next();
		computeCrossRefs(resource, eObject, acceptor);
	}
}
 
Example 13
Source File: RefactoringTestLanguageFragmentProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, Fallback fallback) {
	if (useNames) {
		for (TreeIterator<EObject> i = resource.getAllContents(); i.hasNext();) {
			EObject obj = i.next();
			if (obj instanceof Element && fragment.equals(((Element) obj).getName()))
				return obj;
		}
	}
	return fallback.getEObject(fragment);
}
 
Example 14
Source File: SimpleLocalScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ISelectable getAllDescriptions(final Resource resource) {
	Iterable<EObject> allContents = new Iterable<EObject>(){
		@Override
		public Iterator<EObject> iterator() {
			return resource.getAllContents();
		}
	}; 
	Iterable<IEObjectDescription> allDescriptions = Scopes.scopedElementsFor(allContents, qualifiedNameProvider);
	return new MultimapBasedSelectable(allDescriptions);
}
 
Example 15
Source File: EcoreUtil2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static void resolveAll(Resource resource, CancelIndicator monitor) {
	for (Iterator<EObject> i = resource.getAllContents(); i.hasNext();) {
		if (monitor.isCanceled())
			throw new OperationCanceledException();
		EObject eObject = i.next();
		resolveCrossReferences(eObject, monitor);
	}
}
 
Example 16
Source File: GamlScopeProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected ISelectable getAllDescriptions(final Resource resource) {
	final GamlMultimapBasedSelectable result = new GamlMultimapBasedSelectable();
	final IQualifiedNameProvider provider = getNameProvider();
	final Iterator<EObject> iterator = resource.getAllContents();
	while (iterator.hasNext()) {
		final EObject from = iterator.next();
		final QualifiedName qualifiedName = provider.apply(from);
		if (qualifiedName != null) {
			result.add(qualifiedName, new EObjectDescription(qualifiedName, from, null));
		}
	}
	return result;
}