Added
Link Here
|
1 |
/* |
2 |
* Copyright (c) 2007 Borland Software Corporation |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials are made |
5 |
* available under the terms of the Eclipse Public License v1.0 which |
6 |
* accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: Borland - initial API and implementation |
10 |
*/ |
11 |
package org.eclipse.gmf.internal.common.migrate; |
12 |
|
13 |
import java.util.Arrays; |
14 |
import java.util.Collection; |
15 |
import java.util.HashMap; |
16 |
import java.util.Iterator; |
17 |
import java.util.Map; |
18 |
|
19 |
import org.eclipse.emf.ecore.EClass; |
20 |
import org.eclipse.emf.ecore.EClassifier; |
21 |
import org.eclipse.emf.ecore.EFactory; |
22 |
import org.eclipse.emf.ecore.EObject; |
23 |
import org.eclipse.emf.ecore.EReference; |
24 |
import org.eclipse.emf.ecore.EStructuralFeature; |
25 |
import org.eclipse.emf.ecore.EcoreFactory; |
26 |
import org.eclipse.emf.ecore.EcorePackage; |
27 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
28 |
import org.eclipse.emf.ecore.xmi.impl.XMIHelperImpl; |
29 |
|
30 |
public class MigrationHelper extends XMIHelperImpl { |
31 |
private final MigrationHelperDelegate myDelegate; |
32 |
|
33 |
public MigrationHelper(XMLResource resource, MigrationHelperDelegate delegate) { |
34 |
super(resource); |
35 |
myDelegate = delegate; |
36 |
assert delegate != null; |
37 |
} |
38 |
|
39 |
public static class MigrationHelperDelegateImpl implements MigrationHelperDelegate { |
40 |
|
41 |
public MigrationHelperDelegateImpl() { |
42 |
super(); |
43 |
} |
44 |
|
45 |
private static EStructuralFeature ourDeletedAttribute = EcoreFactory.eINSTANCE.createEAttribute(); |
46 |
static { |
47 |
ourDeletedAttribute.setName("attributeIsDeleted"); //$NON-NLS-1$ |
48 |
ourDeletedAttribute.setEType(EcorePackage.eINSTANCE.getEString()); |
49 |
} |
50 |
|
51 |
private Map<EClassifier, Collection<String>> myDeletedAttributes = new HashMap<EClassifier, Collection<String>>(); |
52 |
private Map<EReference, EClass> myarrowReferenceTypes = new HashMap<EReference, EClass>(); |
53 |
private Map<EClass, Map<String, EStructuralFeature>> myRenamedAttributes = new HashMap<EClass, Map<String, EStructuralFeature>>(); |
54 |
private Map<String, EClassifier> myRenamedTypes = new HashMap<String, EClassifier>(); |
55 |
|
56 |
public void registerDeletedAttributes(EClassifier classifier, String... deletedAttrNames) { |
57 |
assert !myDeletedAttributes.containsKey(classifier); |
58 |
myDeletedAttributes.put(classifier, Arrays.asList(deletedAttrNames)); |
59 |
} |
60 |
|
61 |
public void registerNarrowReferenceType(EReference reference, EClass concreteType) { |
62 |
myarrowReferenceTypes.put(reference, concreteType); |
63 |
} |
64 |
|
65 |
public void registerRenamedAttributes(EClass eClass, Map<String, EStructuralFeature> renamedAttributes) { |
66 |
myRenamedAttributes.put(eClass, renamedAttributes); |
67 |
} |
68 |
|
69 |
public void registerRenamedType(String oldTypeName, EClassifier newType) { |
70 |
myRenamedTypes.put(oldTypeName, newType); |
71 |
} |
72 |
|
73 |
public void registerRenamedAttribute(EClass eClass, String oldName, EStructuralFeature newStructuralFeature) { |
74 |
Map<String, EStructuralFeature> renamedAttributes = myRenamedAttributes.get(eClass); |
75 |
if (renamedAttributes == null) { |
76 |
renamedAttributes = new HashMap<String, EStructuralFeature>(); |
77 |
} |
78 |
renamedAttributes.put(oldName, newStructuralFeature); |
79 |
registerRenamedAttributes(eClass, renamedAttributes); |
80 |
} |
81 |
|
82 |
public boolean isAttributeDeleted(EClass clazz, String name) { |
83 |
Collection<String> deletedAttributes = myDeletedAttributes.get(clazz); |
84 |
boolean result = deletedAttributes != null && deletedAttributes.contains(name); |
85 |
for (Iterator<EClass> it=clazz.getEAllSuperTypes().iterator(); !result && it.hasNext();) { |
86 |
EClass nextParent = it.next(); |
87 |
result = isAttributeDeleted(nextParent, name); |
88 |
} |
89 |
return result; |
90 |
} |
91 |
|
92 |
public EClass getNarrowReferenceType(EStructuralFeature feature) { |
93 |
return myarrowReferenceTypes.get(feature); |
94 |
} |
95 |
|
96 |
public EStructuralFeature getRenamedFeatureFor(EClass clazz, String name) { |
97 |
Map<String, EStructuralFeature> renamings = myRenamedAttributes.get(clazz); |
98 |
EStructuralFeature result = renamings != null ? renamings.get(name) : null; |
99 |
for (Iterator<EClass> it=clazz.getEAllSuperTypes().iterator(); result == null && it.hasNext();) { |
100 |
EClass nextParent = it.next(); |
101 |
result = getRenamedFeatureFor(nextParent, name); |
102 |
} |
103 |
return result; |
104 |
} |
105 |
|
106 |
public EClassifier getRenamedType(String typeName) { |
107 |
return myRenamedTypes.get(typeName); |
108 |
} |
109 |
|
110 |
public EClass getStructuralFeatureType(EStructuralFeature feature) { |
111 |
return getNarrowReferenceType(feature); |
112 |
} |
113 |
|
114 |
public boolean setValue(EObject object, EStructuralFeature feature, Object value, int position) { |
115 |
return ourDeletedAttribute.equals(feature); |
116 |
} |
117 |
|
118 |
public EStructuralFeature getFeature(EClass eClass, String namespaceURI, String name, boolean isElement) { |
119 |
EStructuralFeature result = null; |
120 |
EStructuralFeature rename = null; |
121 |
if (isAttributeDeleted(eClass, name)) { |
122 |
result = ourDeletedAttribute; |
123 |
} else if ((rename = getRenamedFeatureFor(eClass, name)) != null) { |
124 |
result = rename; |
125 |
} |
126 |
return result; |
127 |
} |
128 |
|
129 |
public EClassifier getType(EFactory factory, String typeName) { |
130 |
EClassifier result = null; |
131 |
EClassifier type = getRenamedType(typeName); |
132 |
if (type != null) { |
133 |
result = type; |
134 |
} |
135 |
return result; |
136 |
} |
137 |
|
138 |
public EObject createObject(EFactory factory, EClassifier type) { |
139 |
return null; |
140 |
} |
141 |
|
142 |
public void postProcess() { |
143 |
} |
144 |
|
145 |
public void processObject(EObject result) { |
146 |
} |
147 |
} |
148 |
|
149 |
public EObject createObject(EFactory factory, EClassifier type) { |
150 |
EObject result = myDelegate.createObject(factory, type); |
151 |
if (result == null) { |
152 |
result = super.createObject(factory, type); |
153 |
} |
154 |
myDelegate.processObject(result); |
155 |
return result; |
156 |
} |
157 |
|
158 |
public void setValue(EObject object, EStructuralFeature feature, Object value, int position) { |
159 |
if (!myDelegate.setValue(object, feature, value, position)) { |
160 |
super.setValue(object, feature, value, position); |
161 |
} |
162 |
} |
163 |
|
164 |
public EStructuralFeature getFeature(EClass eClass, String namespaceURI, String name, boolean isElement) { |
165 |
EStructuralFeature result = myDelegate.getFeature(eClass, namespaceURI, name, isElement); |
166 |
if (result == null) { |
167 |
result = super.getFeature(eClass, namespaceURI, name); |
168 |
} |
169 |
EClass narrow = myDelegate.getStructuralFeatureType(result); |
170 |
if (narrow != null) { |
171 |
result.setEType(narrow); |
172 |
} |
173 |
return result; |
174 |
} |
175 |
|
176 |
public EClassifier getType(EFactory factory, String typeName) { |
177 |
EClassifier result = myDelegate.getType(factory, typeName); |
178 |
if (result == null) { |
179 |
result = super.getType(factory, typeName); |
180 |
} |
181 |
return result; |
182 |
} |
183 |
|
184 |
public void popContext() { |
185 |
myDelegate.postProcess(); |
186 |
} |
187 |
} |