/*******************************************************************************
 * Copyright (c) 2009 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.impl;

import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ContentHandler;
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl;
import org.eclipse.xtext.resource.IResourceServiceProvider;

import com.google.inject.Provider;

/**
 * @author Sebastian Zarnekow - Initial contribution and API
 * @author Sven Efftinge
 */
public class ResourceServiceProviderRegistryImpl implements IResourceServiceProvider.Registry {

	/**
	 * @author Sven Efftinge - Initial contribution and API
	 */
	public static class InternalData extends ResourceFactoryRegistryImpl {
		@SuppressWarnings({ "unchecked", "rawtypes" })
		public IResourceServiceProvider getServiceProvider(URI uri, String contentType) {
			Object object = getFactory(uri, protocolToFactoryMap, extensionToFactoryMap, contentTypeIdentifierToFactoryMap, contentType, false);
			if (object instanceof Provider) {
				return ((Provider<IResourceServiceProvider>)object).get();
			}
			return (IResourceServiceProvider) object;
		}
	}

	private final InternalData data = new InternalData();

	public IResourceServiceProvider getResourceServiceProvider(URI uri, String contentType) {
		if (uri==null)
			return null;
		return data.getServiceProvider(uri, contentType);
	}

	public Map<String, Object> getContentTypeToFactoryMap() {
		return data.getContentTypeToFactoryMap();
	}

	public Map<String, Object> getExtensionToFactoryMap() {
		return data.getExtensionToFactoryMap();
	}

	public Map<String, Object> getProtocolToFactoryMap() {
		return data.getProtocolToFactoryMap();
	}

	public IResourceServiceProvider getResourceServiceProvider(URI uri) {
		return getResourceServiceProvider(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
	}
	
	
	
}

@@ -511,16 +511,39 @@
 .impl;%0A%0A
+import java.util.List;%0A
 import j
@@ -651,16 +651,189 @@
 andler;%0A
+import org.eclipse.emf.ecore.resource.URIConverter;%0Aimport org.eclipse.emf.ecore.resource.URIHandler;%0Aimport org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;%0A
 import o
@@ -957,16 +957,56 @@
 vider;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -1363,16 +1363,17 @@
 yImpl %7B%0A
+%0A
 %09%09@Suppr
@@ -1568,33 +1568,38 @@
 ionToFactoryMap,
- 
+%0A%09%09%09%09%09
 contentTypeIdent
@@ -1723,16 +1723,17 @@
 ovider%3E)
+ 
 object).
@@ -1792,16 +1792,793 @@
 ect;%0A%09%09%7D
+%0A%0A%09%09private ExtensibleURIConverterImpl uriConverter;%0A%0A%09%09@Override%0A%09%09protected synchronized URIConverter getURIConverter() %7B%0A%09%09%09if (this.uriConverter == null) %7B%0A%09%09%09%09List%3CContentHandler%3E withoutPlatformDelegate = Lists.newArrayList();%0A%09%09%09%09for (ContentHandler contentHandler : ContentHandler.Registry.INSTANCE.contentHandlers()) %7B%0A%09%09%09%09%09if (!isTooEager(contentHandler))%0A%09%09%09%09%09%09withoutPlatformDelegate.add(contentHandler);%0A%09%09%09%09%7D%0A%09%09%09%09this.uriConverter = new ExtensibleURIConverterImpl(URIHandler.DEFAULT_HANDLERS, withoutPlatformDelegate);%0A%09%09%09%7D%0A%09%09%09return this.uriConverter;%0A%09%09%7D%0A%0A%09%09private boolean isTooEager(ContentHandler contentHandler) %7B%0A%09%09%09try %7B%0A%09%09%09%09return contentHandler.canHandle(null);%0A%09%09%09%7D catch (RuntimeException e) %7B%0A%09%09%09%7D catch (AssertionError e) %7B%0A%09%09%09%7D%0A%09%09%09return false;%0A%09%09%7D
 %0A%09%7D%0A%0A%09pr
@@ -2730,18 +2730,20 @@
 %09if (uri
+ 
 ==
+ 
 null)%0A%09%09
@@ -3284,12 +3284,7 @@
 %0A%09%7D%0A
-%09%0A%09%0A%09
 %0A%7D%0A

exclude too greedy contenthandlers