org.eclipse.xtext.generator.GeneratorContext Java Examples

The following examples show how to use org.eclipse.xtext.generator.GeneratorContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From balzac with Apache License 2.0 6 votes vote down vote up
protected void runGenerator(String string) {
    // Load the resource
    ResourceSet set = resourceSetProvider.get();
    Resource resource = set.getResource(URI.createFileURI(string), true);

    // Validate the resource
    List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
    if (!list.isEmpty()) {
        for (Issue issue : list) {
            System.err.println(issue);
        }
        return;
    }

    // Configure and start the generator
    fileAccess.setOutputPath("src-gen/");
    GeneratorContext context = new GeneratorContext();
    context.setCancelIndicator(CancelIndicator.NullImpl);
    generator.generate(resource, fileAccess, context);

    System.out.println("Code generation finished.");
}
 
Example #2
Source File: StandaloneBuilder.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void generate(List<Resource> sourceResources) {
	GeneratorContext context = new GeneratorContext();
	context.setCancelIndicator(CancelIndicator.NullImpl);
	for (Resource it : sourceResources) {
		LOG.info("Starting generator for input: '" + it.getURI().lastSegment() + "'");
		registerCurrentSource(it.getURI());
		LanguageAccess access = languageAccess(it.getURI());
		JavaIoFileSystemAccess fileSystemAccess = getFileSystemAccess(access);
		if (isWriteStorageResources()) {
			if (it instanceof StorageAwareResource) {
				IResourceStorageFacade resourceStorageFacade = ((StorageAwareResource) it)
						.getResourceStorageFacade();
				if (resourceStorageFacade != null) {
					resourceStorageFacade.saveResource((StorageAwareResource) it, fileSystemAccess);
				}
			}
		}
		access.getGenerator().generate(it, fileSystemAccess, context);
	}
}
 
Example #3
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void generateJavaFiles(ResourceSet resourceSet) {
	JavaIoFileSystemAccess javaIoFileSystemAccess = javaIoFileSystemAccessProvider.get();
	javaIoFileSystemAccess.setOutputPath(outputPath);
	javaIoFileSystemAccess.setWriteTrace(writeTraceFiles);

	GeneratorContext context = new GeneratorContext();
	context.setCancelIndicator(CancelIndicator.NullImpl);
	for (Resource resource : newArrayList(resourceSet.getResources())) {
		if (isSourceFile(resource)) {
			if (isWriteStorageFiles()) {
				StorageAwareResource storageAwareResource = (StorageAwareResource)resource;
				storageAwareResource.getResourceStorageFacade().saveResource(storageAwareResource, javaIoFileSystemAccess);
			}
			generator.generate(resource, javaIoFileSystemAccess, context);
		}
	}
}
 
Example #4
Source File: CompilationTestHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void doGenerate() {
	if (access == null) {
		doValidation();
		access = fileSystemAccessProvider.get();
		
		access.setOutputConfigurations(outputConfigurations);
		for (Resource resource : sources) {
			if (resource instanceof XtextResource) {
				access.setProjectName(PROJECT_NAME);
				XtextResource xtextResource = (XtextResource) resource;
				IResourceServiceProvider resourceServiceProvider = xtextResource.getResourceServiceProvider();
				GeneratorDelegate generator = resourceServiceProvider.get(GeneratorDelegate.class);
				if (generator != null) {
					GeneratorContext context = new GeneratorContext();
					context.setCancelIndicator(CancelIndicator.NullImpl);
					generator.generate(xtextResource, access, context);
				}
			}
		}
		generatedCode = newHashMap();
		for (final GeneratedFile e : access.getGeneratedFiles()) {
			if (e.getJavaClassName() != null) {
				generatedCode.put(e.getJavaClassName(), e.getContents().toString());
			}
		}
	}
}
 
Example #5
Source File: JavaProjectBasedBuilderParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void handleChangedContents(Delta delta, IBuildContext context, IFileSystemAccess fileSystemAccess) {
	if (!resourceServiceProvider.canHandle(delta.getUri()))
		return;
	Resource resource = context.getResourceSet().getResource(delta.getUri(), true);
	if (shouldGenerate(resource, context)) {
		CancelIndicator cancelIndicator = CancelIndicator.NullImpl;
		if (fileSystemAccess instanceof EclipseResourceFileSystemAccess2) {
			cancelIndicator = new MonitorBasedCancelIndicator(((EclipseResourceFileSystemAccess2) fileSystemAccess).getMonitor());
		}
		GeneratorContext generatorContext = new GeneratorContext();
		generatorContext.setCancelIndicator(cancelIndicator);
		generator.generate(resource, (IFileSystemAccess2) fileSystemAccess, generatorContext);
		context.needRebuild();
	}
}
 
Example #6
Source File: CompilationTestHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void doGenerate() {
	if (access == null) {
		doValidation();
		access = fileSystemAccessProvider.get();
		
		access.setOutputConfigurations(outputConfigurations);
		for (Resource resource : sources) {
			if (resource instanceof XtextResource) {
				access.setProjectName(PROJECT_NAME);
				XtextResource xtextResource = (XtextResource) resource;
				IResourceServiceProvider resourceServiceProvider = xtextResource.getResourceServiceProvider();
				GeneratorDelegate generator = resourceServiceProvider.get(GeneratorDelegate.class);
				if (generator != null) {
					GeneratorContext context = new GeneratorContext();
					context.setCancelIndicator(CancelIndicator.NullImpl);
					generator.generate(xtextResource, access, context);
				}
			}
		}
		generatedCode = newHashMap();
		for (final GeneratedFile e : access.getGeneratedFiles()) {
			if (e.getJavaClassName() != null) {
				generatedCode.put(e.getJavaClassName(), e.getContents().toString());
			}
		}
	}
}
 
Example #7
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public final void doGenerate(Resource input, IFileSystemAccess fsa) {
	final IFileSystemAccess2 casted = (IFileSystemAccess2) fsa;
	final GeneratorContext context = new GeneratorContext();
	try {
		beforeGenerate(input, casted, context);
		doGenerate(input, casted, context);
	} finally {
		afterGenerate(input, casted, context);
	}
}
 
Example #8
Source File: XStatefulIncrementalBuilder.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/** 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.setResultGeneratedFile(source, uri);
		return contents;
	});
	fileSystemAccess.setBeforeDelete((uri) -> {
		newMappings.deleteGenerated(uri);
		request.setResultDeleteFile(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.setResultDeleteFile(noLongerCreated);
			} catch (IOException e) {
				Exceptions.sneakyThrow(e);
			}
		}
	}
}
 
Example #9
Source File: ParallelBuilderParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public IGeneratorContext getGeneratorContext() {
	CancelIndicator cancelIndicator = new MonitorBasedCancelIndicator(progressMonitor);
	GeneratorContext context = new GeneratorContext();
	context.setCancelIndicator(cancelIndicator);
	return context;
}
 
Example #10
Source File: IncrementalBuilder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Generate code for the given resource
 */
protected void generate(Resource resource, BuildRequest request, Source2GeneratedMapping newMappings) {
	IResourceServiceProvider serviceProvider = getResourceServiceProvider(resource);
	GeneratorDelegate generator = serviceProvider.get(GeneratorDelegate.class);
	if (generator == null) {
		return;
	}
	Set<URI> previous = newMappings.deleteSource(resource.getURI());
	URIBasedFileSystemAccess fileSystemAccess = createFileSystemAccess(serviceProvider, resource);
	fileSystemAccess.setBeforeWrite((uri, outputCfgName, contents) -> {
		newMappings.addSource2Generated(resource.getURI(), uri, outputCfgName);
		previous.remove(uri);
		request.getAfterGenerateFile().apply(resource.getURI(), uri);
		return contents;
	});
	fileSystemAccess.setBeforeDelete((uri) -> {
		newMappings.deleteGenerated(uri);
		request.getAfterDeleteFile().apply(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);
		}
	}
	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, Collections.emptyMap());
			request.getAfterDeleteFile().apply(noLongerCreated);
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
	}
}