Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb74eebbe5cabf66f3926235357cc2c6d124284c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *
 * 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:
 *  Arnaud Cuccuru (CEA LIST) - Initial API and implementation
 *  Vincent Lorenzo   (CEA LIST)
 *  Benoit Maggi (CEA LIST) - Bug 431629 Patch to avoid loop on imported packages
 *****************************************************************************/

package org.eclipse.papyrus.uml.tools.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.resource.UMLResource;


public class NameResolutionHelper {

	protected Namespace scope;

	protected EClass filter;

	protected Map<String, List<NamedElement>> allNames;

	public NameResolutionHelper(Namespace scope, EClass filter) {
		this.scope = scope;
		this.filter = filter;
	}

	public List<NamedElement> getNamedElements(String name) {
		if (this.allNames == null) {
			this.allNames = new HashMap<String, List<NamedElement>>();
			this.computeAllNames();
		}
		List<NamedElement> namedElements = this.allNames.get(name);
		return namedElements != null ? namedElements : new ArrayList<NamedElement>();
	}

	/**
	 * TODO
	 */
	protected void computeAllNames() {

		// compute names directly available in the scope
		computeNames("", scope, true);

		// compute names related to enclosing namepaces of scope
		Namespace enclosingNamespace = scope.getNamespace();
		String prefix = "";
		while (enclosingNamespace != null) {
			// prefix += enclosingNamespace.getName() + NamedElementUtil.QUALIFIED_NAME_SEPARATOR;
			prefix = "";
			computeNames(prefix, enclosingNamespace, false);
			enclosingNamespace = enclosingNamespace.getNamespace();
		}

		// Compute names related to the root context model
		Namespace model = scope.getModel();
		if (model == null) {
			model = scope;
		}
		if (filter != null) {
			if (filter.isSuperTypeOf(model.eClass())) {
				List<NamedElement> l = this.allNames.get(model.getName());
				if (l == null) { // i.e. no names have already been resolved in enclosed namespaces
					l = new ArrayList<NamedElement>();
					l.add(model);
					this.allNames.put(model.getName(), l);
				}
			}
		} else {
			List<NamedElement> l = this.allNames.get(model.getName());
			if (l == null) { // i.e. no names have already been resolved in enclosed namespaces
				l = new ArrayList<NamedElement>();
				l.add(model);
				this.allNames.put(model.getName(), l);
			}
		}
		computeNames(model.getName() + NamedElementUtil.QUALIFIED_NAME_SEPARATOR, model, false);

		// Build names corresponding to other available UML resources in the workspace
		List<Resource> resources = new ArrayList<Resource>(scope.eResource().getResourceSet().getResources());// we duplicate the resource to avoid concurrent modification
		for (Resource resource : resources) {
			if (resource != scope.eResource() && resource instanceof UMLResource) {
				UMLResource umlResource = (UMLResource) resource;
				Model root = null;
				for (Iterator<EObject> i = umlResource.getAllContents(); i.hasNext() && root == null;) {
					EObject next = i.next();
					if (next instanceof Model) {
						root = (Model) next;
					}
				}
				if (root != null) {
					if (filter != null) {
						if (filter.isSuperTypeOf(root.eClass())) {
							List<NamedElement> l = this.allNames.get(root.getName());
							if (l == null) { // i.e. no names have already been resolved in enclosed namespaces
								l = new ArrayList<NamedElement>();
								l.add(root);
								this.allNames.put(root.getName(), l);
							}
						}
					} else {
						List<NamedElement> l = this.allNames.get(root.getName());
						if (l == null) { // i.e. no names have already been resolved in enclosed namespaces
							l = new ArrayList<NamedElement>();
							l.add(root);
							this.allNames.put(root.getName(), l);
						}
					}
					computeNames(root.getName() + NamedElementUtil.QUALIFIED_NAME_SEPARATOR, root, false);
				}
			}
		}
	}

	/**
	 * TODO
	 */
	protected void computeNames(String prefix, Namespace scope, boolean ignoreAlreadyFoundNames) {
		computeNames(prefix, scope, ignoreAlreadyFoundNames, new HashSet<Namespace>());
	}

	/**
	 *
	 * @param prefix
	 * @param scope
	 * @param ignoreAlreadyFoundNames
	 * @param alreadyComputedNamespace
	 *            list of already visited Namespace to avoid loop on imported packages
	 */
	protected void computeNames(String prefix, Namespace scope, boolean ignoreAlreadyFoundNames, Set<Namespace> alreadyComputedNamespace) {
		alreadyComputedNamespace.add(scope);
		Set<String> preExistingKeys;
		if (ignoreAlreadyFoundNames) {
			preExistingKeys = new HashSet<String>();
		} else {
			preExistingKeys = this.allNames.keySet();
		}
		// iterates members of the scope
		for (NamedElement member : scope.getMembers()) {
			List<String> memberNames = scope.getNamesOfMember(member);
			// iterates other names given to the current member in the context of this scope
			for (String memberName : memberNames) {
				// Checks if the name must be considered or not
				if (!preExistingKeys.contains(prefix + memberName)) {
					List<NamedElement> l = this.getNamedElements(prefix + memberName);
					l.add(member);
					this.allNames.put(prefix + memberName, l);
				}
				if (member instanceof Namespace && !alreadyComputedNamespace.contains(member)) { // avoid loop on imported packages
					// Recursive call on the current member
					computeNames(prefix + memberName + NamedElementUtil.QUALIFIED_NAME_SEPARATOR, (Namespace) member, false, alreadyComputedNamespace);
				}
			}
		}
	}
}

Back to the top