/
TEntityType.java
451 lines (377 loc) · 13.5 KB
/
TEntityType.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
/*******************************************************************************
* Copyright (c) 2013-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
package org.eclipse.winery.model.tosca.xml;
import java.util.List;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.namespace.QName;
import org.eclipse.winery.model.tosca.xml.kvproperties.WinerysPropertiesDefinition;
import org.eclipse.winery.model.tosca.xml.visitor.Visitor;
import io.github.adr.embedded.ADR;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "tEntityType", propOrder = {
"tags",
"derivedFrom",
"propertiesDefinition"
})
@XmlSeeAlso( {
TNodeType.class,
TRelationshipType.class,
TRequirementType.class,
TCapabilityType.class,
TArtifactType.class,
TPolicyType.class
})
public abstract class TEntityType extends TExtensibleElements implements HasName, HasInheritance, HasTargetNamespace {
public static final String NS_SUFFIX_PROPERTIESDEFINITION_WINERY = "propertiesdefinition/winery";
@XmlElement(name = "Tags")
protected TTags tags;
@XmlElement(name = "DerivedFrom")
protected TEntityType.DerivedFrom derivedFrom;
@XmlElement(name = "PropertiesDefinition")
protected TEntityType.PropertiesDefinition propertiesDefinition;
@XmlAttribute(name = "name", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String name;
@XmlAttribute(name = "abstract")
protected TBoolean _abstract;
@XmlAttribute(name = "final")
protected TBoolean _final;
@XmlAttribute(name = "targetNamespace")
@XmlSchemaType(name = "anyURI")
protected String targetNamespace;
public TEntityType() {
}
public TEntityType(Builder builder) {
super(builder);
this.tags = builder.tags;
this.derivedFrom = builder.derivedFrom;
this.propertiesDefinition = builder.propertiesDefinition;
this.name = builder.name;
this._abstract = builder.abstractValue;
this._final = builder.finalValue;
this.targetNamespace = builder.targetNamespace;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TEntityType)) return false;
TEntityType that = (TEntityType) o;
return Objects.equals(tags, that.tags) &&
Objects.equals(derivedFrom, that.derivedFrom) &&
Objects.equals(propertiesDefinition, that.propertiesDefinition) &&
Objects.equals(name, that.name) &&
_abstract == that._abstract &&
_final == that._final &&
Objects.equals(targetNamespace, that.targetNamespace);
}
@Override
public int hashCode() {
return Objects.hash(tags, derivedFrom, propertiesDefinition, name, _abstract, _final, targetNamespace);
}
@Nullable
public TTags getTags() {
return tags;
}
public void setTags(@Nullable TTags value) {
this.tags = value;
}
public TEntityType.@Nullable DerivedFrom getDerivedFrom() {
return derivedFrom;
}
public void setDerivedFrom(@Nullable HasType value) {
this.derivedFrom = (TEntityType.DerivedFrom) value;
}
public TEntityType.@Nullable PropertiesDefinition getPropertiesDefinition() {
return propertiesDefinition;
}
public void setPropertiesDefinition(TEntityType.@Nullable PropertiesDefinition value) {
this.propertiesDefinition = value;
}
@ADR(22)
@Nullable
@Override
public String getName() {
return name;
}
@ADR(22)
@Override
public void setName(@Nullable String value) {
this.name = value;
}
@NonNull
public QName getQName() {
return QName.valueOf("{" + this.targetNamespace + "}" + this.name);
}
@NonNull
public TBoolean getAbstract() {
if (_abstract == null) {
return TBoolean.NO;
} else {
return _abstract;
}
}
public void setAbstract(@Nullable TBoolean value) {
this._abstract = value;
}
@NonNull
public TBoolean getFinal() {
if (_final == null) {
return TBoolean.NO;
} else {
return _final;
}
}
public void setFinal(@Nullable TBoolean value) {
this._final = value;
}
@Nullable
public String getTargetNamespace() {
return targetNamespace;
}
public void setTargetNamespace(@Nullable String value) {
this.targetNamespace = value;
}
/**
* This is a special method for Winery. Winery allows to define a property definition by specifying name/type
* values. Instead of parsing the extensible elements returned TDefinitions, this method is a convenience method to
* access this information
*
* @return a WinerysPropertiesDefinition object, which includes a map of name/type-pairs denoting the associated
* property definitions. A default element name and namespace is added if it is not defined in the underlying XML.
* null if no Winery specific KV properties are defined for the given entity type
*/
@XmlTransient
public WinerysPropertiesDefinition getWinerysPropertiesDefinition() {
// similar implementation as org.eclipse.winery.repository.resources.entitytypes.properties.PropertiesDefinitionResource.getListFromEntityType(TEntityType)
WinerysPropertiesDefinition res = null;
for (Object o : this.getAny()) {
if (o instanceof WinerysPropertiesDefinition) {
res = (WinerysPropertiesDefinition) o;
}
}
if (res != null) {
// we put defaults if elementname and namespace have not been set
if (res.getElementName() == null) {
res.setElementName("Properties");
}
if (res.getNamespace() == null) {
// we use the targetnamespace of the original element
String ns = this.getTargetNamespace();
if (!ns.endsWith("/")) {
ns += "/";
}
ns += NS_SUFFIX_PROPERTIESDEFINITION_WINERY;
res.setNamespace(ns);
}
}
return res;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class DerivedFrom implements HasType {
@XmlAttribute(name = "typeRef", required = true)
protected QName typeRef;
@NonNull
public QName getTypeRef() {
return typeRef;
}
public void setTypeRef(@NonNull QName value) {
Objects.requireNonNull(value);
this.typeRef = value;
}
@NonNull
public QName getType() {
return this.getTypeRef();
}
@Override
public void setType(@NonNull QName type) {
Objects.requireNonNull(type);
this.setTypeRef(type);
}
@Override
@NonNull
public QName getTypeAsQName() {
return this.getType();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DerivedFrom that = (DerivedFrom) o;
return Objects.equals(typeRef, that.typeRef);
}
@Override
public int hashCode() {
return Objects.hash(typeRef);
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class PropertiesDefinition {
@XmlAttribute(name = "element")
protected QName element;
@XmlAttribute(name = "type")
protected QName type;
@Nullable
public QName getElement() {
return element;
}
public void setElement(@Nullable QName value) {
this.element = value;
}
@Nullable
public QName getType() {
return type;
}
public void setType(@Nullable QName value) {
this.type = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PropertiesDefinition that = (PropertiesDefinition) o;
return Objects.equals(element, that.element) &&
Objects.equals(type, that.type);
}
@Override
public int hashCode() {
return Objects.hash(element, type);
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
@ADR(11)
public abstract static class Builder<T extends Builder<T>> extends TExtensibleElements.Builder<T> {
private final String name;
private TTags tags;
private TEntityType.DerivedFrom derivedFrom;
private TEntityType.PropertiesDefinition propertiesDefinition;
private TBoolean abstractValue;
private TBoolean finalValue;
private String targetNamespace;
public Builder(String name) {
this.name = name;
}
public Builder(TEntityType entityType) {
super(entityType);
this.name = entityType.getName();
this.derivedFrom = entityType.getDerivedFrom();
this.addTags(entityType.getTags());
this.abstractValue = entityType.getAbstract();
this.finalValue = entityType.getFinal();
this.targetNamespace = entityType.getTargetNamespace();
this.propertiesDefinition = entityType.getPropertiesDefinition();
}
public T setTags(TTags tags) {
this.tags = tags;
return self();
}
public T setDerivedFrom(TEntityType.DerivedFrom derivedFrom) {
this.derivedFrom = derivedFrom;
return self();
}
public T setDerivedFrom(QName derivedFrom) {
if (derivedFrom == null) {
return self();
}
if (this.derivedFrom == null) {
this.derivedFrom = new TEntityType.DerivedFrom();
}
this.derivedFrom.setTypeRef(derivedFrom);
return self();
}
public T setDerivedFrom(String derivedFrom) {
if (derivedFrom == null || derivedFrom.length() == 0) {
return self();
}
return setDerivedFrom(new QName(derivedFrom));
}
public T setPropertiesDefinition(PropertiesDefinition propertiesDefinition) {
this.propertiesDefinition = propertiesDefinition;
return self();
}
public T setAbstract(TBoolean abstractValue) {
this.abstractValue = abstractValue;
return self();
}
public T setAbstract(Boolean abstractValue) {
return setAbstract(abstractValue ? TBoolean.YES : TBoolean.NO);
}
public T setFinal(TBoolean finalValue) {
this.finalValue = finalValue;
return self();
}
public T setFinal(Boolean finalValue) {
return setFinal(finalValue ? TBoolean.YES : TBoolean.NO);
}
public T setTargetNamespace(String targetNamespace) {
this.targetNamespace = targetNamespace;
return self();
}
public T addTags(TTags tags) {
if (tags == null || tags.getTag().isEmpty()) {
return self();
}
if (this.tags == null) {
this.tags = tags;
} else {
this.tags.getTag().addAll(tags.getTag());
}
return self();
}
public T addTags(List<TTag> tags) {
if (tags == null) {
return self();
}
TTags tmp = new TTags();
tmp.getTag().addAll(tags);
return addTags(tmp);
}
public T addTags(TTag tags) {
if (tags == null) {
return self();
}
TTags tmp = new TTags();
tmp.getTag().add(tags);
return addTags(tmp);
}
public T addTags(String key, String value) {
if (value == null) {
return self();
}
TTag tag = new TTag();
tag.setName(key);
tag.setValue(value);
return addTags(tag);
}
public TEntityType build() {
throw new IllegalStateException("Abstract types must never be build.");
}
}
}