/*******************************************************************************
 * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext.resource.generic;

import org.eclipse.emf.mwe2.runtime.workflow.IWorkflowComponent;
import org.eclipse.emf.mwe2.runtime.workflow.IWorkflowContext;
import org.eclipse.xtext.resource.FileExtensionProvider;
import org.eclipse.xtext.resource.IResourceServiceProvider;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * 
 * This class is intended to be used in an MWE workflow.
 * 
 * It instantiates and registers Xtext support for a non-Xtext based EMF resources, e.g. to reference model elements from
 * any Xtext language.
 * 
 * @author Sven Efftinge - Initial contribution and API
 * @author Jan Koehnlein 
 */
public abstract class AbstractGenericResourceSupport implements IWorkflowComponent {

	@Inject
	private FileExtensionProvider fileExtensionProvider;

	@Inject
	private IResourceServiceProvider resourceServiceProvider;

	@Inject
	private IResourceServiceProvider.Registry registry;

	public void preInvoke() {
		Injector injector = Guice.createInjector(getGuiceModule());
		injector.injectMembers(this);
		registerInRegistry();
	}

	private Module guiceModule;

	public void setGuiceModule(Module module) {
		this.guiceModule = module;
	}

	protected Module getGuiceModule() {
		if (guiceModule == null)
			guiceModule = createGuiceModule();
		return guiceModule;
	}

	protected abstract Module createGuiceModule();

	protected void registerInRegistry() {
		for(String fileExtension: fileExtensionProvider.getFileExtensions())
			registry.getExtensionToFactoryMap().put(fileExtension, resourceServiceProvider);
	}

	public void invoke(IWorkflowContext ctx) {
	}

	public void postInvoke() {
	}

}

@@ -514,16 +514,39 @@
 neric;%0A%0A
+import java.util.Map;%0A%0A
 import o
@@ -1530,16 +1530,259 @@
 oke() %7B%0A
+%09%09registerServices(true);%0A%09%7D%0A%0A%09/**%0A%09 * Inject members into this instance and register the services afterwards.%0A%09 * @see #getGuiceModule()%0A%09 * @see #registerInRegistry(boolean)%0A%09 * @since 2.1%0A%09 */%0A%09public void registerServices(boolean force) %7B%0A
 %09%09Inject
@@ -1888,16 +1888,21 @@
 egistry(
+force
 );%0A%09%7D%0A%0A%09
@@ -2190,45 +2190,396 @@
 ;%0A%0A%09
-protected void registerInRegistry() %7B
+/**%0A%09 * @deprecated use %7B@link #registerInRegistry(boolean)%7D instead.%0A%09 */%0A%09@Deprecated%0A%09protected void registerInRegistry() %7B%0A%09%09registerInRegistry(true);%0A%09%7D%0A%09%0A%09/**%0A%09 * @param force %3Ccode%3Etrue%3C/code%3E if you want to override existing service providers.%0A%09 * @since 2.1%0A%09 */%0A%09protected void registerInRegistry(boolean force) %7B%0A%09%09Map%3CString, Object%3E map = registry.getExtensionToFactoryMap();
 %0A%09%09f
@@ -2649,47 +2649,67 @@
 s())
+ %7B
 %0A%09%09%09
-registry.getExtensionToFactoryMap()
+if (force %7C%7C !map.containsKey(fileExtension))%0A%09%09%09%09map
 .put
@@ -2750,16 +2750,20 @@
 vider);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pub

Allow to populate the resource set with additional resources