Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bbe506e8f9faa9b62e36b4f89429549c86d937b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Tristan Faure (Atos Origin) tristan.faure@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.resource.util;

import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.resource.Resource;
import org.eclipse.papyrus.resource.ResourceFactory;

/**
 * This class provides static methods to help users using Resources
 * 
 * @author tristan.faure@atosorigin.com
 * 
 */
public class ResourceUtil {

	/**
	 * Get in the EMF Resource of the eobject e the Resource Papyrus If it doesn't exist, it adds it
	 * 
	 * @param e
	 *            the eobject contained in the resource browsed
	 * @return the Resource or null if the parameter is null or if the eobject in parameter doesn't
	 *         have an EMF Resource
	 */
	public static Resource getResource(EObject e) {
		if (e == null || e.eResource() == null) {
			return null;
		}
		return getResource(e.eResource());
	}

	/**
	 * Get in the EMF Resource the Resource papyrus If it doesn't exist, it adds it
	 * 
	 * @param eResource
	 *            the Papyrus Resource browsed
	 * @return the Resource or null if the parameter is null
	 */
	public static Resource getResource(org.eclipse.emf.ecore.resource.Resource eResource) {
		if (eResource == null) {
			return null;
		}
		Resource result = null;
		for (EObject e : eResource.getContents()) {
			if (e instanceof Resource) {
				result = (Resource) e;
				break;
			}
		}
		if (result == null) {
			result = ResourceFactory.eINSTANCE.createResource();
			eResource.getContents().add(result);
		}
		return result;
	}

	/**
	 * Remove the Papyrus Resource from the EMF Resource of e
	 * 
	 * @param e
	 */
	public static void removeResource(EObject e) {
		if (e == null || e.eResource() == null) {
			return;
		}
		removeResource(e.eResource());
	}

	/**
	 * Remove the Papyrus Resource from the EMF Resource eResource
	 * 
	 * @param eResource
	 */
	public static void removeResource(org.eclipse.emf.ecore.resource.Resource eResource) {
		if (eResource == null) {
			return;
		}
		Iterator<EObject> i = eResource.getContents().iterator();
		while (i.hasNext()) {
			EObject current = i.next();
			if (current instanceof Resource) {
				i.remove();
			}
		}
	}
}

Back to the top