Search in sources :

Example 31 with XtextResourceSet

use of org.eclipse.xtext.resource.XtextResourceSet in project xtext-core by eclipse.

the class AbstractXtextResourceSetTest method testResourcesAreClearedWithNormalizedURI_01.

@Test
public void testResourcesAreClearedWithNormalizedURI_01() {
    final XtextResourceSet rs = this.createEmptyResourceSet();
    Assert.assertEquals(0, rs.getURIResourceMap().size());
    final XtextResource resource = new XtextResource();
    resource.setURI(URI.createURI("/a/../foo"));
    EList<Resource> _resources = rs.getResources();
    ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
    Iterables.<Resource>addAll(_resources, _newArrayList);
    Assert.assertEquals(2, rs.getURIResourceMap().size());
    rs.getResources().clear();
    Assert.assertTrue(resource.eAdapters().isEmpty());
    Assert.assertEquals(0, rs.getURIResourceMap().size());
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) AbstractResourceSetTest(org.eclipse.xtext.resource.AbstractResourceSetTest) Test(org.junit.Test)

Example 32 with XtextResourceSet

use of org.eclipse.xtext.resource.XtextResourceSet in project xtext-core by eclipse.

the class AbstractXtextResourceSetTest method testResourcesAreInMapWithNormalizedURI_02.

@Test
public void testResourcesAreInMapWithNormalizedURI_02() {
    final XtextResourceSet rs = this.createEmptyResourceSet();
    Assert.assertEquals(0, rs.getURIResourceMap().size());
    final XtextResource resource = new XtextResource();
    resource.setURI(URI.createURI("/a/../foo"));
    EList<Resource> _resources = rs.getResources();
    ArrayList<Resource> _newArrayList = CollectionLiterals.<Resource>newArrayList(resource);
    Iterables.<Resource>addAll(_resources, _newArrayList);
    Assert.assertEquals(2, rs.getURIResourceMap().size());
    rs.getResources().remove(resource);
    Assert.assertTrue(resource.eAdapters().isEmpty());
    Assert.assertEquals(0, rs.getURIResourceMap().size());
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) AbstractResourceSetTest(org.eclipse.xtext.resource.AbstractResourceSetTest) Test(org.junit.Test)

Example 33 with XtextResourceSet

use of org.eclipse.xtext.resource.XtextResourceSet in project vorto by eclipse.

the class XtextResourceModelParser method parseModel.

@SuppressWarnings("unchecked")
private <M> ParseModelResult<M> parseModel(URI uri, Class<M> modelClass) {
    ResourceSet rs = new XtextResourceSet();
    Resource resource = rs.getResource(uri, true);
    if (!resource.getContents().isEmpty()) {
        Collection<Resource.Diagnostic> errorDiagnostics = Lists.newArrayList();
        EObject eModel = resource.getContents().get(0);
        // linking errors
        errorDiagnostics.addAll(getLinkingErrors(eModel));
        // syntax errors
        errorDiagnostics.addAll(Collections2.filter(resource.getErrors(), notXtextLinkingDiagnostics));
        return ParseModelResult.newResult(errorDiagnostics, (M) eModel);
    } else {
        return ParseModelResult.newResult(Collections2.filter(resource.getErrors(), notXtextLinkingDiagnostics), null);
    }
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.common.util.Diagnostic) XtextLinkingDiagnostic(org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet)

Example 34 with XtextResourceSet

use of org.eclipse.xtext.resource.XtextResourceSet in project smarthome by eclipse.

the class ScriptEngineImpl method parseScriptIntoXTextEObject.

private XExpression parseScriptIntoXTextEObject(String scriptAsString) throws ScriptParsingException {
    XtextResourceSet resourceSet = getResourceSet();
    // IS-A XtextResource
    Resource resource = resourceSet.createResource(computeUnusedUri(resourceSet));
    try {
        resource.load(new StringInputStream(scriptAsString, StandardCharsets.UTF_8.name()), resourceSet.getLoadOptions());
    } catch (IOException e) {
        throw new ScriptParsingException("Unexpected IOException; from close() of a String-based ByteArrayInputStream, no real I/O; how is that possible???", scriptAsString, e);
    }
    List<Diagnostic> errors = resource.getErrors();
    if (errors.size() != 0) {
        throw new ScriptParsingException("Failed to parse expression (due to managed SyntaxError/s)", scriptAsString).addDiagnosticErrors(errors);
    }
    EList<EObject> contents = resource.getContents();
    if (!contents.isEmpty()) {
        Iterable<Issue> validationErrors = getValidationErrors(contents.get(0));
        if (!validationErrors.iterator().hasNext()) {
            return (XExpression) contents.get(0);
        } else {
            throw new ScriptParsingException("Failed to parse expression (due to managed ValidationError/s)", scriptAsString).addValidationIssues(validationErrors);
        }
    } else {
        return null;
    }
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) ScriptParsingException(org.eclipse.smarthome.model.script.engine.ScriptParsingException) Issue(org.eclipse.xtext.validation.Issue) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XExpression(org.eclipse.xtext.xbase.XExpression) IOException(java.io.IOException)

Example 35 with XtextResourceSet

use of org.eclipse.xtext.resource.XtextResourceSet in project n4js by eclipse.

the class ManifestMerger method mergeContent.

/**
 * Merges the content of two {@link ProjectDescription project description} instances that are representing the
 * actual N4JS manifests.
 *
 * @param fromLocation
 *            the source location. These attributes and references will be merged to the other one given with the
 *            {@code toLocation}.
 * @param toLocation
 *            the target location. The project description that has to be updated with the content of the
 *            {@code fromLocation}.
 * @return the merged project description that has been detached from its resource.
 */
public ProjectDescription mergeContent(final URI fromLocation, final URI toLocation) {
    final XtextResourceSet fromResourceSet = getResourceSet(fromLocation);
    final XtextResourceSet toResourceSet = getResourceSet(toLocation);
    if (null == fromResourceSet || null == toResourceSet) {
        return null;
    }
    try {
        final Resource from = fromResourceSet.getResource(fromLocation, true);
        final Resource to = toResourceSet.getResource(toLocation, true);
        return mergeContent(from, to);
    } catch (final Exception e) {
        LOGGER.error("Error while trying to merge N4JS manifest content. Source URI: " + fromLocation + ". Target URI: " + toLocation + ".", e);
    }
    return null;
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Resource(org.eclipse.emf.ecore.resource.Resource)

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