package io.joynr.generator.loading;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2016 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.StringInputStream;
import org.franca.core.dsl.FrancaIDLStandaloneSetup;

import com.google.common.base.Joiner;
import com.google.common.collect.Iterators;
import com.google.inject.Inject;
import com.google.inject.Injector;

public class ModelStore implements Iterable<EObject> {

    private final IUriProvider uriProvider;
    private final XtextResourceSet resourceSet;
    {
        Injector injector = new FrancaIDLStandaloneSetup().createInjectorAndDoEMFRegistration();
        resourceSet = injector.getInstance(XtextResourceSet.class);
        resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
    }

    @Inject
    public ModelStore(IUriProvider uriProvider) {
        super();
        this.uriProvider = uriProvider;
    }

    public Resource getResource(URI uri) {
        return resourceSet.getResource(uri, true);
    }

    public List<Resource> load() {
        for (URI uri : uriProvider.allUris()) {
            try {
                Resource resource = resourceSet.getResource(uri, false);
                if (resource == null) {
                    resource = resourceSet.createResource(uri);
                }
                resource.load(null);
                EcoreUtil.resolveAll(resource);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        EcoreUtil.resolveAll(resourceSet);
        return resourceSet.getResources();
    }

    public void add(Resource... resources) {
        resourceSet.getResources().addAll(asList(resources));
    }

    public Resource parse(String name, String[] lines) {
        Resource resource = createResource(name);
        String inputString = Joiner.on("\n").join("\n", lines);
        StringInputStream inputStream = new StringInputStream(inputString);
        try {
            resource.load(inputStream, emptyMap());
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(0);
        }
        return resource;
    }

    private Resource createResource(String name) {
        URI uri = URI.createURI(name);
        Resource resource = resourceSet.getResource(uri, false);
        if (resource != null) {
            resource.unload();
        } else {
            resource = resourceSet.createResource(uri);
        }
        return resource;
    }

    public Iterator<EObject> iterator() {
        return Iterators.filter(resourceSet.getAllContents(), EObject.class);
    }

    public static ModelStore modelsIn(IUriProvider uriProvider) {
        ModelStore modelStore = new ModelStore(uriProvider);
        modelStore.load();
        return modelStore;
    }

}

@@ -709,54 +709,8 @@
 ist;
-%0Aimport static java.util.Collections.emptyMap;
 %0A%0Aim
@@ -1062,57 +1062,8 @@
 et;%0A
-import org.eclipse.xtext.util.StringInputStream;%0A
 impo
@@ -1116,46 +1116,8 @@
 p;%0A%0A
-import com.google.common.base.Joiner;%0A
 impo
@@ -2562,793 +2562,8 @@
  %7D%0A%0A
-    public Resource parse(String name, String%5B%5D lines) %7B%0A        Resource resource = createResource(name);%0A        String inputString = Joiner.on(%22%5Cn%22).join(%22%5Cn%22, lines);%0A        StringInputStream inputStream = new StringInputStream(inputString);%0A        try %7B%0A            resource.load(inputStream, emptyMap());%0A        %7D catch (IOException ex) %7B%0A            ex.printStackTrace();%0A            System.exit(0);%0A        %7D%0A        return resource;%0A    %7D%0A%0A    private Resource createResource(String name) %7B%0A        URI uri = URI.createURI(name);%0A        Resource resource = resourceSet.getResource(uri, false);%0A        if (resource != null) %7B%0A            resource.unload();%0A        %7D else %7B%0A            resource = resourceSet.createResource(uri);%0A        %7D%0A        return resource;%0A    %7D%0A%0A
     

Remove unused ModelStore.parse method