use of org.eclipse.xtext.resource.XtextResourceSet in project dsl-devkit by dsldevkit.
the class BugAig1314 method testSameScopeUseTwice.
/**
* Tests that querying the same scope twice doesn't make the resource set grow.
*/
@Test
public void testSameScopeUseTwice() {
XtextResourceSet rs = new XtextResourceSet();
URL url = createURL();
ModelLocation modelLocation = createModelLocation(url);
CatalogFromExtensionPointScope scope = new TestScope(modelLocation, rs);
assertResourceSetEmpty(rs);
Iterable<IEObjectDescription> elements = scope.getAllElements();
assertIterableNotEmpty(elements);
int nofResourcesInMap = rs.getURIResourceMap().size();
int nofResourcesInSet = rs.getResources().size();
elements = scope.getAllElements();
assertIterableNotEmpty(elements);
assertResourceSet(rs, nofResourcesInSet, nofResourcesInMap);
}
use of org.eclipse.xtext.resource.XtextResourceSet in project dsl-devkit by dsldevkit.
the class AbstractXtextTestUtil method getResource.
/**
* Creates a resource with the given URI and content.
*
* @param uri
* to associate the model with
* @param content
* String representation of the create a resource from
* @return {@link XtextResource} created
* @throws IOException
* may be thrown when trying to load the given content
*/
protected final XtextResource getResource(final URI uri, final String content) throws IOException {
StringInputStream instanceStream = new StringInputStream(content);
XtextResourceSet rs = getResourceSet();
XtextResource resource = (XtextResource) rs.createResource(uri);
rs.getResources().add(resource);
resource.load(instanceStream, null);
EcoreUtil.resolveAll(resource);
return resource;
}
use of org.eclipse.xtext.resource.XtextResourceSet in project dsl-devkit by dsldevkit.
the class AbstractFastLinkingService method getUsedGrammar.
/**
* Tries to find a grammar.
*
* @param resourceSet
* to use for loading
* @param grammarName
* qualified grammar name
* @return A singleton list containing the grammar, or an empty list if not found.
*/
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
// copied from XtextLinkingService#getUsedGrammar()
try {
if (grammarName != null) {
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null) {
rootElement = parseResult.getRootASTElement();
}
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource) {
resource.getContents();
}
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName())) {
return Collections.<EObject>singletonList(usedGrammar);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
return Collections.emptyList();
} catch (ValueConverterException e) {
return Collections.emptyList();
}
}
use of org.eclipse.xtext.resource.XtextResourceSet in project xtext-xtend by eclipse.
the class ActiveAnnotationsRuntimeTest method compileMacroResourceSet.
public XtextResourceSet compileMacroResourceSet(final Pair<String, String> macroFile, final Pair<String, String> clientFile) {
final URI macroURI = this.copyToDisk(this.macroProject, macroFile);
final URI clientURI = this.copyToDisk(this.clientProject, clientFile);
File _file = new File(this.workspaceRoot, this.macroProject);
FileProjectConfig _fileProjectConfig = new FileProjectConfig(_file);
final Procedure1<FileProjectConfig> _function = (FileProjectConfig it) -> {
it.addSourceFolder("src");
};
final FileProjectConfig macroProjectConfig = ObjectExtensions.<FileProjectConfig>operator_doubleArrow(_fileProjectConfig, _function);
File _file_1 = new File(this.workspaceRoot, this.clientProject);
FileProjectConfig _fileProjectConfig_1 = new FileProjectConfig(_file_1);
final Procedure1<FileProjectConfig> _function_1 = (FileProjectConfig it) -> {
it.addSourceFolder("src");
};
final FileProjectConfig clientProjectConfig = ObjectExtensions.<FileProjectConfig>operator_doubleArrow(_fileProjectConfig_1, _function_1);
final XtextResourceSet macroResourceSet = this.resourceSetProvider.get();
ProjectConfigAdapter.install(macroResourceSet, macroProjectConfig);
macroResourceSet.setClasspathURIContext(this.getClass().getClassLoader());
macroResourceSet.createResource(macroURI);
final XtextResourceSet resourceSet = this.resourceSetProvider.get();
ProjectConfigAdapter.install(resourceSet, clientProjectConfig);
resourceSet.createResource(clientURI);
final IAcceptor<CompilationTestHelper.Result> _function_2 = (CompilationTestHelper.Result result) -> {
ClassLoader _classLoader = this.getClass().getClassLoader();
final DelegatingClassloader classLoader = new DelegatingClassloader(_classLoader, result);
resourceSet.setClasspathURIContext(classLoader);
this.compiler.setJavaCompilerClassPath(classLoader);
};
this.compiler.compile(macroResourceSet, _function_2);
return resourceSet;
}
use of org.eclipse.xtext.resource.XtextResourceSet in project xtext-xtend by eclipse.
the class AbstractXtendTestCase method files.
protected Iterable<XtendFile> files(boolean validate, String... contents) throws Exception {
XtextResourceSet set = getResourceSet();
List<XtendFile> result = newArrayList();
for (String string : contents) {
String fileName = getFileName(string);
Resource resource = set.createResource(URI.createURI(fileName + ".xtend"));
resource.load(new StringInputStream(string), null);
assertEquals(resource.getErrors().toString(), 0, resource.getErrors().size());
}
for (Resource resource : new ArrayList<Resource>(set.getResources())) {
XtendFile file = (XtendFile) resource.getContents().get(0);
result.add(file);
}
if (validate) {
for (XtendFile file : result) {
List<Issue> issues = ((XtextResource) file.eResource()).getResourceServiceProvider().getResourceValidator().validate(file.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
}
}
return result;
}
Aggregations