Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 27d95786a89304e628ec20d77746796bd3836dca (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*****************************************************************************
 * Copyright (c) 2013, 2014 CEA LIST and others.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 429242
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.importer;

import java.util.Collection;
import java.util.Set;

import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.cdo.internal.core.Activator;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.util.DiUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * This is the DependencyAdapter type. Enjoy.
 */
public class DependencyAdapter extends AdapterImpl {

	private final ModelsReader modelsMetadata = new ModelsReader();

	private final Set<Resource> dependencies = Sets.newLinkedHashSet();

	private final Set<Resource> dependents = Sets.newLinkedHashSet();

	private DependencyAdapter() {
		super();
	}

	public static DependencyAdapter getInstance(Resource resource) {
		DependencyAdapter result = getExistingInstance(resource);

		if (result == null) {
			result = new DependencyAdapter();
			resource.eAdapters().add(Math.min(1, resource.eAdapters().size()), result);

			result.analyze(resource);
		}

		return result;
	}

	static DependencyAdapter getExistingInstance(Resource resource) {
		DependencyAdapter result = null;

		for (Object next : resource.eAdapters()) {
			if (next instanceof DependencyAdapter) {
				result = (DependencyAdapter) next;
				break;
			}
		}

		return result;
	}

	public Set<Resource> getDependencies() {
		return dependencies;
	}

	public static Set<Resource> getDependencies(Resource resource) {
		return getInstance(resource).getDependencies();
	}

	public Set<Resource> getDependents() {
		return dependents;
	}

	public static Set<Resource> getDependents(Resource resource) {
		return getInstance(resource).getDependents();
	}

	private void analyze(Resource resource) {
		if (resource.getContents().isEmpty() && isDIResource(resource)) {
			// similarly-named resources that are recognized by Papyrus are implicitly components
			for (Resource next : getImplicitComponents(resource)) {
				if (isUserModelResource(next.getURI())) {
					addDependency(next);
				}
			}
		} else {
			for (TreeIterator<EObject> iter = EcoreUtil.getAllProperContents(resource, false); iter.hasNext();) {
				EObject next = iter.next();

				// ignore annotations, such as are used for hyperlinks
				if (next instanceof EAnnotation) {
					iter.prune();
				} else {
					for (EObject xref : next.eCrossReferences()) {
						Resource xrefRes = xref.eResource();
						if ((xrefRes != null) && (isUserModelResource(xrefRes.getURI()))) {
							addDependency(xrefRes);
						}
					}
				}
			}
		}
	}

	private Resource getResource() {
		return (Resource) getTarget();
	}

	void addDependency(Resource resource) {
		Resource self = getResource();

		if ((resource != self) && dependencies.add(resource)) {
			getInstance(resource).addDependent(self);
		}
	}

	private void addDependent(Resource resource) {
		if (resource != getResource()) {
			dependents.add(resource);
		}
	}

	boolean isUserModelResource(URI uri) {
		ModelSet modelSet = getModelSet();
		boolean result = (modelSet != null) ? modelSet.isUserModelResource(uri) :
				// config.hasResource(uri) &&
				uri.isPlatformResource() || uri.isFile() || CDOUtils.isCDOURI(uri);

		return result && !uri.isArchive();
	}

	private ModelSet getModelSet() {
		return CDOUtils.adapt(getResource().getResourceSet(), ModelSet.class);
	}

	private Iterable<Resource> getImplicitComponents(Resource diResource) {
		// usually only two components: diagrams and semantics
		Collection<Resource> result = Lists.newArrayListWithExpectedSize(2);

		ResourceSet rset = diResource.getResourceSet();
		URIConverter converter = rset.getURIConverter();

		for (URI next : modelsMetadata.getKnownModelURIs(diResource.getURI())) {
			if (!next.equals(diResource.getURI()) && converter.exists(next, null)) {
				try {
					result.add(rset.getResource(next, true));
				} catch (Exception e) {
					Activator.log.error("Uncaught exception in loading component of Papyrus model.", e); //$NON-NLS-1$
				}
			}
		}

		return result;
	}

	public static boolean isDIResource(Resource resource) {
		boolean result = false;

		if (resource.getContents().isEmpty()) {
			// DI resources are typically empty; just markers
			result = DiModel.DI_FILE_EXTENSION.equals(resource.getURI().fileExtension());
		} else {
			// Look for legacy DI content (the Sash Model that now is in a *.sash resource in the workspace metadata area)
			result = DiUtils.lookupSashWindowsMngr(resource) != null;
		}

		return result;
	}

	public static Resource getDIResource(Resource resource) {
		Resource result = null;

		if (isDIResource(resource)) {
			result = resource;
		} else {
			// find the the DI resource
			ResourceSet rset = resource.getResourceSet();
			URI uri = resource.getURI().trimFileExtension().appendFileExtension(DiModel.DI_FILE_EXTENSION);
			if (rset.getURIConverter().exists(uri, null)) {
				Resource di = rset.getResource(uri, true);

				if ((di != null) && isDIResource(di)) {
					result = di;
				}
			}
		}

		return result;
	}
}

Back to the top