1
2
3
4
5
6 package gov.nist.secauto.metaschema.databind.model.annotations;
7
8 import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter;
9 import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider;
10 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
11 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
12 import gov.nist.secauto.metaschema.core.model.IBoundObject;
13 import gov.nist.secauto.metaschema.core.model.IMetaschemaData;
14 import gov.nist.secauto.metaschema.core.model.IModule;
15 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
16 import gov.nist.secauto.metaschema.databind.IBindingContext;
17 import gov.nist.secauto.metaschema.databind.model.IGroupAs;
18 import gov.nist.secauto.metaschema.databind.model.impl.DefaultGroupAs;
19
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Field;
22 import java.net.URI;
23
24 import edu.umd.cs.findbugs.annotations.NonNull;
25 import edu.umd.cs.findbugs.annotations.Nullable;
26
27 public final class ModelUtil {
28
29
30 public static final String NO_STRING_VALUE = "##none";
31 public static final String DEFAULT_STRING_VALUE = "##default";
32
33
34
35
36
37
38
39 public static final String NULL_VALUE = "\u0000";
40
41 private ModelUtil() {
42
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 @NonNull
59 public static <A extends Annotation> A getAnnotation(
60 @NonNull Class<?> clazz,
61 Class<A> annotationClass) {
62 A annotation = clazz.getAnnotation(annotationClass);
63 if (annotation == null) {
64 throw new IllegalArgumentException(
65 String.format("Class '%s' is missing the '%s' annotation.",
66 clazz.getName(),
67 annotationClass.getName()));
68 }
69 return annotation;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 @NonNull
86 public static <A extends Annotation> A getAnnotation(
87 @NonNull Field javaField,
88 Class<A> annotationClass) {
89 A annotation = javaField.getAnnotation(annotationClass);
90 if (annotation == null) {
91 throw new IllegalArgumentException(
92 String.format("Field '%s' is missing the '%s' annotation.",
93 javaField.toGenericString(),
94 annotationClass.getName()));
95 }
96 return annotation;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 @Nullable
111 public static String resolveNoneOrDefault(@Nullable String value, @Nullable String defaultValue) {
112 String retval;
113 if (value == null || DEFAULT_STRING_VALUE.equals(value)) {
114 retval = defaultValue;
115 } else if (NO_STRING_VALUE.equals(value)) {
116 retval = null;
117 } else {
118 retval = value;
119 }
120 return retval;
121 }
122
123
124
125
126
127
128
129
130
131 @Nullable
132 public static String resolveNoneOrValue(@NonNull String value) {
133 return NO_STRING_VALUE.equals(value) ? null : value;
134 }
135
136
137
138
139
140
141
142
143
144 @Nullable
145 public static MarkupLine resolveToMarkupLine(@NonNull String value) {
146 return resolveNoneOrValue(value) == null ? null : MarkupLine.fromMarkdown(value);
147 }
148
149
150
151
152
153
154
155
156
157 @Nullable
158 public static MarkupMultiline resolveToMarkupMultiline(@NonNull String value) {
159 return resolveNoneOrValue(value) == null ? null : MarkupMultiline.fromMarkdown(value);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 @NonNull
176 public static IDataTypeAdapter<?> getDataTypeAdapter(
177 @NonNull Class<? extends IDataTypeAdapter<?>> adapterClass,
178 @NonNull IBindingContext bindingContext) {
179 IDataTypeAdapter<?> retval;
180 if (NullJavaTypeAdapter.class.equals(adapterClass)) {
181 retval = MetaschemaDataTypeProvider.DEFAULT_DATA_TYPE;
182 } else {
183 retval = ObjectUtils.requireNonNull(bindingContext.getJavaTypeAdapterInstance(adapterClass));
184 }
185 return retval;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 @Nullable
204 public static Object resolveDefaultValue(@NonNull String defaultValue, IDataTypeAdapter<?> adapter) {
205 Object retval = null;
206 if (!NULL_VALUE.equals(defaultValue)) {
207 retval = adapter.parse(defaultValue);
208 }
209 return retval;
210 }
211
212
213
214
215
216
217
218
219
220
221
222 public static Integer resolveDefaultInteger(int value) {
223 return value == Integer.MIN_VALUE ? null : value;
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238 @NonNull
239 public static IGroupAs resolveDefaultGroupAs(
240 @NonNull GroupAs groupAs,
241 @NonNull IModule module) {
242 return NULL_VALUE.equals(groupAs.name())
243 ? IGroupAs.SINGLETON_GROUP_AS
244 : new DefaultGroupAs(groupAs, module);
245 }
246
247 public static String toLocation(@NonNull IBoundObject obj) {
248 IMetaschemaData data = obj.getMetaschemaData();
249
250 String retval = "";
251 if (data != null) {
252 int line = data.getLine();
253 if (line > -1) {
254 retval = line + ":" + data.getColumn();
255 }
256 }
257 return retval;
258 }
259
260 public static String toLocation(@NonNull IBoundObject obj, @Nullable URI uri) {
261 String retval = uri == null ? "" : uri.toASCIIString();
262
263 String location = toLocation(obj);
264 if (!location.isEmpty()) {
265 retval = retval.isEmpty() ? location : retval + "@" + location;
266 }
267 return retval;
268 }
269 }