View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
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      // only concrete classes should construct
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 }