private RefactoringStatus validate(IProgressMonitor pm, EObject object) { RefactoringStatus refactoringStatus = RefactoringStatus.create(Status.OK_STATUS); EValidator.Registry validatorRegistry = extensions.getInstance(EValidator.Registry.class, object); EPackage epackage = object.eClass().getEPackage(); EValidator validator = validatorRegistry.getEValidator(epackage); BasicDiagnostic diagnostics = new BasicDiagnostic(); if (pm.isCanceled()) { refactoringStatus = RefactoringStatus.create(Status.CANCEL_STATUS); pm.done(); } else { SubMonitor sm = SubMonitor.convert(pm, "Validating re-factoring.", IProgressMonitor.UNKNOWN); validator.validate(object, diagnostics, Maps.newHashMap()); Iterator<Diagnostic> validationIterator = Iterables.filter( diagnostics.getChildren(), new Predicate<Diagnostic>() { @Override public boolean apply(Diagnostic diagnostic) { if (diagnostic instanceof FeatureBasedDiagnostic) { return FeatureBasedDiagnostic.ERROR == ((FeatureBasedDiagnostic) diagnostic).getSeverity(); } return false; } }) .iterator(); if (validationIterator.hasNext()) { Diagnostic diagnostic = validationIterator.next(); refactoringStatus = RefactoringStatus.createFatalErrorStatus(diagnostic.getMessage()); } sm.done(); } return refactoringStatus; }
/** @since 2.3 */ protected void validateGrammar(Grammar grammar) { validateAllImports(grammar); EValidator validator = EValidator.Registry.INSTANCE.getEValidator(XtextPackage.eINSTANCE); if (validator != null) { DiagnosticChain chain = new DiagnosticChain() { @Override public void add(Diagnostic diagnostic) { if (diagnostic.getSeverity() == Diagnostic.ERROR) { if (diagnostic.getException() == null) throw new IllegalStateException(diagnostic.getMessage()); else throw new IllegalStateException( diagnostic.getMessage(), diagnostic.getException()); } } @Override public void addAll(Diagnostic diagnostic) { add(diagnostic); } @Override public void merge(Diagnostic diagnostic) { throw new UnsupportedOperationException(); } }; validator.validate(grammar, chain, null); TreeIterator<EObject> iterator = grammar.eAllContents(); while (iterator.hasNext()) validator.validate(iterator.next(), chain, new HashMap<Object, Object>()); } }
// BEGIN COMPLEX CODE public boolean validateKeyAttributeDomainModelReference_resolveable( VKeyAttributeDomainModelReference keyAttributeDomainModelReference, DiagnosticChain diagnostics, Map<Object, Object> context) { if (keyAttributeDomainModelReference.getKeyDMR() == null) { if (keyAttributeDomainModelReference.eContainer() != null && diagnostics != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, "Missing key DMR.", //$NON-NLS-1$ keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } return false; } if (keyAttributeDomainModelReference.getKeyValue() == null) { if (keyAttributeDomainModelReference.eContainer() != null && diagnostics != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, "No key Value.", //$NON-NLS-1$ keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } return false; } // validate path to reference if (!ViewValidator.INSTANCE.validateFeaturePathDomainModelReference_resolveable( keyAttributeDomainModelReference, diagnostics, context)) { return false; } final EStructuralFeature feature = keyAttributeDomainModelReference.getDomainModelEFeature(); if (!EReference.class.isInstance(feature) || !feature.isMany()) { if (diagnostics != null) { final String message = "Domain model reference does not end at a multi reference."; //$NON-NLS-1$ if (keyAttributeDomainModelReference.eContainer() != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, message, keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, message, keyAttributeDomainModelReference, VViewPackage.eINSTANCE.getFeaturePathDomainModelReference_DomainModelEFeature())); } return false; } final EClass rootEClass = EReference.class.cast(feature).getEReferenceType(); final VDomainModelReference targetDMR = keyAttributeDomainModelReference.getKeyDMR(); EValidator validator = EValidator.Registry.INSTANCE.getEValidator(targetDMR.eClass().getEPackage()); Map<Object, Object> newContext = new LinkedHashMap<Object, Object>(context); newContext.put(ViewValidator.ECLASS_KEY, rootEClass); if (!validator.validate(targetDMR, diagnostics, newContext)) { final String message = "Key DMR cannot be resolved"; // $NON-NLS-1$ if (keyAttributeDomainModelReference.eContainer() != null && diagnostics != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, message, keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } return false; } if (keyAttributeDomainModelReference.getValueDMR() == null) { if (keyAttributeDomainModelReference.eContainer() != null && diagnostics != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, "Missing value DMR", //$NON-NLS-1$ keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } return false; } final VDomainModelReference valueDMR = keyAttributeDomainModelReference.getValueDMR(); validator = EValidator.Registry.INSTANCE.getEValidator(valueDMR.eClass().getEPackage()); newContext = new LinkedHashMap<Object, Object>(context); newContext.put(ViewValidator.ECLASS_KEY, rootEClass); if (!validator.validate(valueDMR, diagnostics, newContext)) { final String message = "Value DMR cannot be resolved"; // $NON-NLS-1$ if (keyAttributeDomainModelReference.eContainer() != null && diagnostics != null) { diagnostics.add( createDiagnostic( Diagnostic.ERROR, 0, message, keyAttributeDomainModelReference.eContainer(), keyAttributeDomainModelReference.eContainingFeature())); } return false; } return true; }