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.xml;
28
29 import gov.nist.secauto.metaschema.core.model.IAssemblyInstance;
30 import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
31 import gov.nist.secauto.metaschema.core.model.IFieldInstance;
32 import gov.nist.secauto.metaschema.core.model.IModelContainer;
33 import gov.nist.secauto.metaschema.core.model.IModelContainerSupport;
34 import gov.nist.secauto.metaschema.core.model.IModelInstance;
35 import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
36 import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.ChoiceType;
37
38 import org.apache.xmlbeans.XmlObject;
39
40 import java.util.Collection;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.stream.Collectors;
44
45 import edu.umd.cs.findbugs.annotations.NonNull;
46
47 class XmlModelContainerSupport
48 implements
49 IModelContainerSupport<IModelInstance, INamedModelInstance, IFieldInstance, IAssemblyInstance, IChoiceInstance> {
50
51 @NonNull
52 private final List<IModelInstance> modelInstances;
53 @NonNull
54 private final Map<String, INamedModelInstance> namedModelInstances;
55 @NonNull
56 private final Map<String, IFieldInstance> fieldInstances;
57 @NonNull
58 private final Map<String, IAssemblyInstance> assemblyInstances;
59
60 /**
61 * Construct a new model container.
62 *
63 * @param xmlContent
64 * the XMLBeans content to parse
65 * @param container
66 * the assembly containing this model
67 */
68 public XmlModelContainerSupport(@NonNull XmlObject xmlContent, @NonNull IModelContainer container) {
69 XmlModelParser parser = new XmlModelParser();
70 if (xmlContent instanceof ChoiceType) {
71 parser.parseChoice(xmlContent, container);
72 } else {
73 parser.parseModel(xmlContent, container);
74 }
75 this.modelInstances = parser.getModelInstances();
76 this.namedModelInstances = parser.getNamedModelInstances();
77 this.fieldInstances = parser.getFieldInstances();
78 this.assemblyInstances = parser.getAssemblyInstances();
79 }
80
81 /**
82 * Get a listing of all model instances.
83 *
84 * @return the listing
85 */
86 @Override
87 @NonNull
88 public Collection<IModelInstance> getModelInstances() {
89 return modelInstances;
90 }
91
92 /**
93 * Get a mapping of all named model instances, mapped from their effective name
94 * to the instance.
95 *
96 * @return the mapping
97 */
98 @Override
99 @NonNull
100 public Map<String, INamedModelInstance> getNamedModelInstanceMap() {
101 return namedModelInstances;
102 }
103
104 /**
105 * Get a mapping of all field instances, mapped from their effective name to the
106 * instance.
107 *
108 * @return the mapping
109 */
110 @Override
111 @NonNull
112 public Map<String, IFieldInstance> getFieldInstanceMap() {
113 return fieldInstances;
114 }
115
116 /**
117 * Get a mapping of all assembly instances, mapped from their effective name to
118 * the instance.
119 *
120 * @return the mapping
121 */
122 @Override
123 @NonNull
124 public Map<String, IAssemblyInstance> getAssemblyInstanceMap() {
125 return assemblyInstances;
126 }
127
128 /**
129 * Get a listing of all choice instances.
130 *
131 * @return the listing
132 */
133 @Override
134 @SuppressWarnings("null")
135 @NonNull
136 public List<IChoiceInstance> getChoiceInstances() {
137 // this shouldn't get called all that often, so this is better than allocating
138 // memory
139 return getModelInstances().stream()
140 .filter(obj -> obj instanceof IChoiceInstance)
141 .map(obj -> (IChoiceInstance) obj)
142 .collect(Collectors.toList());
143 }
144 }