Skip to content

Package: ViewTemplateSupplierUtil

ViewTemplateSupplierUtil

nameinstructionbranchcomplexitylinemethod
loadViewTemplate(URI)
M: 11 C: 64
85%
M: 4 C: 4
50%
M: 4 C: 1
20%
M: 4 C: 18
82%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.spi.view.template.service;
15:
16: import java.io.IOException;
17: import java.util.Map;
18:
19: import org.eclipse.emf.common.util.URI;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.resource.Resource;
22: import org.eclipse.emf.ecore.resource.ResourceSet;
23: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
24: import org.eclipse.emf.ecore.util.EcoreUtil;
25: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
26: import org.eclipse.emf.ecp.view.template.model.VTTemplatePackage;
27: import org.eclipse.emf.ecp.view.template.model.VTViewTemplate;
28:
29: /**
30: * Utility class for common functionality useful for implementing {@link ViewTemplateSupplier ViewTemplateSuppliers}.
31: *
32: * @author Lucas Koehler
33: * @since 1.18
34: */
35: public final class ViewTemplateSupplierUtil {
36:
37:         // Do not instantiate utility class
38:         private ViewTemplateSupplierUtil() {
39:         }
40:
41:         /**
42:          * Loads a {@link VTViewTemplate} from the resource at the given URI. Returns <code>null</code> if the resource does
43:          * not exist, cannot be loaded, or does not contain exactly one {@link VTViewTemplate} on the root level.
44:          *
45:          * @param uri The URI to the resource containing the {@link VTViewTemplate} to load
46:          * @return The loaded {@link VTViewTemplate}
47:          */
48:         public static VTViewTemplate loadViewTemplate(URI uri) {
49:                 final ResourceSet resourceSet = new ResourceSetImpl();
50:                 final Map<String, Object> extensionToFactoryMap = resourceSet
51:                         .getResourceFactoryRegistry().getExtensionToFactoryMap();
52:                 extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
53:                         new XMIResourceFactoryImpl());
54:                 resourceSet.getPackageRegistry().put(VTTemplatePackage.eNS_URI,
55:                         VTTemplatePackage.eINSTANCE);
56:                 final Resource resource = resourceSet.createResource(uri);
57:•                if (resource == null) {
58:                         return null;
59:                 }
60:
61:                 try {
62:                         resource.load(null);
63:                 } catch (final IOException exception) {
64:                         return null;
65:                 }
66:
67:                 int rsSize = resourceSet.getResources().size();
68:                 EcoreUtil.resolveAll(resourceSet);
69:•                while (rsSize != resourceSet.getResources().size()) {
70:                         EcoreUtil.resolveAll(resourceSet);
71:                         rsSize = resourceSet.getResources().size();
72:                 }
73:
74:•                if (resource.getContents().size() != 1) {
75:                         return null;
76:                 }
77:
78:                 final EObject eObject = resource.getContents().get(0);
79:•                return eObject instanceof VTViewTemplate ? (VTViewTemplate) eObject : null;
80:         }
81: }