Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0daf46e3964be1dfc10a4b00874a382eef499ab8 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*****************************************************************************
 * Copyright (c) 2014, 2016 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.decoratormodel.internal.resource.index;

import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.util.UMLUtil;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

/**
 * Framework for SAX parsing handlers for indexing UML resources.
 */
public abstract class AbstractUMLIndexHandler extends DefaultHandler {
	protected final URI fileURI;
	private final Map<String, Map<String, String>> packageToProfileApplications = Maps.newHashMap();

	private String umlNamespace;
	private String umlPrefix;
	private String xmiType;
	private String xmiID;

	private Set<String> packageTypes;
	private String eAnnotations;
	private String references;

	private UMLElement top;
	private int ignore;

	protected UMLElement currentPackage;

	private Await await = new Await();

	/**
	 * Initializes me.
	 *
	 * @param fileURI
	 *            the URI of the UML file that I am parsing
	 */
	public AbstractUMLIndexHandler(final URI fileURI) {
		this.fileURI = fileURI;
	}

	public URI getFileURI() {
		return fileURI;
	}

	public Map<String, Map<String, String>> getProfileApplicationsByPackage() {
		return packageToProfileApplications;
	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		if (uri.startsWith(XMIResource.XMI_NAMESPACE_PREFIX) || uri.startsWith(XMIResource.XMI_2_4_NAMESPACE_PREFIX)) {
			xmiType = qname(prefix, "type"); //$NON-NLS-1$
			xmiID = qname(prefix, "id"); //$NON-NLS-1$
		} else if (EPackage.Registry.INSTANCE.getEPackage(uri) == UMLPackage.eINSTANCE) {
			umlNamespace = uri;
			umlPrefix = prefix;

			initializeUMLElementNames();
		}
	}

	protected void initializeUMLElementNames() {
		packageTypes = ImmutableSet.of(umlElement("Package"), umlElement("Model"), umlElement("Profile")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		eAnnotations = "eAnnotations"; //$NON-NLS-1$
		references = "references"; //$NON-NLS-1$
	}

	protected final String umlElement(String name) {
		return qname(umlPrefix, name);
	}

	protected final String qname(String prefix, String name) {
		StringBuilder buf = new StringBuilder(prefix.length() + name.length() + 1);
		return buf.append(prefix).append(':').append(name).toString();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (umlNamespace.equals(uri) || (top != null)) {
			// skip over annotations
			if (!ignore(qName, attributes)) {
				push(qName, attributes);
				handleUMLElement(top, attributes);
			}
		}
	}

	protected boolean ignore(String qName, Attributes attributes) {
		boolean result = false;

		if (attributes != null) { // Starting an element
			result = (ignore > 0) || (eAnnotations.equals(qName) && !UMLUtil.UML2_UML_PACKAGE_2_0_NS_URI.equals(attributes.getValue("source"))); //$NON-NLS-1$
			if (result) {
				ignore++;
			}
		} else { // Ending an element
			result = (ignore > 0);
			if (result) {
				ignore--;
			}
		}

		return result;
	}

	protected final void push(String qName, Attributes attributes) {
		top = new UMLElement(qName, attributes);
	}

	protected final UMLElement pop() {
		UMLElement result = top;
		if (top != null) {
			top = top.parent;
		}
		return result;
	}

	protected void handleUMLElement(UMLElement element, Attributes attributes) throws SAXException {
		if (element.isPackage() && (element.getHREF() == null)) {
			// An href is a reference to a package, not a package in our element hierarchy
			currentPackage = element;
			enterPackage(currentPackage, attributes);
		}

		if (!doHandleUMLElement(element, attributes)) {
			if (element.isA(UMLUtil.UML2_UML_PACKAGE_2_0_NS_URI)) {
				// It's the applied profile definition annotation
				await.push(references);
			} else if (await.isAwaiting(element)) {
				if (element.isRole(references)) {
					handleAnnotationReferences(element);
				} else {
					handleAwaitedElement(element);
				}

				await.pop();
			}
		}
	}

	protected void enterPackage(UMLElement package_, Attributes attributes) {
		// Pass
	}

	protected void exitPackage(UMLElement package_) {
		// Pass
	}

	protected abstract boolean doHandleUMLElement(UMLElement element, Attributes attributes);

	protected final void await(String elementName) {
		await.push(elementName);
	}

	protected void handleAnnotationReferences(UMLElement references) {
		// Pass
	}

	protected abstract void handleAwaitedElement(UMLElement element);

	protected abstract void summarize();

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (!ignore(qName, null)) {
			if (await.stopAt(pop())) {
				await.pop();
			}

			if (top != null) {
				UMLElement newPackage = top.nearestPackage();
				if ((newPackage != currentPackage) && (currentPackage != null)) {
					exitPackage(currentPackage);
				}
				currentPackage = newPackage;
			}

			if (top == null) {
				// We're done with UML content
				summarize();
				throw new OperationCanceledException();
			}
		}
	}

	//
	// Nested types
	//

	protected final class UMLElement {
		final UMLElement parent;

		final String role;
		final String type;
		final String id;
		final String href;

		UMLElement(String qName, Attributes attributes) {
			parent = top;

			String type;
			if (qName.equals(eAnnotations)) {
				// "type" of an annotation is its source
				type = attributes.getValue("source"); //$NON-NLS-1$
			} else {
				type = attributes.getValue(xmiType);
				if (Strings.isNullOrEmpty(type)) {
					type = qName;
				}
			}

			this.role = qName;
			this.type = type;
			this.id = attributes.getValue(xmiID);
			this.href = attributes.getValue("href"); //$NON-NLS-1$
		}

		boolean isPackage() {
			return packageTypes.contains(type);
		}

		boolean isRole(String roleName) {
			return roleName.equals(role);
		}

		boolean isA(String xmiType) {
			return xmiType.equals(type);
		}

		URI getHREF() {
			return Strings.isNullOrEmpty(href) ? null : URI.createURI(href).resolve(fileURI);
		}

		UMLElement nearestPackage() {
			for (UMLElement next = this; next != null; next = next.parent) {
				if (next.isPackage()) {
					return next;
				}
			}
			return null;
		}
	}

	private final class Await {
		final Await parent = await;

		final String awaiting;
		final UMLElement limit;

		Await() {
			this(null);
		}

		private Await(String awaiting) {
			this.awaiting = awaiting;
			this.limit = top;
		}

		boolean isRoot() {
			return parent == null;
		}

		boolean isAwaiting(UMLElement element) {
			return !isRoot() && element.isRole(awaiting);
		}

		boolean stopAt(UMLElement element) {
			return !isRoot() && (limit == element);
		}

		Await push(String elementName) {
			Await result = new Await(elementName);
			await = result;
			return result;
		}

		void pop() {
			if (!isRoot()) {
				await = parent;
			}
		}
	}

	protected static final class URIPair {
		public URI first;
		public URI second;
	}
}

Back to the top