Skip to content

Package: TemplateLoaderService$Provider

TemplateLoaderService$Provider

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.core.services.datatemplate;
15:
16: import java.io.IOException;
17: import java.io.InputStream;
18: import java.util.Collection;
19:
20: import javax.inject.Named;
21:
22: import org.eclipse.emf.common.util.URI;
23: import org.eclipse.emf.ecore.resource.Resource;
24: import org.eclipse.emf.ecore.resource.ResourceSet;
25: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
26: import org.eclipse.emf.ecore.util.EcoreUtil;
27: import org.eclipse.emfforms.bazaar.Vendor;
28: import org.eclipse.emfforms.datatemplate.DataTemplatePackage;
29: import org.eclipse.emfforms.datatemplate.TemplateCollection;
30:
31: /**
32: * Protocol for a data template loader service.
33: *
34: * @since 1.21
35: */
36: public interface TemplateLoaderService {
37:
38:         /**
39:          * The default template loader service.
40:          */
41:         TemplateLoaderService DEFAULT = uri -> {
42:                 final ResourceSet resourceSet = new ResourceSetImpl();
43:                 final Resource resource = resourceSet.createResource(URI.createURI("VIRTUAL_URI.xmi")); //$NON-NLS-1$
44:                 try (InputStream inputStream = resourceSet.getURIConverter().createInputStream(uri, null)) {
45:                         resource.load(inputStream, null);
46:                         return EcoreUtil.getObjectsByType(resource.getContents(), DataTemplatePackage.Literals.TEMPLATE_COLLECTION);
47:                 }
48:         };
49:
50:         /**
51:          * Load a template resource from an URI.
52:          *
53:          * @param uri the URI of the template resource to load
54:          * @return the template collection(s) loaded from the resource
55:          *
56:          * @throws IOException on failure to load the templates
57:          */
58:         Collection<? extends TemplateCollection> loadTemplates(URI uri) throws IOException;
59:
60:         //
61:         // Nested types
62:         //
63:
64:         /**
65:          * Specific Bazaar vendor interface for {@link TemplateLoaderService} providers.
66:          * It is intended that implementations be registered as OSGi services, for
67:          * the XMI-based {@link TemplateProvider} to use to load template resources.
68:          * The Bazaar context provides the following dependencies for injection:
69:          * <ul>
70:          * <li>the {@link URI} of the resource to be loaded</li>
71:          * <li>the contributor ID of the bundle providing the resource, as a
72:          * {@link String} {@linkplain Named named} {@link Provider#CONTRIBUTOR_ID "contributorID"}</li>
73:          * </ul>
74:          *
75:          * @since 1.21
76:          */
77:         // CHECKSTYLE.OFF: Interface - Prefer a distinct type from Vendor for OSGi registration
78:         public interface Provider extends Vendor<TemplateLoaderService> {
79:                 // CHECKSTYLE.ON: Interface
80:                 /**
81:                  * Name of the {@link String}-valued context variable for the contributor ID.
82:                  *
83:                  * @see Named @Named
84:                  */
85:                 String CONTRIBUTOR_ID = "contributorID"; //$NON-NLS-1$
86:
87:                 // Nothing to add to the superinterface
88:         }
89:
90: }