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);
}
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;
}
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));
}
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));
}
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;
}
}
Aggregations