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.metaschema.core.model;
28
29 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
30 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
31
32 import java.util.Map;
33 import java.util.Set;
34
35 import javax.xml.namespace.QName;
36
37 import edu.umd.cs.findbugs.annotations.NonNull;
38 import edu.umd.cs.findbugs.annotations.Nullable;
39
40 /**
41 * A marker interface for Metaschema constructs that can be members of a
42 * Metaschema definition's model that are named.
43 */
44 public interface INamedModelElement extends IModelElement {
45 /**
46 * The formal display name.
47 *
48 * @return the formal name or {@code null} if not defined
49 */
50 @Nullable
51 String getFormalName();
52
53 /**
54 * The resolved formal display name, which allows an instance to override a
55 * definition's name.
56 *
57 * @return the formal name or {@code null} if not defined
58 */
59 @Nullable
60 default String getEffectiveFormalName() {
61 return getFormalName();
62 }
63
64 /**
65 * Get the text that describes the basic use of the element.
66 *
67 * @return a line of markup text or {@code null} if not defined
68 */
69 @Nullable
70 MarkupLine getDescription();
71
72 /**
73 * Get the text that describes the basic use of the element, which allows an
74 * instance to override a definition's description.
75 *
76 * @return a line of markup text or {@code null} if not defined
77 */
78 @Nullable
79 default MarkupLine getEffectiveDescription() {
80 return getDescription();
81 }
82
83 /**
84 * Get the mapping of property name to values for the model element.
85 *
86 * @return the mapping
87 */
88 @NonNull
89 Map<QName, Set<String>> getProperties();
90
91 /**
92 * Determine if a property is defined.
93 *
94 * @param qname
95 * the qualified name of the property
96 * @return {@code true} if the property is defined or {@code false} otherwise
97 */
98 default boolean hasProperty(@NonNull QName qname) {
99 return getProperties().containsKey(qname);
100 }
101
102 /**
103 * Get the values associated with a given property.
104 *
105 * @param qname
106 * the qualified name of the property
107 * @return the values or an empty set
108 */
109 @NonNull
110 default Set<String> getPropertyValues(@NonNull QName qname) {
111 Set<String> retval = getProperties().get(qname);
112 if (retval == null) {
113 retval = CollectionUtil.emptySet();
114 }
115 return retval;
116 }
117
118 /**
119 * Determine if a given property, with a given {@code qname}, has the identified
120 * {@code value}.
121 *
122 * @param qname
123 * the qualified name of the property
124 * @param value
125 * the expected property value
126 * @return {@code true} if the property value is defined or {@code false}
127 * otherwise
128 */
129 default boolean hasPropertyValue(@NonNull QName qname, @NonNull String value) {
130 Set<String> values = getProperties().get(qname);
131 return values != null && values.contains(value);
132 }
133
134 // @NonNull
135 // default QName getXmlQName() {
136 // String namespace = getXmlNamespace();
137 //
138 // @NonNull
139 // QName retval;
140 // if (namespace != null) {
141 // retval = new QName(namespace, getEffectiveName());
142 // } else {
143 // retval = new QName(getEffectiveName());
144 // }
145 // return retval;
146 // }
147
148 /**
149 * Get the name used for the associated property in JSON/YAML.
150 *
151 * @return the JSON property name
152 */
153 @NonNull
154 default String getJsonName() {
155 return getEffectiveName();
156 }
157
158 /**
159 * Get the name to use based on the provided names. This method will return the
160 * use name provided by {@link #getUseName()} if the call is not {@code null},
161 * and fall back to the name provided by {@link #getName()} otherwise. This is
162 * the model name to use for the for an instance where the instance is
163 * referenced.
164 *
165 * @return the use name if available, or the name if not
166 *
167 * @see #getUseName()
168 * @see #getName()
169 */
170 @NonNull
171 default String getEffectiveName() {
172 @Nullable String useName = getUseName();
173 return useName == null ? getName() : useName;
174 }
175
176 /**
177 * Retrieve the name of the model element.
178 *
179 * @return the name
180 */
181 @NonNull
182 String getName();
183
184 /**
185 * Retrieve the name to use for the model element, instead of the name.
186 *
187 * @return the use name or {@code null} if no use name is defined
188 */
189 @Nullable
190 String getUseName();
191 }