001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.model;
028
029import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
030import gov.nist.secauto.metaschema.core.util.CollectionUtil;
031
032import java.util.Map;
033import java.util.Set;
034
035import javax.xml.namespace.QName;
036
037import edu.umd.cs.findbugs.annotations.NonNull;
038import edu.umd.cs.findbugs.annotations.Nullable;
039
040/**
041 * A marker interface for Metaschema constructs that can be members of a
042 * Metaschema definition's model that are named.
043 */
044public interface INamedModelElement extends IModelElement {
045  /**
046   * The formal display name.
047   *
048   * @return the formal name or {@code null} if not defined
049   */
050  @Nullable
051  String getFormalName();
052
053  /**
054   * The resolved formal display name, which allows an instance to override a
055   * definition's name.
056   *
057   * @return the formal name or {@code null} if not defined
058   */
059  @Nullable
060  default String getEffectiveFormalName() {
061    return getFormalName();
062  }
063
064  /**
065   * Get the text that describes the basic use of the element.
066   *
067   * @return a line of markup text or {@code null} if not defined
068   */
069  @Nullable
070  MarkupLine getDescription();
071
072  /**
073   * Get the text that describes the basic use of the element, which allows an
074   * instance to override a definition's description.
075   *
076   * @return a line of markup text or {@code null} if not defined
077   */
078  @Nullable
079  default MarkupLine getEffectiveDescription() {
080    return getDescription();
081  }
082
083  /**
084   * Get the mapping of property name to values for the model element.
085   *
086   * @return the mapping
087   */
088  @NonNull
089  Map<QName, Set<String>> getProperties();
090
091  /**
092   * Determine if a property is defined.
093   *
094   * @param qname
095   *          the qualified name of the property
096   * @return {@code true} if the property is defined or {@code false} otherwise
097   */
098  default boolean hasProperty(@NonNull QName qname) {
099    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}