Search in sources :

Example 1 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project xtext-eclipse by eclipse.

the class NoJRESmokeTester method checkNoErrorsInValidator.

protected void checkNoErrorsInValidator(final String model, XtextResource resource) {
    ResourceValidatorImpl validator = resourceValidatorProvider.get();
    Assert.assertNotSame(validator, resource.getResourceServiceProvider().getResourceValidator());
    validator.setDiagnosticConverter(new IDiagnosticConverter() {

        @Override
        public void convertValidatorDiagnostic(org.eclipse.emf.common.util.Diagnostic diagnostic, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof BasicDiagnostic) {
                List<?> data = diagnostic.getData();
                if (!data.isEmpty() && data.get(0) instanceof Throwable) {
                    Throwable t = (Throwable) data.get(0);
                    throwError(t);
                }
                if (EObjectValidator.DIAGNOSTIC_SOURCE.equals(diagnostic.getSource()) && diagnostic.getCode() == EObjectValidator.EOBJECT__EVERY_REFERENCE_IS_CONTAINED) {
                    throwError(new RuntimeException("Dangling reference found."));
                }
            }
        }

        private void throwError(Throwable e) {
            ComparisonFailure result = new ComparisonFailure(e.getMessage(), model, "");
            result.setStackTrace(e.getStackTrace());
            throw result;
        }

        @Override
        public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
            if (diagnostic instanceof ExceptionDiagnostic) {
                Exception e = ((ExceptionDiagnostic) diagnostic).getException();
                throwError(e);
            }
        }
    });
    validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
}
Also used : Issue(org.eclipse.xtext.validation.Issue) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Severity(org.eclipse.xtext.diagnostics.Severity) ResourceValidatorImpl(org.eclipse.xtext.validation.ResourceValidatorImpl) IDiagnosticConverter(org.eclipse.xtext.validation.IDiagnosticConverter) ComparisonFailure(org.junit.ComparisonFailure) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) List(java.util.List)

Example 2 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project archi by archimatetool.

the class EditorModelManager method loadModel.

@Override
public IArchimateModel loadModel(File file) {
    if (file == null || !file.exists()) {
        return null;
    }
    // If it is already loaded return it
    IArchimateModel model = locateLoadedModel(file);
    if (model != null) {
        return model;
    }
    // Ascertain if this is an archive file
    boolean useArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
    // Create the Resource
    Resource resource = ArchimateResourceFactory.createNewResource(useArchiveFormat ? IArchiveManager.FACTORY.createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
    // Check model compatibility
    ModelCompatibility modelCompatibility = new ModelCompatibility(resource);
    // Load the model file
    try {
        resource.load(null);
    } catch (IOException ex) {
        // Error occured loading model.
        try {
            modelCompatibility.checkErrors();
        } catch (IncompatibleModelException ex1) {
            // Was it a disaster?
            MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_2, NLS.bind(Messages.EditorModelManager_3, file) + "\n" + // $NON-NLS-1$
            ex1.getMessage());
            return null;
        }
    }
    model = (IArchimateModel) resource.getContents().get(0);
    // Once loaded - check for later model version
    boolean isLaterModelVersion = modelCompatibility.isLaterModelVersion(ModelVersion.VERSION);
    if (isLaterModelVersion) {
        boolean answer = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_4, NLS.bind(Messages.EditorModelManager_5, file, model.getVersion()));
        if (!answer) {
            return null;
        }
    } else // Check for unknown model features which might be OK to load
    {
        List<Diagnostic> exceptions = modelCompatibility.getAcceptableExceptions();
        if (!exceptions.isEmpty()) {
            // $NON-NLS-1$
            String message = "";
            for (int i = 0; i < exceptions.size(); i++) {
                if (i == 3) {
                    // $NON-NLS-1$
                    message += (exceptions.size() - 3) + " " + Messages.EditorModelManager_12;
                    break;
                }
                // $NON-NLS-1$
                message += exceptions.get(i).getMessage() + "\n";
            }
            boolean answer = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_4, NLS.bind(Messages.EditorModelManager_13, file) + "\n\n" + // $NON-NLS-1$
            message);
            if (!answer) {
                return null;
            }
        }
    }
    // And then fix any backward compatibility issues
    try {
        modelCompatibility.fixCompatibility();
    } catch (CompatibilityHandlerException ex) {
    }
    model.setFile(file);
    model.setDefaults();
    getModels().add(model);
    model.eAdapters().add(new ECoreAdapter());
    // New Command Stack
    createNewCommandStack(model);
    // New Archive Manager
    createNewArchiveManager(model);
    // Initiate all diagram models to be marked as "saved" - this is for the editor view persistence
    markDiagramModelsAsSaved(model);
    // This last
    firePropertyChange(this, PROPERTY_MODEL_LOADED, null, model);
    return model;
}
Also used : ModelCompatibility(com.archimatetool.editor.model.compatibility.ModelCompatibility) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) CompatibilityHandlerException(com.archimatetool.editor.model.compatibility.CompatibilityHandlerException) IOException(java.io.IOException) IArchimateModel(com.archimatetool.model.IArchimateModel) IncompatibleModelException(com.archimatetool.editor.model.compatibility.IncompatibleModelException)

Example 3 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project archi by archimatetool.

the class ModelCompatibilityTests method testIsCatastrophicException.

@Test
public void testIsCatastrophicException() {
    createResource(file2);
    assertEquals(2, resource.getErrors().size());
    Diagnostic diagnostic = resource.getErrors().get(0);
    assertTrue(mc.isCatastrophicException(diagnostic));
    diagnostic = resource.getErrors().get(1);
    assertTrue(mc.isCatastrophicException(diagnostic));
}
Also used : Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) Test(org.junit.Test)

Example 4 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project archi by archimatetool.

the class ModelCompatibilityTests method testIsFeatureNotFoundException.

@Test
public void testIsFeatureNotFoundException() {
    createResource(file1);
    assertEquals(2, resource.getErrors().size());
    Diagnostic diagnostic = resource.getErrors().get(0);
    assertTrue(mc.isFeatureNotFoundException(diagnostic));
    diagnostic = resource.getErrors().get(1);
    assertTrue(mc.isFeatureNotFoundException(diagnostic));
}
Also used : Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) Test(org.junit.Test)

Example 5 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic 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)

Aggregations

Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)32 Test (org.junit.Test)12 ExceptionDiagnostic (org.eclipse.xtext.diagnostics.ExceptionDiagnostic)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 XtextResource (org.eclipse.xtext.resource.XtextResource)8 XtextLinkingDiagnostic (org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic)7 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)6 Issue (org.eclipse.xtext.validation.Issue)6 List (java.util.List)5 EObject (org.eclipse.emf.ecore.EObject)5 Severity (org.eclipse.xtext.diagnostics.Severity)5 IDiagnosticConverter (org.eclipse.xtext.validation.IDiagnosticConverter)5 ResourceValidatorImpl (org.eclipse.xtext.validation.ResourceValidatorImpl)5 IOException (java.io.IOException)4 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)3 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)3 ComparisonFailure (org.junit.ComparisonFailure)3 ArrayList (java.util.ArrayList)2 URI (org.eclipse.emf.common.util.URI)2