001/*******************************************************************************
002 * Copyright (c) 2017, 2018 Red Hat Inc
003 * All rights reserved. This program and the accompanying materials
004 * are made available under the terms of the Eclipse Public License v1.0
005 * which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v10.html
007 *
008 * Contributors:
009 *     Jens Reimann - initial API and implementation
010 *******************************************************************************/
011package de.dentrassi.varlink.maven;
012
013import java.io.IOException;
014import java.nio.file.Path;
015import java.util.LinkedList;
016import java.util.List;
017
018import org.eclipse.emf.common.util.URI;
019import org.eclipse.emf.ecore.EObject;
020import org.eclipse.emf.ecore.resource.Resource;
021import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
022import org.eclipse.emf.ecore.resource.ResourceSet;
023import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
024
025import de.dentrassi.varlink.idl.VarlinkIdlStandaloneSetup;
026import de.dentrassi.varlink.idl.varlinkIdl.Interface;
027import de.dentrassi.varlink.idl.varlinkIdl.VarlinkIdlPackage;
028
029public class Loader {
030
031    public List<Interface> interfaces = new LinkedList<>();
032
033    public List<Interface> getInterfaces() {
034        return this.interfaces;
035    }
036
037    public List<Diagnostic> loadFrom(final Path path) throws IOException {
038
039        new VarlinkIdlStandaloneSetup().createInjectorAndDoEMFRegistration();
040
041        final ResourceSet resourceSet = new ResourceSetImpl();
042
043        final URI uri = URI.createFileURI(path.toString());
044
045        // initialize package
046        VarlinkIdlPackage.eINSTANCE.eClass();
047
048        final Resource r = resourceSet.createResource(uri);
049        r.load(null);
050
051        final EObject c = r.getContents().get(0);
052        if (!(c instanceof Interface)) {
053            throw new IllegalStateException(String.format("File '%s' does not contain a Varlink interface", path));
054        }
055
056        this.interfaces.add((Interface) c);
057
058        return r.getErrors();
059    }
060
061}