Commit f2dfccff authored by Martti Käärik's avatar Martti Käärik
Browse files

Interpreted validator registered at activation exactly once in a composite

parent 6b867a01
Loading
Loading
Loading
Loading
+34 −47
Changes for plugins/org.etsi.mts.tdl.model/src/org/etsi/mts/tdl/Validator.java: 34 added lines, 47 removed lines.
Original line number Diff line number Diff line
@@ -89,69 +89,56 @@ public class Validator {
	}

	public static void removeValidator(EPackage ePackage){
		EValidator existingValidator = EValidator.Registry.INSTANCE.getEValidator(ePackage);
		if (existingValidator instanceof CompositeEValidator) {
			((CompositeEValidator) existingValidator).getContents().removeIf(e -> e.getDelegate() instanceof CompleteOCLEObjectValidator);
		}
		
		if (true) return;
		EValidator existing = EValidator.Registry.INSTANCE.getEValidator(ePackage);
		if (existing instanceof CompositeEValidator) {
			// an Xtext injector has wrapped the entry: take the interpreter out, keep the language validators
			((CompositeEValidator) existing).getContents().removeIf(e -> e.getDelegate() instanceof CompleteOCLEObjectValidator);
		} else if (existing instanceof CompleteOCLEObjectValidator) {
			EValidator.Registry.INSTANCE.remove(ePackage);
		}
		validator = null;
	}

	
	public static void registerValidator(String path, EPackage ePackage, BundleContext context) {
//		EValidator.Registry.INSTANCE.put(null, new OCLinEcoreEObjectValidator());
//		EValidator.Registry.INSTANCE.put(ePackage, tdlValidator.INSTANCE);

		//TODO: currently nukes all other registered validators (e.g. Xtext one), better to add and remove as needed instead.. 

		EValidator existingValidator = EValidator.Registry.INSTANCE.getEValidator(ePackage);
		if (existingValidator instanceof CompositeEValidator) {
		    Path oclPath = new Path(path);
			URL oclURL = FileLocator.find(context.getBundle(), oclPath);

			URI oclURI;
		URL oclURL = FileLocator.find(context.getBundle(), new Path(path));
		try {
				oclURI = URI.createURI(oclURL.toURI().toString());
				CompleteOCLEObjectValidator validator = createValidator(ePackage, oclURI);
				
				((CompositeEValidator) existingValidator).addValidator(validator);
				//EValidator.Registry.INSTANCE.put(ePackage, validator);
			install(ePackage, createValidator(ePackage, URI.createURI(oclURL.toURI().toString())));
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
		}
			//.add(evlValidator);
		} else {
//		    if (existingValidator == null) {
//		        existingValidator = EObjectValidator.INSTANCE;
//		    }
//		    CompositeEValidator newValidator = new CompositeEValidator();
//		    newValidator.getDelegates().add(existingValidator);
//		    newValidator.getDelegates().add(evlValidator);
//		    EValidator.Registry.INSTANCE.put(ePackage, newValidator);
	}

	public static void registerValidator(String path, EPackage ePackage) {
		URI uri = URI.createURI("platform:/plugin/org.etsi.mts.tdl.model/"+path);
		install(ePackage, Validator.createValidator(ePackage, uri));
	}

		if (true) return;
		Path oclPath = new Path(path);
		URL oclURL = FileLocator.find(context.getBundle(), oclPath);

		URI oclURI;
		try {
			oclURI = URI.createURI(oclURL.toURI().toString());
			CompleteOCLEObjectValidator validator = createValidator(ePackage, oclURI);
			
			EValidator.Registry.INSTANCE.put(ePackage, validator);
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
	/**
	 * Installs the interpreting validator for a package depending on:
	 * <ul>
	 * <li>no Xtext injector yet and the entry is the generated validator - replace
	 * and Xtext later wraps it together with the language validators.</li>
	 * <li>injector has already wrapped the entry into a {@link CompositeEValidator}
	 * - the generated validator and any earlier interpreter are taken out of the
	 * composite and the new interpreter is added.</li>
	 * </ul>
	 */
	private static void install(EPackage ePackage, CompleteOCLEObjectValidator interpreter) {
		EValidator existing = EValidator.Registry.INSTANCE.getEValidator(ePackage);
		if (existing instanceof CompositeEValidator) {
			CompositeEValidator composite = (CompositeEValidator) existing;
			composite.getContents().removeIf(e -> e.getDelegate() instanceof CompleteOCLEObjectValidator || isGenerated(e.getDelegate()));
			composite.addValidator(interpreter);
		} else {
			EValidator.Registry.INSTANCE.put(ePackage, interpreter);
		}
	}

	public static void registerValidator(String path, EPackage ePackage) {
		URI uri = URI.createURI("platform:/plugin/org.etsi.mts.tdl.model/"+path);
		CompleteOCLEObjectValidator validator = Validator.createValidator(ePackage, uri);
		EValidator.Registry.INSTANCE.put(ePackage, validator);
	private static boolean isGenerated(EValidator v) {
		return v instanceof org.etsi.mts.tdl.util.tdlValidator
				|| v instanceof org.etsi.mts.tdl.structuredobjectives.util.StructuredObjectivesValidator
				|| v instanceof org.etsi.mts.tdl.extendedconfigurations.util.ExtendedConfigurationsValidator;
	}
	
	public static CompleteOCLEObjectValidator createValidator(EPackage ePakcage, URI oclURI) {