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 package gov.nist.secauto.metaschema.databind.model;
28
29 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
30 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
31 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
32 import gov.nist.secauto.metaschema.databind.model.annotations.BoundAssembly;
33 import gov.nist.secauto.metaschema.databind.model.info.IDataTypeHandler;
34
35 import java.lang.reflect.Field;
36 import java.util.Locale;
37
38 import edu.umd.cs.findbugs.annotations.NonNull;
39
40 class ClassBindingAssemblyProperty
41 extends AbstractNamedModelProperty
42 implements IBoundAssemblyInstance {
43
44 @NonNull
45 private final BoundAssembly assembly;
46 @NonNull
47 private final IAssemblyClassBinding definition;
48
49 protected ClassBindingAssemblyProperty(
50 @NonNull Field field,
51 @NonNull IAssemblyClassBinding definition,
52 @NonNull IAssemblyClassBinding parentClassBinding) {
53 super(field, parentClassBinding);
54 if (field.isAnnotationPresent(BoundAssembly.class)) {
55 this.assembly = ObjectUtils.notNull(field.getAnnotation(BoundAssembly.class));
56 } else {
57 throw new IllegalArgumentException(String.format("BoundField '%s' on class '%s' is missing the '%s' annotation.",
58 field.getName(), parentClassBinding.getBoundClass().getName(), BoundAssembly.class.getName()));
59 }
60 this.definition = definition;
61 }
62
63 @NonNull
64 private BoundAssembly getAssemblyAnnotation() {
65 return assembly;
66 }
67
68 @Override
69 protected IDataTypeHandler newDataTypeHandler() {
70 IClassBinding classBinding
71 = getParentClassBinding().getBindingContext().getClassBinding(getPropertyInfo().getItemType());
72 if (classBinding == null) {
73 throw new IllegalStateException(
74 String.format("Class '%s' is not bound", getPropertyInfo().getItemType().getClass().getName()));
75 }
76 return IDataTypeHandler.newDataTypeHandler(this, classBinding);
77 }
78
79 @Override
80 public IAssemblyClassBinding getDefinition() {
81 return definition;
82 }
83
84 @Override
85 public String getFormalName() {
86 return ModelUtil.resolveToString(getAssemblyAnnotation().formalName());
87 }
88
89 @Override
90 public MarkupLine getDescription() {
91 return ModelUtil.resolveToMarkupLine(getAssemblyAnnotation().description());
92 }
93
94 @Override
95 public MarkupMultiline getRemarks() {
96 return ModelUtil.resolveToMarkupMultiline(getAssemblyAnnotation().remarks());
97 }
98
99 @Override
100 public String getUseName() {
101 return ModelUtil.resolveToString(getAssemblyAnnotation().useName());
102 }
103
104 @Override
105 public String getXmlNamespace() {
106 return ObjectUtils
107 .notNull(ModelUtil.resolveNamespace(getAssemblyAnnotation().namespace(), getParentClassBinding()));
108 }
109
110 @Override
111 public int getMinOccurs() {
112 return getAssemblyAnnotation().minOccurs();
113 }
114
115 @Override
116 public int getMaxOccurs() {
117 return getAssemblyAnnotation().maxOccurs();
118 }
119
120 @Override
121 public Object defaultValue() {
122 return getPropertyInfo().newPropertyCollector().getValue();
123 }
124
125 @SuppressWarnings("null")
126 @Override
127 public String toCoordinates() {
128 return String.format("%s Instance(%s): %s",
129 getModelType().name().toLowerCase(Locale.ROOT),
130 getParentClassBinding().getBoundClass().getName(),
131 getName());
132 }
133
134 }