/** * Gets the language for the given object. * * @param context * object * @return the language the corresponding resource was parsed from, may be {@code null} */ private String getLanguage(final Object context) { if (context instanceof EObject) { Resource resource = ((EObject) context).eResource(); if (resource instanceof LazyLinkingResource) { return ((LazyLinkingResource) resource).getLanguageName(); } } else if (context instanceof Issue) { URI uri = ((Issue) context).getUriToProblem(); if (uri != null) { Registry registry = IResourceServiceProvider.Registry.INSTANCE; IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(uri); if (resourceServiceProvider != null) { return resourceServiceProvider.get(Injector.class).getInstance(Key.get(String.class, Names.named(Constants.LANGUAGE_NAME))); } else { LOGGER.error("Could not fetch a ResourceServiceProvider for URI: " + uri); //$NON-NLS-1$ } } else { LOGGER.warn("Could not fetch eResource from issue: URI to problem is null"); //$NON-NLS-1$ } } return null; }
/** * Gets the all languages available in the workbench. * * @return set of all languages */ public Set<String> getAllLanguages() { Set<String> languages = new HashSet<String>(); for (String extension : Registry.INSTANCE.getExtensionToFactoryMap().keySet()) { final URI dummyUri = URI.createURI("foo:/foo." + extension); IResourceServiceProvider resourceServiceProvider = Registry.INSTANCE.getResourceServiceProvider(dummyUri); // By checking that description manager is AbstractCachingResourceDescriptionManager we exclude technical languages of the framework if (resourceServiceProvider != null && resourceServiceProvider.getResourceDescriptionManager() instanceof AbstractCachingResourceDescriptionManager) { try { IGrammarAccess grammarAccess = resourceServiceProvider.get(IGrammarAccess.class); if (grammarAccess != null && grammarAccess.getGrammar() != null) { languages.add(grammarAccess.getGrammar().getName()); } } catch (ConfigurationException e) { // Will happen if no binding for IGrammarAccess was present. } } } return languages; }
public static void main(String[] args) { SadlStandaloneSetup.doSetup(); SadlStandaloneSetup setup = new SadlStandaloneSetup(); Injector injector = setup.createInjector(); ResourceSet resourceSet = injector.getInstance(ResourceSet.class); resourceSet.setResourceFactoryRegistry(Resource.Factory.Registry.INSTANCE); URI uri = URI.createFileURI("resources/shape-rules.sadl"); Assert.assertEquals("sadl", uri.fileExtension()); Resource resource = resourceSet.getResource(uri, true); Validator validator = new Validator(); Issues issues = new IssuesImpl(); Registry registry = IResourceServiceProvider.Registry.INSTANCE; validator.validate(resourceSet, registry, issues); Assert.assertFalse(issues.hasErrors()); int i=0; }
public void validate(ResourceSet resourceSet, IResourceServiceProvider.Registry registry, Issues issues) { List<Resource> resources = Lists.newArrayList(resourceSet.getResources()); for (Resource resource : resources) { try { resource.load(null); IResourceServiceProvider provider = registry.getResourceServiceProvider(resource.getURI()); if (provider != null) { List<Issue> result = provider.getResourceValidator().validate(resource, CheckMode.ALL, null); for (Issue issue : result) { switch (issue.getSeverity()) { case ERROR: issues.addError(issue.getMessage(), issue); break; case WARNING: issues.addWarning(issue.getMessage(), issue); break; case INFO: issues.addInfo(issue.getMessage(), issue); break; case IGNORE: break; } } } } catch (IOException e) { throw new WorkflowInterruptedException("Couldn't load resource (" + resource.getURI() + ")", e); } } if (isStopOnError() && issues.hasErrors()) { String errorMessage = toString(issues); throw new WorkflowInterruptedException("Validation problems: \n" + errorMessage); } }
@Override public void validate(ResourceSet resourceSet, Registry registry, Issues issues) { // do nothing }
public ReferenceDescriptionAcceptor(IResourceServiceProvider.Registry resourceServiceProviderRegistry, StatusWrapper status) { this.resourceServiceProviderRegistry = resourceServiceProviderRegistry; this.status = status; }
/** * Instantiates a new marker update job in which all sources identified by given * set of URIs are validated. * * @param name * the name of the job * @param uris * the set of URIs to be validated */ public CheckMarkerUpdateJob(final String name, final Set<URI> uris) { super(name); this.serviceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE; this.uris = uris; this.checkMode = CheckMode.ALL; }