Search in sources :

Example 1 with GeneratorDelegate

use of org.eclipse.xtext.generator.GeneratorDelegate in project n4js by eclipse.

the class XStatefulIncrementalBuilder method generate.

/**
 * Generate code for the given resource
 */
protected void generate(Resource resource, XSource2GeneratedMapping newMappings, IResourceServiceProvider serviceProvider) {
    GeneratorDelegate generator = serviceProvider.get(GeneratorDelegate.class);
    if (generator == null) {
        return;
    }
    if (isResourceInOutputDirectory(resource, serviceProvider)) {
        return;
    }
    URI source = resource.getURI();
    Set<URI> previous = newMappings.deleteSource(source);
    URIBasedFileSystemAccess fileSystemAccess = this.createFileSystemAccess(serviceProvider, resource);
    fileSystemAccess.setBeforeWrite((uri, outputCfgName, contents) -> {
        newMappings.addSource2Generated(source, uri, outputCfgName);
        previous.remove(uri);
        request.afterGenerate(source, uri);
        return contents;
    });
    fileSystemAccess.setBeforeDelete((uri) -> {
        newMappings.deleteGenerated(uri);
        request.afterDelete(uri);
        return true;
    });
    fileSystemAccess.setContext(resource);
    if (request.isWriteStorageResources() && resource instanceof StorageAwareResource) {
        IResourceStorageFacade resourceStorageFacade = ((StorageAwareResource) resource).getResourceStorageFacade();
        if (resourceStorageFacade != null) {
            resourceStorageFacade.saveResource((StorageAwareResource) resource, fileSystemAccess);
        }
    }
    if (request.canGenerate()) {
        GeneratorContext generatorContext = new GeneratorContext();
        generatorContext.setCancelIndicator(request.getCancelIndicator());
        generator.generate(resource, fileSystemAccess, generatorContext);
        XtextResourceSet resourceSet = request.getResourceSet();
        for (URI noLongerCreated : previous) {
            try {
                resourceSet.getURIConverter().delete(noLongerCreated, CollectionLiterals.emptyMap());
                request.afterDelete(noLongerCreated);
            } catch (IOException e) {
                Exceptions.sneakyThrow(e);
            }
        }
    }
}
Also used : GeneratorDelegate(org.eclipse.xtext.generator.GeneratorDelegate) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IResourceStorageFacade(org.eclipse.xtext.resource.persistence.IResourceStorageFacade) URIBasedFileSystemAccess(org.eclipse.xtext.generator.URIBasedFileSystemAccess) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) IOException(java.io.IOException) GeneratorContext(org.eclipse.xtext.generator.GeneratorContext) URI(org.eclipse.emf.common.util.URI)

Example 2 with GeneratorDelegate

use of org.eclipse.xtext.generator.GeneratorDelegate in project ArchCNL by Mari-Wie.

the class CNL2OWLGenerator method transformCNLFile.

/**
 * Translates the given file from ArchCNL's CNL to OWL. The resulting OWL ontology is written to
 * the specified output file.
 *
 * @param path The path of the file to transform.
 * @param outputPath The path of the output file.
 * @return The RuleType of the parsed rule or null when something went wrong.
 * @throws IOException
 */
public RuleType transformCNLFile(String path, String outputPath) throws IOException {
    LOG.trace("transforming CNL file with path: " + path);
    RuleTypeStorageSingleton.getInstance().setOutputFile(outputPath);
    ArchcnlStandaloneSetup setup = new ArchcnlStandaloneSetup();
    Injector injector = setup.createInjectorAndDoEMFRegistration();
    XtextResourceSet set = injector.getInstance(XtextResourceSet.class);
    // load a resource by URI, in this case from the file system
    URI uri = URI.createFileURI(path);
    LOG.trace("uri: " + uri);
    LOG.debug("Reading CNL file: " + path);
    Resource resource = set.getResource(uri, true);
    IResourceValidator validator = ((XtextResource) resource).getResourceServiceProvider().getResourceValidator();
    List<Issue> issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
    for (Issue issue : issues) {
        LOG.error(issue.getMessage());
    }
    GeneratorDelegate generator = injector.getInstance(GeneratorDelegate.class);
    // fsa wird nur als leer Hülle  für den Aufruf erstellt, aber nicht weiter verwendet.
    InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
    LOG.trace("resource: " + resource);
    try {
        generator.doGenerate(resource, fsa);
    } catch (UncheckedIOException e) {
        throw new IOException("Failure while generating", e);
    }
    RuleType ruleType = RuleTypeStorageSingleton.getInstance().retrieveTypeOfRule(id);
    id++;
    return ruleType;
}
Also used : Issue(org.eclipse.xtext.validation.Issue) GeneratorDelegate(org.eclipse.xtext.generator.GeneratorDelegate) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) InMemoryFileSystemAccess(org.eclipse.xtext.generator.InMemoryFileSystemAccess) UncheckedIOException(java.io.UncheckedIOException) RuleType(org.archcnl.common.datatypes.RuleType) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(org.eclipse.emf.common.util.URI) Injector(com.google.inject.Injector) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet)

Aggregations

IOException (java.io.IOException)2 URI (org.eclipse.emf.common.util.URI)2 GeneratorDelegate (org.eclipse.xtext.generator.GeneratorDelegate)2 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)2 Injector (com.google.inject.Injector)1 UncheckedIOException (java.io.UncheckedIOException)1 RuleType (org.archcnl.common.datatypes.RuleType)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 GeneratorContext (org.eclipse.xtext.generator.GeneratorContext)1 InMemoryFileSystemAccess (org.eclipse.xtext.generator.InMemoryFileSystemAccess)1 URIBasedFileSystemAccess (org.eclipse.xtext.generator.URIBasedFileSystemAccess)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IResourceStorageFacade (org.eclipse.xtext.resource.persistence.IResourceStorageFacade)1 StorageAwareResource (org.eclipse.xtext.resource.persistence.StorageAwareResource)1 IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)1 Issue (org.eclipse.xtext.validation.Issue)1