Search in sources :

Example 41 with XtextResourceSet

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);
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IModelLocation(com.avaloq.tools.ddk.check.runtime.configuration.IModelLocation) ModelLocation(com.avaloq.tools.ddk.check.runtime.configuration.ModelLocation) URL(java.net.URL) CatalogFromExtensionPointScope(com.avaloq.tools.ddk.check.scoping.CatalogFromExtensionPointScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 42 with XtextResourceSet

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;
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 43 with XtextResourceSet

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();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Grammar(org.eclipse.xtext.Grammar) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 44 with XtextResourceSet

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;
}
Also used : CompilationTestHelper(org.eclipse.xtext.xbase.testing.CompilationTestHelper) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) FileProjectConfig(org.eclipse.xtext.workspace.FileProjectConfig) URI(org.eclipse.emf.common.util.URI) XtendFile(org.eclipse.xtend.core.xtend.XtendFile) File(java.io.File)

Example 45 with XtextResourceSet

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;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) StringInputStream(org.eclipse.xtext.util.StringInputStream) Issue(org.eclipse.xtext.validation.Issue) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) XtextResource(org.eclipse.xtext.resource.XtextResource)

Aggregations

XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)177 Resource (org.eclipse.emf.ecore.resource.Resource)81 XtextResource (org.eclipse.xtext.resource.XtextResource)77 Test (org.junit.Test)57 StringInputStream (org.eclipse.xtext.util.StringInputStream)42 URI (org.eclipse.emf.common.util.URI)33 EObject (org.eclipse.emf.ecore.EObject)20 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)19 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)19 Grammar (org.eclipse.xtext.Grammar)10 Issue (org.eclipse.xtext.validation.Issue)10 IJavaProject (org.eclipse.jdt.core.IJavaProject)8 File (java.io.File)7 ArrayList (java.util.ArrayList)7 EList (org.eclipse.emf.common.util.EList)7 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)7 Before (org.junit.Before)7 Injector (com.google.inject.Injector)6 IOException (java.io.IOException)6 Diagnostic (org.eclipse.emf.common.util.Diagnostic)5