/
UMLHandler.java
118 lines (93 loc) · 3.28 KB
/
UMLHandler.java
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
/*
* Copyright (c) 2005, 2018 IBM Corporation, Embarcadero Technologies, CEA, and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM - initial API and implementation
* Kenn Hussey (Embarcadero Technologies) - 204200
* Kenn Hussey (CEA) - 173843, 430118, 497222
* Kenn Hussey - 535301
*
*/
package org.eclipse.uml2.uml.internal.resource;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
import org.eclipse.emf.ecore.xmi.XMLHelper;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.SAXXMIHandler;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.eclipse.uml2.uml.Profile;
public class UMLHandler
extends SAXXMIHandler {
protected final Map<URI, Map<String, EObject>> urisToProxies = new HashMap<URI, Map<String, EObject>>();
public UMLHandler(XMLResource xmiResource, XMLHelper helper,
Map<?, ?> options) {
super(xmiResource, helper, options);
}
@Override
public void endElement(String uri, String localName, String name) {
EObject peek = objects.peek();
if (peek instanceof Profile && !peek.eIsProxy()) {
handleForwardReferences();
}
super.endElement(uri, localName, name);
}
@Override
public void endDocument() {
super.endDocument();
if (extendedMetaData != null) {
for (EPackage demandedPackage : extendedMetaData.demandedPackages()) {
String nsURI = demandedPackage.getNsURI();
if (nsURI != null) {
if (urisToLocations != null) {
URI locationURI = urisToLocations.get(nsURI);
if (locationURI != null) {
Resource resource = new ResourceImpl();
resource.setURI(locationURI.trimFragment());
resource.getContents().add(demandedPackage);
}
}
for (Map.Entry<String, String> entry : helper
.getPrefixToNamespaceMap()) {
if (nsURI.equals(entry.getValue())) {
demandedPackage.setNsPrefix(entry.getKey());
}
}
}
}
}
}
@Override
protected EObject validateCreateObjectFromFactory(EFactory factory,
String typeName, EObject newObject, EStructuralFeature feature) {
if (!(objects.peek() instanceof AnyType) && newObject != null
&& newObject.eIsProxy() && !sameDocumentProxies.contains(newObject)) {
URI proxyURI = ((InternalEObject) newObject).eProxyURI();
Map<String, EObject> typeNamesToProxies = urisToProxies
.get(proxyURI);
if (typeNamesToProxies == null) {
urisToProxies.put(proxyURI,
typeNamesToProxies = new HashMap<String, EObject>());
}
EObject proxy = typeNamesToProxies.get(typeName);
if (proxy == null) {
typeNamesToProxies.put(typeName, proxy = newObject);
}
// canonicalize proxies
newObject = proxy;
}
return super.validateCreateObjectFromFactory(factory, typeName,
newObject, feature);
}
}