1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model.metaschema.impl;
7   
8   import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
9   import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute;
10  import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance;
11  import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
12  import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute;
13  import gov.nist.secauto.metaschema.core.model.IContainerModelSupport;
14  import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
15  import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute;
16  import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute;
17  import gov.nist.secauto.metaschema.core.model.IModule;
18  import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute;
19  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
20  import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly;
21  import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference;
22  import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference;
23  
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.xml.namespace.QName;
28  
29  import edu.umd.cs.findbugs.annotations.NonNull;
30  
31  public abstract class AbstractBindingModelContainerSupport
32      implements IContainerModelSupport<
33          IModelInstanceAbsolute,
34          INamedModelInstanceAbsolute,
35          IFieldInstanceAbsolute,
36          IAssemblyInstanceAbsolute> {
37  
38    protected static void addInstance(
39        @NonNull IAssemblyInstanceAbsolute assembly,
40        @NonNull List<IModelInstanceAbsolute> modelInstances,
41        @NonNull Map<QName, INamedModelInstanceAbsolute> namedModelInstances,
42        @NonNull Map<QName, IAssemblyInstanceAbsolute> assemblyInstances) {
43      QName effectiveName = assembly.getXmlQName();
44      modelInstances.add(assembly);
45      namedModelInstances.put(effectiveName, assembly);
46      assemblyInstances.put(effectiveName, assembly);
47    }
48  
49    protected static void addInstance(
50        @NonNull IFieldInstanceAbsolute field,
51        @NonNull List<IModelInstanceAbsolute> modelInstances,
52        @NonNull Map<QName, INamedModelInstanceAbsolute> namedModelInstances,
53        @NonNull Map<QName, IFieldInstanceAbsolute> fieldInstances) {
54      QName effectiveName = field.getXmlQName();
55      modelInstances.add(field);
56      namedModelInstances.put(effectiveName, field);
57      fieldInstances.put(effectiveName, field);
58    }
59  
60    protected static void addInstance(
61        @NonNull IChoiceInstance choice,
62        @NonNull List<IModelInstanceAbsolute> modelInstances,
63        @NonNull List<IChoiceInstance> choiceInstances) {
64      modelInstances.add(choice);
65      choiceInstances.add(choice);
66    }
67  
68    protected static void addInstance(
69        @NonNull IChoiceGroupInstance choiceGroup,
70        @NonNull List<IModelInstanceAbsolute> modelInstances,
71        @NonNull Map<String, IChoiceGroupInstance> choiceGroupInstances) {
72      modelInstances.add(choiceGroup);
73      choiceGroupInstances.put(choiceGroup.getGroupAsName(), choiceGroup);
74    }
75  
76    @NonNull
77    protected static IAssemblyInstanceAbsolute newInstance(
78        @NonNull AssemblyReference obj,
79        @NonNull IBoundInstanceModelGroupedAssembly objInstance,
80        int position,
81        @NonNull IContainerModelAbsolute parent) {
82      IAssemblyDefinition owningDefinition = parent.getOwningDefinition();
83      IModule module = owningDefinition.getContainingModule();
84  
85      String name = ObjectUtils.requireNonNull(obj.getRef());
86      IAssemblyDefinition definition = module.getScopedAssemblyDefinitionByName(
87          module.toModelQName(name));
88  
89      if (definition == null) {
90        throw new IllegalStateException(
91            String.format("Unable to resolve assembly reference '%s' in definition '%s' in module '%s'",
92                name,
93                owningDefinition.getName(),
94                module.getShortName()));
95      }
96      return new InstanceModelAssemblyReference(obj, objInstance, position, definition, parent);
97    }
98  
99    @NonNull
100   protected static IFieldInstanceAbsolute newInstance(
101       @NonNull FieldReference obj,
102       @NonNull IBoundInstanceModelGroupedAssembly objInstance,
103       int position,
104       @NonNull IContainerModelAbsolute parent) {
105     IAssemblyDefinition owningDefinition = parent.getOwningDefinition();
106     IModule module = owningDefinition.getContainingModule();
107 
108     String name = ObjectUtils.requireNonNull(obj.getRef());
109     IFieldDefinition definition = module.getScopedFieldDefinitionByName(
110         module.toModelQName(name));
111     if (definition == null) {
112       throw new IllegalStateException(
113           String.format("Unable to resolve field reference '%s' in definition '%s' in module '%s'",
114               name,
115               owningDefinition.getName(),
116               module.getShortName()));
117     }
118     return new InstanceModelFieldReference(obj, objInstance, position, definition, parent);
119   }
120 }