1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model;
7   
8   import gov.nist.secauto.metaschema.core.model.constraint.IFeatureValueConstrained;
9   
10  import java.util.Locale;
11  
12  import javax.xml.namespace.QName;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  import edu.umd.cs.findbugs.annotations.Nullable;
16  
17  public interface IDefinition extends INamedModelElement, IAttributable, IFeatureValueConstrained {
18  
19    @NonNull
20    ModuleScopeEnum DEFAULT_DEFINITION_MODEL_SCOPE = ModuleScopeEnum.INHERITED;
21  
22    /**
23     * Retrieve the definition's scope within the context of its defining module.
24     *
25     * @return the module scope
26     */
27    @NonNull
28    default ModuleScopeEnum getModuleScope() {
29      return ModuleScopeEnum.LOCAL;
30    }
31  
32    /**
33     * The qualified name for the definition.
34     * <p>
35     * This name is the combination of the definition's namespace, which is the
36     * module's namespace, and the definition's name.
37     *
38     * @return the definition's qualified name
39     */
40    @NonNull
41    default QName getDefinitionQName() {
42      return new QName(
43          getContainingModule().getXmlNamespace().toASCIIString(),
44          getName());
45    }
46  
47    /**
48     * Determine if the definition is defined inline, meaning the definition is
49     * declared where it is used.
50     *
51     * @return {@code true} if the definition is declared inline or {@code false} if
52     *         the definition is able to be globally referenced
53     */
54    default boolean isInline() {
55      return getInlineInstance() != null;
56    }
57  
58    /**
59     * If {@link #isInline()} is {@code true}, return the instance the definition is
60     * inlined for.
61     *
62     * @return the instance or {@code null} otherwise
63     */
64    INamedInstance getInlineInstance();
65  
66    /**
67     * Generates a coordinate string for the provided information element
68     * definition.
69     *
70     * A coordinate consists of the element's:
71     * <ul>
72     * <li>containing Metaschema's short name</li>
73     * <li>model type</li>
74     * <li>name</li>
75     * <li>hash code</li>
76     * </ul>
77     *
78     * @return the coordinate
79     */
80    @SuppressWarnings("null")
81    @Override
82    default String toCoordinates() {
83      return String.format("%s:%s-definition:%s(%d)",
84          getContainingModule().getShortName(),
85          getModelType().toString().toUpperCase(Locale.ROOT),
86          getName(),
87          hashCode());
88    }
89  
90    /**
91     * Get the resource location information for the provided item, if known.
92     *
93     * @param itemValue
94     *          the item to get the location information for
95     *
96     * @return the resource location information, or {@code null} if not known
97     */
98    @Nullable
99    default IResourceLocation getLocation(@NonNull Object itemValue) {
100     return itemValue instanceof IBoundObject ? ((IBoundObject) itemValue).getMetaschemaData() : null;
101   }
102 }