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.oscal.lib.model.metadata;
28
29 import gov.nist.secauto.metaschema.model.common.util.CollectionUtil;
30 import gov.nist.secauto.oscal.lib.model.Property;
31
32 import java.net.URI;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Objects;
36 import java.util.Optional;
37 import java.util.UUID;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40
41 import javax.xml.namespace.QName;
42
43 import edu.umd.cs.findbugs.annotations.NonNull;
44
45 public abstract class AbstractProperty implements IProperty {
46
47 @NonNull
48 public static QName qname(URI namespace, @NonNull String name) {
49 return new QName(normalizeNamespace(namespace).toString(), name);
50 }
51
52 @NonNull
53 public static QName qname(@NonNull String name) {
54 return new QName(OSCAL_NAMESPACE.toString(), name);
55 }
56
57 @NonNull
58 public static URI normalizeNamespace(URI namespace) {
59 URI propertyNamespace = namespace;
60 if (propertyNamespace == null) {
61 propertyNamespace = OSCAL_NAMESPACE;
62 }
63 return propertyNamespace;
64 }
65
66 @SuppressWarnings("null")
67 @NonNull
68 public static Optional<Property> find(List<Property> props, @NonNull QName qname) {
69 return CollectionUtil.listOrEmpty(props).stream().filter(prop -> qname.equals(prop.getQName())).findFirst();
70 }
71
72 protected AbstractProperty() {
73
74 }
75
76 public static List<Property> merge(@NonNull List<Property> original, @NonNull List<Property> additional) {
77 return Stream.concat(original.stream(), additional.stream())
78 .collect(Collectors.toCollection(LinkedList::new));
79 }
80
81 @Override
82 public boolean isNamespaceEqual(@NonNull URI namespace) {
83 return normalizeNamespace(getNs()).equals(namespace);
84 }
85
86 @NonNull
87 public QName getQName() {
88 return new QName(normalizeNamespace(getNs()).toString(), getName());
89 }
90
91 @NonNull
92 public static Builder builder(@NonNull String name) {
93 return new Builder(name);
94 }
95
96 public static class Builder {
97 @NonNull
98 private final String name;
99
100 private UUID uuid;
101 private URI namespace;
102 private String value;
103 private String clazz;
104
105 public Builder(@NonNull String name) {
106 this.name = Objects.requireNonNull(name, "name");
107 }
108
109 @NonNull
110 public Builder uuid(@NonNull UUID uuid) {
111 this.uuid = Objects.requireNonNull(uuid);
112 return this;
113 }
114
115 @NonNull
116 public Builder namespace(@NonNull URI namespace) {
117 if (IProperty.OSCAL_NAMESPACE.equals(namespace)) {
118 this.namespace = null;
119 } else {
120 this.namespace = Objects.requireNonNull(namespace);
121 }
122 return this;
123 }
124
125 @NonNull
126 public Builder value(@NonNull String value) {
127 this.value = Objects.requireNonNull(value);
128 return this;
129 }
130
131 @NonNull
132 public Builder clazz(@NonNull String clazz) {
133 this.clazz = Objects.requireNonNull(clazz);
134 return this;
135 }
136
137 @NonNull
138 public Property build() {
139 Property retval = new Property();
140 retval.setName(name);
141 retval.setValue(Objects.requireNonNull(value, "value"));
142 if (uuid != null) {
143 retval.setUuid(uuid);
144 }
145 if (namespace != null) {
146 retval.setNs(namespace);
147 }
148 if (clazz != null) {
149 retval.setClazz(clazz);
150 }
151
152 return retval;
153 }
154 }
155 }