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.MetaschemaConstants;
9   import gov.nist.secauto.metaschema.core.model.constraint.IFeatureModelConstrained;
10  import gov.nist.secauto.metaschema.core.model.util.ModuleUtils;
11  import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  import edu.umd.cs.findbugs.annotations.Nullable;
15  
16  public interface IAssemblyDefinition
17      extends IModelDefinition, IContainerModelAssembly, IAssembly, IFeatureModelConstrained {
18    IEnhancedQName MODEL_QNAME = IEnhancedQName.of(MetaschemaConstants.METASCHEMA_NAMESPACE, "model");
19  
20    /**
21     * Check if the assembly is a top-level root assembly.
22     *
23     * @return {@code true} if the assembly is a top-level root, or {@code false}
24     *         otherwise
25     */
26    default boolean isRoot() {
27      // not a root by default
28      return false;
29    }
30  
31    /**
32     * Get the root name if this assembly is a top-level root.
33     *
34     * @return the root name if this assembly is a top-level root, or {@code null}
35     *         otherwise
36     */
37    @Nullable
38    default String getRootName() {
39      // not a root by default
40      return null;
41    }
42  
43    /**
44     * Get the root index to use for binary data, if this assembly is a top-level
45     * root.
46     *
47     * @return the root index if provided and this assembly is a top-level root, or
48     *         {@code null} otherwise
49     */
50    @Nullable
51    default Integer getRootIndex() {
52      // not a root by default
53      return null;
54    }
55  
56    /**
57     * Get the XML qualified name to use in XML as the root element.
58     *
59     * @return the root XML qualified name if this assembly is a top-level root, or
60     *         {@code null} otherwise
61     */
62    default IEnhancedQName getRootQName() {
63      IEnhancedQName retval = null;
64      String rootName = getRootName();
65      if (rootName != null) {
66        retval = ModuleUtils.parseModelName(getContainingModule(), rootName);
67      }
68      return retval;
69    }
70  
71    /**
72     * Get the name used for the associated property in JSON/YAML.
73     *
74     * @return the root JSON property name if this assembly is a top-level root, or
75     *         {@code null} otherwise
76     */
77    default String getRootJsonName() {
78      return getRootName();
79    }
80  
81    @Override
82    default boolean isInline() {
83      // not inline by default
84      return false;
85    }
86  
87    @Override
88    default IAssemblyInstance getInlineInstance() {
89      // not inline by default
90      return null;
91    }
92  
93    @Override
94    default IAssemblyDefinition getOwningDefinition() {
95      return this;
96    }
97    //
98    // @Override
99    // default IAssemblyNodeItem getNodeItem() {
100   // return null;
101   // }
102 
103   @Override
104   default boolean hasChildren() {
105     return IModelDefinition.super.hasChildren() || IContainerModelAssembly.super.hasChildren();
106   }
107 
108   /**
109    * A visitor callback.
110    *
111    * @param <CONTEXT>
112    *          the type of the context parameter
113    * @param <RESULT>
114    *          the type of the visitor result
115    * @param visitor
116    *          the calling visitor
117    * @param context
118    *          a parameter used to pass contextual information between visitors
119    * @return the visitor result
120    */
121   @Override
122   default <CONTEXT, RESULT> RESULT accept(@NonNull IModelElementVisitor<CONTEXT, RESULT> visitor, CONTEXT context) {
123     return visitor.visitAssemblyDefinition(this, context);
124   }
125 }