This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
MappedModelUtil.java
228 lines (214 loc) · 7.84 KB
/
MappedModelUtil.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
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
/**
* Copyright 2011 The University of North Carolina at Chapel Hill
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package crosswalk.diagram.custom;
import gov.loc.mods.mods.DateEncodingAttributeDefinition;
import gov.loc.mods.mods.MODSPackage;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import crosswalk.ContextProvider;
import crosswalk.CrossWalk;
import crosswalk.CrosswalkFactory;
import crosswalk.MappedAttribute;
import crosswalk.MappedElement;
import crosswalk.MappingContainer;
import crosswalk.OutputElement;
import crosswalk.impl.MappedElementImpl;
/**
* @author Gregory Jansen
*
*/
public class MappedModelUtil {
/**
* For the given mappable parent object, looks up the possible element children
*
* @param parent
* a CrossWalk or MappedElement eObject
* @return a list of possible child reference types
*/
public static List<EStructuralFeature> getChildElementFeatures(EObject parent) {
// LOG.debug("model parent:" + parent);
EClass mappedParentType = null;
List<MappedElement> elementsMappedAlready = new ArrayList<MappedElement>();
if (parent instanceof MappingContainer) {
MappingContainer mapContainer = (MappingContainer) parent;
for(OutputElement e : mapContainer.getElements()) {
if(e instanceof MappedElement) elementsMappedAlready.add((MappedElement)e);
}
for (EObject next = parent.eContainer(); next != null; next = next.eContainer()) {
if (next instanceof ContextProvider) {
EClass aMappedParentType = ((ContextProvider) next).getOutputType();
if(aMappedParentType != null) {
mappedParentType = aMappedParentType;
break;
}
}
}
} else if (parent instanceof MappedElement) {
MappedElement pe = (MappedElement) parent;
elementsMappedAlready.addAll(pe.getChildElements());
if (pe.getMappedFeature() != null) {
mappedParentType = (EClass) pe.getMappedFeature().getEType();
}
} else {
throw new IllegalArgumentException("Expecting a CrossWalk or MappedElement");
}
// LOG.debug("mappedParentType:" + mappedParentType);
if (mappedParentType == null) {
throw new Error("No feature was mapped on the given parent");
}
List<EStructuralFeature> result = new ArrayList<EStructuralFeature>();
// result.addAll(mappedParentType.getEAllReferences());
// FIXME remove the elements that are already full
for (EStructuralFeature a : mappedParentType.getEAllReferences().toArray(new EStructuralFeature[0])) {
int count = 0;
for (MappedElement m : elementsMappedAlready) {
if (m.getMappedFeature() != null && m.getMappedFeature().equals(a))
count++;
}
if (a.getUpperBound() < 0 || count < a.getUpperBound()) {
result.add(a);
}
}
return result;
}
/**
* @param el
* @return
*/
public static List<EStructuralFeature> getAttributes(EObject parent) {
// System.out.println("get attributes for: "+parent);
EClass mappedParentType = null;
List<MappedAttribute> attsMappedAlready = new ArrayList<MappedAttribute>();
if (parent instanceof CrossWalk) {
CrossWalk cw = (CrossWalk) parent;
mappedParentType = cw.getOutputType();
} else if (parent instanceof MappedElement) {
MappedElement pe = (MappedElement) parent;
attsMappedAlready.addAll(pe.getAttributes());
if (pe.getMappedFeature() != null) {
mappedParentType = (EClass) pe.getMappedFeature().getEType();
}
} else {
throw new IllegalArgumentException("Expecting a CrossWalk or MappedElement");
}
if (mappedParentType == null) {
throw new Error("No feature was mapped on the given parent");
}
List<EStructuralFeature> result = new ArrayList<EStructuralFeature>();
// FIXME remove the elements that are already full
for (EAttribute a : mappedParentType.getEAllAttributes().toArray(new EAttribute[0])) {
int count = 0;
for (MappedAttribute m : attsMappedAlready) {
if (m.getMappedFeature().equals(a))
count++;
}
if (a.getUpperBound() < 0 || count < a.getUpperBound()) {
result.add(a);
}
}
return result;
}
/**
* @param me
*/
public static void setMODSDateEncodingAttribute(MappedElementImpl me) {
MappedAttribute encodingAttr = null;
for (MappedAttribute ma : me.getAttributes()) {
if (MODSPackage.eINSTANCE.getDateBaseDefinition_Encoding().equals(ma.getMappedFeature())) {
encodingAttr = ma;
break;
}
}
if (encodingAttr == null) {
encodingAttr = CrosswalkFactory.eINSTANCE.createMappedAttribute();
encodingAttr.setMappedFeature(MODSPackage.eINSTANCE.getDateBaseDefinition_Encoding());
me.getAttributes().add(encodingAttr);
}
encodingAttr.setDefaultValue(DateEncodingAttributeDefinition.ISO8601.getLiteral());
}
/**
* @param me
* @param textAttr
*/
public static void setEDateToStringConversionStrategyAttribute(MappedElementImpl me, EAttribute textAttr) {
MappedAttribute mappedTextAttribute = getXMLTextMappedAttribute(me);
if (mappedTextAttribute == null) {
mappedTextAttribute = CrosswalkFactory.eINSTANCE.createMappedAttribute();
mappedTextAttribute.setMappedFeature(textAttr);
me.getAttributes().add(mappedTextAttribute);
}
mappedTextAttribute.setConversionStrategy(CrosswalkFactory.eINSTANCE.createDateToISO8601StringConversion());
}
public static EAttribute getXMLTextAttributeInReferenceType(MappedElement me) {
EAttribute result = null;
// TODO check for annotation <eAnnotations
// source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
// <details key="name" value=":0"/>
// find the value attribute of type String with Annotation kind=simple
for (EAttribute a : me.getMappedFeature().getEReferenceType().getEAllAttributes()) {
if (MappedModelUtil.isXMLTextValueEAttribute(a)) {
return a;
}
}
return result;
}
/**
* @param me
*/
public static void setTextAttribute(MappedElementImpl me, EAttribute textValue) {
MappedAttribute mappedTextAttribute = getXMLTextMappedAttribute(me);
if (mappedTextAttribute == null) {
mappedTextAttribute = CrosswalkFactory.eINSTANCE.createMappedAttribute();
mappedTextAttribute.setMappedFeature(textValue);
me.getAttributes().add(mappedTextAttribute);
}
mappedTextAttribute.unsetConversionStrategy();
}
public static boolean isXMLTextValueEAttribute(EAttribute att) {
boolean result = false;
if (att != null) {
if ("value".equals(att.getName())) {
System.out.println("found an attribute named value: " + att);
EAnnotation note = att.getEAnnotation("http:///org/eclipse/emf/ecore/util/ExtendedMetaData");
if (note != null && note.getDetails() != null) {
System.out.println("found value attribute details: " + note.getDetails());
if (note.getDetails().get("name") != null && ":0".equals(note.getDetails().get("name"))) {
System.out.println("attribute name details match");
if (note.getDetails().get("kind") != null && "simple".equals(note.getDetails().get("kind"))) {
System.out.println("attribute kind details match");
return true;
}
}
}
}
}
return result;
}
public static MappedAttribute getXMLTextMappedAttribute(MappedElement me) {
MappedAttribute result = null;
for (MappedAttribute ma : me.getAttributes()) {
if (isXMLTextValueEAttribute(ma.getMappedFeature())) {
return ma;
}
}
return result;
}
}