-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMapping.java
494 lines (430 loc) · 17.1 KB
/
Mapping.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package org.hisrc.jsonix.definition;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.lang3.Validate;
import org.hisrc.jsonix.analysis.DefaultInfoVertexVisitor;
import org.hisrc.jsonix.analysis.DependencyEdge;
import org.hisrc.jsonix.analysis.DependencyType;
import org.hisrc.jsonix.analysis.ElementInfoVertex;
import org.hisrc.jsonix.analysis.InfoVertex;
import org.hisrc.jsonix.analysis.InfoVertexVisitor;
import org.hisrc.jsonix.analysis.ModelInfoGraphAnalyzer;
import org.hisrc.jsonix.analysis.PackageInfoVertex;
import org.hisrc.jsonix.analysis.PropertyInfoVertex;
import org.hisrc.jsonix.analysis.TypeInfoVertex;
import org.hisrc.jsonix.context.JsonixContext;
import org.jgrapht.DirectedGraph;
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MEnumLeafInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPackageInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfoVisitor;
import org.jvnet.jaxb2_commons.xml.bind.model.util.DefaultTypeInfoVisitor;
import org.slf4j.Logger;
public class Mapping<T, C extends T> {
private final Logger logger;
private final ModelInfoGraphAnalyzer<T, C> analyzer;
private final MPackageInfo packageInfo;
private final Collection<MClassInfo<T, C>> classInfos = new HashSet<MClassInfo<T, C>>();
private final Collection<MPropertyInfo<T, C>> propertyInfos = new HashSet<MPropertyInfo<T, C>>();
private final Collection<MEnumLeafInfo<T, C>> enumLeafInfos = new HashSet<MEnumLeafInfo<T, C>>();
private final Collection<MElementInfo<T, C>> elementInfos = new HashSet<MElementInfo<T, C>>();
private final Collection<InfoVertex<T, C>> infoVertices = new HashSet<InfoVertex<T, C>>();
private final String packageName;
private final String mappingName;
private final String schemaId;
private final String mappingStyle;
private final String targetNamespaceURI;
private final String defaultElementNamespaceURI;
private final String defaultAttributeNamespaceURI;
private final Map<InfoVertex<T, C>, ContainmentType> verticesContainmentMap = new HashMap<InfoVertex<T, C>, ContainmentType>();
public Mapping(JsonixContext context,
ModelInfoGraphAnalyzer<T, C> analyzer, MPackageInfo packageInfo,
String mappingName, String schemaId, String mappingStyle, String targetNamespaceURI,
String defaultElementNamespaceURI,
String defaultAttributeNamespaceURI) {
this.logger = Validate.notNull(context).getLoggerFactory()
.getLogger(Mapping.class.getName());
Validate.notNull(analyzer);
Validate.notNull(packageInfo);
Validate.notNull(mappingName);
Validate.notNull(schemaId);
Validate.notNull(mappingStyle);
Validate.notNull(defaultElementNamespaceURI);
Validate.notNull(defaultAttributeNamespaceURI);
this.analyzer = analyzer;
this.packageInfo = packageInfo;
this.packageName = packageInfo.getPackageName();
this.mappingName = mappingName;
this.schemaId = schemaId;
this.mappingStyle = mappingStyle;
this.targetNamespaceURI = targetNamespaceURI;
this.defaultElementNamespaceURI = defaultElementNamespaceURI;
this.defaultAttributeNamespaceURI = defaultAttributeNamespaceURI;
}
public boolean isEmpty() {
return this.elementInfos.isEmpty() && this.classInfos.isEmpty()
&& this.enumLeafInfos.isEmpty();
}
public void includePackage(MPackageInfo packageInfo) {
Validate.notNull(packageInfo);
final PackageInfoVertex<T, C> vertex = new PackageInfoVertex<T, C>(
packageInfo);
includeInfoVertex(vertex);
}
public void includePropertyInfo(MPropertyInfo<T, C> propertyInfo) {
Validate.notNull(propertyInfo);
final PropertyInfoVertex<T, C> vertex = new PropertyInfoVertex<T, C>(
propertyInfo);
includeInfoVertex(vertex);
}
public void includeElementInfo(MElementInfo<T, C> elementInfo) {
Validate.notNull(elementInfo);
final ElementInfoVertex<T, C> vertex = new ElementInfoVertex<T, C>(
elementInfo);
includeInfoVertex(vertex);
}
public void includeTypeInfo(MTypeInfo<T, C> typeInfo) {
Validate.notNull(typeInfo);
final TypeInfoVertex<T, C> vertex = new TypeInfoVertex<T, C>(
this.packageInfo, typeInfo);
includeInfoVertex(vertex);
}
public void includeDependenciesOfMapping(Mapping<T, C> mapping) {
Validate.notNull(mapping);
final Map<InfoVertex<T, C>, ContainmentType> dependendMappingVerticesContainmentMap = mapping.verticesContainmentMap;
for (Map.Entry<InfoVertex<T, C>, ContainmentType> entry : dependendMappingVerticesContainmentMap
.entrySet()) {
final ContainmentType dependendMappingVertexContainmentType = entry
.getValue();
// If a given vertex is excluded in the dependend mapping
if (!dependendMappingVertexContainmentType.isIncluded()) {
// Check if this vertex is included in the given map
final InfoVertex<T, C> dependendMappingVertex = entry.getKey();
final ContainmentType containmentType = this.verticesContainmentMap
.get(dependendMappingVertex);
final ContainmentType newContainmentType = dependendMappingVertexContainmentType
.combineWith(containmentType);
if (newContainmentType != containmentType) {
this.verticesContainmentMap.put(dependendMappingVertex,
newContainmentType);
}
}
}
for (Map.Entry<InfoVertex<T, C>, ContainmentType> entry : dependendMappingVerticesContainmentMap
.entrySet()) {
final ContainmentType dependendMappingVertexContainmentType = entry
.getValue();
// If a given vertex is excluded in the dependend mapping
if (dependendMappingVertexContainmentType.isIncluded()) {
final InfoVertex<T, C> dependendMappingVertex = entry.getKey();
includeInfoVertex(dependendMappingVertex);
}
}
}
public Collection<MappingDependency<T, C>> getDirectDependencies() {
final Map<MPackageInfo, MappingDependency<T, C>> dependencies = new HashMap<MPackageInfo, MappingDependency<T, C>>();
final DirectedGraph<InfoVertex<T, C>, DependencyEdge> graph = analyzer
.getGraph();
final Collection<InfoVertex<T, C>> vertices = new HashSet<InfoVertex<T, C>>(
getInfoVertices());
for (InfoVertex<T, C> sourceVertex : vertices) {
final Set<DependencyEdge> edges = graph
.outgoingEdgesOf(sourceVertex);
for (DependencyEdge edge : edges) {
if (edge.getType() == DependencyType.HARD) {
final InfoVertex<T, C> targetVertex = graph
.getEdgeTarget(edge);
final MPackageInfo packageInfo = targetVertex
.getPackageInfo();
// If this another package (not this package and not the
// built-in package)
if (packageInfo != null
&& !this.packageInfo.equals(packageInfo)) {
MappingDependency<T, C> dependency = dependencies
.get(packageInfo);
if (dependency == null) {
dependency = new MappingDependency<T, C>(
packageInfo);
dependencies.put(packageInfo, dependency);
}
dependency.addInfoVertex(targetVertex);
}
}
}
}
return dependencies.values();
}
public void excludePropertyInfo(MPropertyInfo<T, C> propertyInfo) {
Validate.notNull(propertyInfo);
final PropertyInfoVertex<T, C> vertex = new PropertyInfoVertex<T, C>(
propertyInfo);
excludeInfoVertex(vertex);
}
public void excludeElementInfo(MElementInfo<T, C> elementInfo) {
Validate.notNull(elementInfo);
final ElementInfoVertex<T, C> vertex = new ElementInfoVertex<T, C>(
elementInfo);
excludeInfoVertex(vertex);
}
public void excludeTypeInfo(MTypeInfo<T, C> typeInfo) {
Validate.notNull(typeInfo);
final TypeInfoVertex<T, C> vertex = new TypeInfoVertex<T, C>(
this.packageInfo, typeInfo);
excludeInfoVertex(vertex);
}
private void includeInfoVertex(final InfoVertex<T, C> initialVertex) {
logger.trace(MessageFormat.format("Including the vertex [{0}].",
initialVertex));
final DirectedGraph<InfoVertex<T, C>, DependencyEdge> graph = analyzer
.getGraph();
final SortedMap<ContainmentType, Deque<InfoVertex<T, C>>> deques = new TreeMap<ContainmentType, Deque<InfoVertex<T, C>>>();
deques.put(ContainmentType.INCLUDED_EXPLICITLY,
new LinkedList<InfoVertex<T, C>>());
deques.put(ContainmentType.INCLUDED_AS_HARD_DEPENDENCY,
new LinkedList<InfoVertex<T, C>>());
deques.put(ContainmentType.INCLUDED_AS_SOFT_DEPENDENCY,
new LinkedList<InfoVertex<T, C>>());
deques.get(ContainmentType.INCLUDED_EXPLICITLY).add(initialVertex);
for (Map.Entry<ContainmentType, Deque<InfoVertex<T, C>>> dequeEntry : deques
.entrySet()) {
final ContainmentType dequeContainmentType = dequeEntry.getKey();
final Deque<InfoVertex<T, C>> deque = dequeEntry.getValue();
while (!deque.isEmpty()) {
final InfoVertex<T, C> sourceVertex = deque.removeFirst();
final ContainmentType currentSourceContainmentType = getInfoVertexContainmentType(sourceVertex);
final ContainmentType sourceContainmentType = dequeContainmentType
.combineWith(currentSourceContainmentType);
if (currentSourceContainmentType == null
|| currentSourceContainmentType
.compareTo(sourceContainmentType) > 0) {
if (currentSourceContainmentType != null
&& !currentSourceContainmentType.isIncluded()) {
logger.warn(MessageFormat
.format("The vertex [{0}] was excluded with the containment type [{1}], but it must be included with containment type [{2}], otherwise mappings will not be consistent.",
sourceVertex,
currentSourceContainmentType,
sourceContainmentType));
} else {
logger.trace(MessageFormat
.format("Including the vertex [{0}] with the containment type [{1}].",
sourceVertex, dequeContainmentType));
}
setInfoVertexContainmentType(sourceVertex,
sourceContainmentType);
final Set<DependencyEdge> edges = graph
.outgoingEdgesOf(sourceVertex);
for (DependencyEdge edge : edges) {
final InfoVertex<T, C> targetVertex = graph
.getEdgeTarget(edge);
final DependencyType dependencyType = edge.getType();
final ContainmentType targetContainmentType = dependencyType
.combineWith(sourceContainmentType);
if (targetContainmentType != null) {
final Deque<InfoVertex<T, C>> targetDeque = deques
.get(targetContainmentType);
if (targetDeque != null) {
logger.trace(MessageFormat
.format("Queueing the inclusion of the vertex [{0}] with the containment type [{1}].",
targetVertex,
targetContainmentType));
targetDeque.add(targetVertex);
}
}
}
} else {
logger.trace(MessageFormat
.format("Vertex [{0}] is already included with the containment type [{1}].",
sourceVertex, currentSourceContainmentType));
}
}
}
}
private ContainmentType getInfoVertexContainmentType(
final InfoVertex<T, C> sourceVertex) {
ContainmentType sourceContainmentType = verticesContainmentMap
.get(sourceVertex);
return sourceContainmentType;
}
private void excludeInfoVertex(final InfoVertex<T, C> vertex) {
Validate.notNull(vertex);
logger.trace(MessageFormat.format("Excluding [{0}].", vertex));
final DirectedGraph<InfoVertex<T, C>, DependencyEdge> graph = analyzer
.getGraph();
final SortedMap<ContainmentType, Deque<InfoVertex<T, C>>> deques = new TreeMap<ContainmentType, Deque<InfoVertex<T, C>>>();
deques.put(ContainmentType.EXCLUDED_EXPLICITLY,
new LinkedList<InfoVertex<T, C>>());
deques.put(ContainmentType.EXCLUDED_AS_HARD_DEPENDENCY,
new LinkedList<InfoVertex<T, C>>());
deques.get(ContainmentType.EXCLUDED_EXPLICITLY).add(vertex);
for (Map.Entry<ContainmentType, Deque<InfoVertex<T, C>>> dequeEntry : deques
.entrySet()) {
final ContainmentType dequeContainmentType = dequeEntry.getKey();
final Deque<InfoVertex<T, C>> deque = dequeEntry.getValue();
while (!deque.isEmpty()) {
final InfoVertex<T, C> targetVertex = deque.removeFirst();
final ContainmentType currentTargetContainmentType = getInfoVertexContainmentType(targetVertex);
final ContainmentType targetContainmentType = dequeContainmentType
.combineWith(currentTargetContainmentType);
if (currentTargetContainmentType == null
|| currentTargetContainmentType
.compareTo(targetContainmentType) > 0) {
logger.trace(MessageFormat
.format("Excluding the vertex [{0}] with the containment type [{1}].",
targetVertex, targetContainmentType));
setInfoVertexContainmentType(targetVertex,
targetContainmentType);
final Set<DependencyEdge> edges = graph
.incomingEdgesOf(targetVertex);
for (DependencyEdge edge : edges) {
final InfoVertex<T, C> sourceVertex = graph
.getEdgeSource(edge);
final DependencyType dependencyType = edge.getType();
final ContainmentType sourceContainmentType = dependencyType
.combineWith(targetContainmentType);
if (sourceContainmentType != null) {
final Deque<InfoVertex<T, C>> sourceDeque = deques
.get(sourceContainmentType);
if (sourceDeque != null) {
logger.trace(MessageFormat
.format("Queueing the exclusion of the vertex [{0}] with the containment type [{1}].",
sourceVertex,
sourceContainmentType));
sourceDeque.add(sourceVertex);
}
}
}
} else {
logger.trace(MessageFormat
.format("Vertex [{0}] is already excluded with the containment type [{1}].",
targetVertex, targetContainmentType));
}
}
}
}
private final InfoVertexVisitor<T, C, Void> infoVertexAdder = new DefaultInfoVertexVisitor<T, C, Void>() {
@Override
public Void visitTypeInfoVertex(TypeInfoVertex<T, C> vertex) {
addTypeInfo(vertex.getTypeInfo());
return null;
}
@Override
public Void visitElementInfoVertex(ElementInfoVertex<T, C> vertex) {
addElementInfo(vertex.getElementInfo());
return null;
}
@Override
public Void visitPropertyInfoVertex(PropertyInfoVertex<T, C> vertex) {
addPropertyInfo(vertex.getPropertyInfo());
return null;
}
};
private void setInfoVertexContainmentType(InfoVertex<T, C> vertex,
ContainmentType containmentType) {
Validate.notNull(vertex);
verticesContainmentMap.put(vertex, containmentType);
if (containmentType.isIncluded()) {
vertex.accept(infoVertexAdder);
}
}
private final MTypeInfoVisitor<T, C, Void> typeInfoAdder = new DefaultTypeInfoVisitor<T, C, Void>() {
@Override
public Void visitClassInfo(MClassInfo<T, C> info) {
Mapping.this.addClassInfo(info);
return null;
}
@Override
public Void visitEnumLeafInfo(MEnumLeafInfo<T, C> info) {
Mapping.this.addEnumLeafInfo(info);
return null;
}
};
private void addTypeInfo(MTypeInfo<T, C> typeInfo) {
Validate.notNull(typeInfo);
typeInfo.acceptTypeInfoVisitor(typeInfoAdder);
}
private void addElementInfo(MElementInfo<T, C> elementInfo) {
Validate.notNull(elementInfo);
if (this.packageInfo.equals(elementInfo.getPackageInfo())) {
this.elementInfos.add(elementInfo);
this.infoVertices.add(new ElementInfoVertex<T, C>(elementInfo));
}
}
private void addClassInfo(MClassInfo<T, C> classInfo) {
Validate.notNull(classInfo);
if (this.packageInfo.equals(classInfo.getPackageInfo())) {
this.classInfos.add(classInfo);
this.infoVertices.add(new TypeInfoVertex<T, C>(classInfo
.getPackageInfo(), classInfo));
}
}
private void addEnumLeafInfo(MEnumLeafInfo<T, C> enumLeafInfo) {
Validate.notNull(enumLeafInfo);
if (this.packageInfo.equals(enumLeafInfo.getPackageInfo())) {
this.enumLeafInfos.add(enumLeafInfo);
this.infoVertices.add(new TypeInfoVertex<T, C>(enumLeafInfo
.getPackageInfo(), enumLeafInfo));
}
}
private void addPropertyInfo(MPropertyInfo<T, C> propertyInfo) {
Validate.notNull(propertyInfo);
if (this.packageInfo.equals(propertyInfo.getClassInfo()
.getPackageInfo())) {
this.propertyInfos.add(propertyInfo);
this.infoVertices.add(new PropertyInfoVertex<T, C>(propertyInfo));
}
}
private Collection<InfoVertex<T, C>> getInfoVertices() {
return infoVertices;
}
public MPackageInfo getPackageInfo() {
return packageInfo;
}
public String getPackageName() {
return packageName;
}
public String getMappingName() {
return mappingName;
}
public String getSchemaId() {
return this.schemaId;
}
public String getMappingStyle() {
return this.mappingStyle;
}
public String getTargetNamespaceURI() {
return targetNamespaceURI;
}
public String getDefaultElementNamespaceURI() {
return defaultElementNamespaceURI;
}
public String getDefaultAttributeNamespaceURI() {
return defaultAttributeNamespaceURI;
}
public Collection<MClassInfo<T, C>> getClassInfos() {
return this.classInfos;
}
public Collection<MPropertyInfo<T, C>> getPropertyInfos() {
return propertyInfos;
}
public Collection<MEnumLeafInfo<T, C>> getEnumLeafInfos() {
return this.enumLeafInfos;
}
public Collection<MElementInfo<T, C>> getElementInfos() {
return this.elementInfos;
}
@Override
public String toString() {
return MessageFormat.format("Mapping [{0}]", this.mappingName);
}
}