Skip to content

Package: ResourceSetHelpers

ResourceSetHelpers

nameinstructionbranchcomplexitylinemethod
addResourceToSet(ResourceSet, URI)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
addResourceToSet(ResourceSet, URI, Map)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createResourceSet(AdapterFactoryEditingDomain)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createResourceSet(CommandStack)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
findAllOf(ResourceSet, Class, boolean)
M: 0 C: 68
100%
M: 3 C: 15
83%
M: 3 C: 7
70%
M: 0 C: 17
100%
M: 0 C: 1
100%
findAllOfTypeInResourceSet(EObject, Class, boolean)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findAllOfTypeInResourceSet(EObject, EClassifier, boolean)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
loadResourceSetWithProxies(URI, BasicCommandStack)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
loadResourceSetWithProxies(URI, BasicCommandStack, Map)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
loadResourceWithProxies(URI, ResourceSet)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
loadResourceWithProxies(URI, ResourceSet, Map)
M: 2 C: 7
78%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
resolveAllProxies(ResourceSet)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
save(ResourceSet)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
save(ResourceSet, Map)
M: 17 C: 21
55%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 4 C: 4
50%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * Clemens Elflein - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.editor.helpers;
15:
16: import java.io.IOException;
17: import java.util.ArrayList;
18: import java.util.Collections;
19: import java.util.HashMap;
20: import java.util.List;
21: import java.util.Map;
22:
23: import org.eclipse.core.runtime.IStatus;
24: import org.eclipse.core.runtime.Status;
25: import org.eclipse.emf.common.command.BasicCommandStack;
26: import org.eclipse.emf.common.command.CommandStack;
27: import org.eclipse.emf.common.notify.AdapterFactory;
28: import org.eclipse.emf.common.util.TreeIterator;
29: import org.eclipse.emf.common.util.URI;
30: import org.eclipse.emf.ecore.EClassifier;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecore.EcorePackage;
33: import org.eclipse.emf.ecore.plugin.EcorePlugin;
34: import org.eclipse.emf.ecore.resource.Resource;
35: import org.eclipse.emf.ecore.resource.ResourceSet;
36: import org.eclipse.emf.ecore.util.EcoreUtil;
37: import org.eclipse.emf.ecore.xmi.XMLResource;
38: import org.eclipse.emf.ecp.view.model.common.edit.provider.CustomReflectiveItemProviderAdapterFactory;
39: import org.eclipse.emf.ecp.view.spi.model.reporting.StatusReport;
40: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
41: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
42: import org.eclipse.emfforms.internal.editor.Activator;
43:
44: /**
45: * The Class ResourceSetHelpers.
46: * It is a utility class to perform operations on ResourceSet objects.
47: */
48: public final class ResourceSetHelpers {
49:
50:         private ResourceSetHelpers() {
51:         }
52:
53:         /**
54:          * Save all changes in a ResourceSet.
55:          *
56:          * @param resourceSet the resource set
57:          * @return true, if successful
58:          * @deprecated see {@link ResourceSetHelpers#save(ResourceSet, Map)}
59:          */
60:         @Deprecated
61:         public static boolean save(ResourceSet resourceSet) {
62:                 return save(resourceSet, Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
63:         }
64:
65:         /**
66:          * Save all changes in a ResourceSet.
67:          *
68:          * @param resourceSet the resource set
69:          * @param saveOptions the save options
70:          * @return true, if successful
71:          * @since 1.19
72:          */
73:         public static boolean save(ResourceSet resourceSet, Map<Object, Object> saveOptions) {
74:                 try {
75:•                        for (final Resource resource : resourceSet.getResources()) {
76:•                                if (!resource.getURI().isPlatformPlugin()) {
77:                                         resource.save(saveOptions);
78:                                 }
79:                         }
80:                         return true;
81:                 } catch (final IOException ex) {
82:                         Activator.getDefault().getReportService().report(
83:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
84:                 }
85:                 return false;
86:         }
87:
88:         /**
89:          * Load resource set with proxies.
90:          *
91:          * @param resourceURI the resource uri (= File to load)
92:          * @param commandStack the command stack
93:          * @return the resource set
94:          * @deprecated see {@link ResourceSetHelpers#loadResourceSetWithProxies(URI, BasicCommandStack, Map)}
95:          */
96:         @Deprecated
97:         public static ResourceSet loadResourceSetWithProxies(URI resourceURI,
98:                 BasicCommandStack commandStack) {
99:                 try {
100:                         loadResourceSetWithProxies(resourceURI, commandStack, null);
101:                 } catch (final IOException ex) {
102:                         Activator.getDefault().getReportService().report(
103:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
104:                 }
105:                 return null;
106:         }
107:
108:         /**
109:          * Load resource set with proxies.
110:          *
111:          * @param resourceURI the resource uri (= File to load)
112:          * @param commandStack the command stack
113:          * @param options the resource load options
114:          * @return the resource set
115:          * @throws IOException if an error occurred while loading the resource
116:          * @since 1.19
117:          */
118:         public static ResourceSet loadResourceSetWithProxies(URI resourceURI, BasicCommandStack commandStack,
119:                 Map<Object, Object> options) throws IOException {
120:                 // Create a ResourceSet and add the requested Resource
121:                 final ResourceSet resourceSet = createResourceSet(commandStack);
122:                 return loadResourceWithProxies(resourceURI, resourceSet, options);
123:         }
124:
125:         /**
126:          * Load resource set with proxies.
127:          *
128:          * @param resourceURI the resource uri (= File to load)
129:          * @param resourceSet the resource set
130:          * @return the resource set
131:          * @since 1.10
132:          * @deprecated see {@link ResourceSetHelpers#loadResourceWithProxies(URI, ResourceSet, Map)}
133:          */
134:         @Deprecated
135:         public static ResourceSet loadResourceWithProxies(URI resourceURI, final ResourceSet resourceSet) {
136:                 try {
137:•                        if (addResourceToSet(resourceSet, resourceURI, null)) {
138:                                 return resourceSet;
139:                         }
140:                 } catch (final IOException ex) {
141:                         Activator.getDefault().getReportService().report(
142:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
143:                 }
144:                 return null;
145:         }
146:
147:         /**
148:          * Load resource set with proxies.
149:          *
150:          * @param resourceURI the resource uri (= File to load)
151:          * @param resourceSet the resource set
152:          * @return the resource set
153:          * @param loadOptions the resource load options
154:          * @throws IOException if an error occurred while loading the resource
155:          * @since 1.19
156:          */
157:         public static ResourceSet loadResourceWithProxies(URI resourceURI, final ResourceSet resourceSet,
158:                 final Map<Object, Object> loadOptions) throws IOException {
159:•                if (addResourceToSet(resourceSet, resourceURI, loadOptions)) {
160:                         return resourceSet;
161:                 }
162:                 return null;
163:         }
164:
165:         /**
166:          * Creates a ResourceSet with a CommandStack attached to it.
167:          *
168:          * @param commandStack the command stack
169:          * @return the resource set
170:          * @since 1.10
171:          */
172:         public static ResourceSet createResourceSet(CommandStack commandStack) {
173:                 final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(
174:                         new ComposedAdapterFactory(
175:                                 new AdapterFactory[] {
176:                                         new CustomReflectiveItemProviderAdapterFactory(),
177:                                         new ComposedAdapterFactory(
178:                                                 ComposedAdapterFactory.Descriptor.Registry.INSTANCE) }),
179:                         commandStack);
180:                 return createResourceSet(domain);
181:         }
182:
183:         /**
184:          * Creates a ResourceSet with the given EditingDomain.
185:          *
186:          * @param domain the editing domain
187:          * @return the resource set
188:          * @since 1.11
189:          */
190:         public static ResourceSet createResourceSet(final AdapterFactoryEditingDomain domain) {
191:                 final ResourceSet resourceSet = domain.getResourceSet();
192:                 resourceSet.eAdapters().add(
193:                         new AdapterFactoryEditingDomain.EditingDomainProvider(domain));
194:                 // needed to be able to resolve resource paths to plugin paths and thus load referenced ecores
195:                 resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
196:
197:                 resourceSet.getLoadOptions().put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, Boolean.TRUE);
198:                 return resourceSet;
199:         }
200:
201:         /**
202:          * Loads a resource from resourceURI and adds it to the resourceSet.
203:          *
204:          * @param resourceSet the resource set
205:          * @param resourceURI the resource uri
206:          * @return true, if successful
207:          * @deprecated see {@link ResourceSetHelpers#addResourceToSet(ResourceSet, URI, Map)}
208:          */
209:         @Deprecated
210:         public static boolean addResourceToSet(ResourceSet resourceSet,
211:                 URI resourceURI) {
212:
213:                 final Map<Object, Object> loadOptions = new HashMap<Object, Object>();
214:                 loadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
215:                         Boolean.TRUE);
216:                 try {
217:                         return addResourceToSet(resourceSet, resourceURI, loadOptions);
218:                 } catch (final IOException ex) {
219:                         Activator.getDefault().getReportService().report(
220:                                 new StatusReport(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex)));
221:                 }
222:                 return false;
223:         }
224:
225:         /**
226:          * Loads a resource from resourceURI and adds it to the resourceSet.
227:          *
228:          * @param resourceSet the resource set
229:          * @param resourceURI the resource uri
230:          * @param loadOptions the resource load options
231:          * @return true, if successful
232:          * @throws IOException if an error occurred while loading the resource
233:          * @since 1.18
234:          */
235:         public static boolean addResourceToSet(ResourceSet resourceSet, URI resourceURI,
236:                 final Map<Object, Object> loadOptions) throws IOException {
237:                 resourceSet.createResource(resourceURI).load(loadOptions);
238:
239:                 // resolve all proxies
240:                 resolveAllProxies(resourceSet);
241:                 return true;
242:         }
243:
244:         /**
245:          * Visits all proxies in the resource set and tries to resolve them. Recursively also resolves all resources added
246:          * during the resolvement of the initial resources.
247:          *
248:          * @param resourceSet The {@link ResourceSet} containing the {@link Resource Resources} to resolve
249:          * @since 1.22
250:          */
251:         public static void resolveAllProxies(ResourceSet resourceSet) {
252:                 int rsSize = resourceSet.getResources().size();
253:
254:                 EcoreUtil.resolveAll(resourceSet);
255:•                while (rsSize != resourceSet.getResources().size()) {
256:                         EcoreUtil.resolveAll(resourceSet);
257:                         rsSize = resourceSet.getResources().size();
258:                 }
259:         }
260:
261:         /**
262:          * Find all of type in resource set.
263:          *
264:          * @param object the object
265:          * @param type the type
266:          * @param includeEcorePackage the include ecore package
267:          * @return the list
268:          */
269:         public static List<?> findAllOfTypeInResourceSet(EObject object,
270:                 EClassifier type, boolean includeEcorePackage) {
271:                 return ResourceSetHelpers.findAllOf(
272:                         object.eResource().getResourceSet(), type.getInstanceClass(),
273:                         includeEcorePackage);
274:         }
275:
276:         /**
277:          * Find all of type in resource set.
278:          *
279:          * @param <T> the generic type
280:          * @param object the object
281:          * @param clazz the clazz
282:          * @param includeEcorePackage the include ecore package
283:          * @return the list
284:          */
285:         public static <T> List<T> findAllOfTypeInResourceSet(EObject object,
286:                 Class<T> clazz, boolean includeEcorePackage) {
287:                 return ResourceSetHelpers
288:                         .findAllOf(object.eResource().getResourceSet(), clazz,
289:                                 includeEcorePackage);
290:         }
291:
292:         /**
293:          * Find all of type in the ResourceSet.
294:          *
295:          * @param <T> the generic type
296:          * @param resourceSet the resource set
297:          * @param clazz the clazz
298:          * @param includeEcorePackage the include ecore package
299:          * @return the list
300:          */
301:         public static <T> List<T> findAllOf(ResourceSet resourceSet,
302:                 Class<T> clazz, boolean includeEcorePackage) {
303:                 final List<T> result = new ArrayList<T>();
304:
305:                 // Iterate through all EObjects in every Resource in the set and return
306:                 // all Objects of Class clazz.
307:•                if (resourceSet != null) {
308:•                        for (final Resource resource : resourceSet.getResources()) {
309:                                 final TreeIterator<EObject> objectIterator = resource
310:                                         .getAllContents();
311:•                                while (objectIterator.hasNext()) {
312:                                         final EObject o = objectIterator.next();
313:•                                        if (o != null && clazz.isInstance(o)) {
314:                                                 result.add(clazz.cast(o));
315:                                         }
316:                                 }
317:                         }
318:                 }
319:
320:•                if (includeEcorePackage) {
321:                         // Find all EDatatypes in the ECore-Package.
322:                         final TreeIterator<EObject> objectIterator = EcorePackage.eINSTANCE
323:                                 .eAllContents();
324:•                        while (objectIterator.hasNext()) {
325:                                 final EObject o = objectIterator.next();
326:•                                if (o != null && clazz.isInstance(o)) {
327:                                         result.add(clazz.cast(o));
328:                                 }
329:                         }
330:                 }
331:
332:                 return result;
333:         }
334: }