View Javadoc
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.datatype.markup.MarkupLine;
30  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
31  import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition;
32  import gov.nist.secauto.metaschema.core.model.IAssemblyInstance;
33  import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
34  import gov.nist.secauto.metaschema.core.model.IDefinition;
35  import gov.nist.secauto.metaschema.core.model.IFeatureFlagContainer;
36  import gov.nist.secauto.metaschema.core.model.IFeatureModelContainer;
37  import gov.nist.secauto.metaschema.core.model.IFieldInstance;
38  import gov.nist.secauto.metaschema.core.model.IFlagInstance;
39  import gov.nist.secauto.metaschema.core.model.IModelInstance;
40  import gov.nist.secauto.metaschema.core.model.INamedModelInstance;
41  import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum;
42  import gov.nist.secauto.metaschema.core.model.constraint.IConstraint.ExternalModelSource;
43  import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained;
44  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GlobalAssemblyDefinitionType;
45  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
46  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
47  
48  import java.util.Map;
49  import java.util.Set;
50  
51  import javax.xml.namespace.QName;
52  
53  import edu.umd.cs.findbugs.annotations.NonNull;
54  import nl.talsmasoftware.lazy4j.Lazy;
55  
56  @SuppressWarnings("PMD.CouplingBetweenObjects")
57  class XmlGlobalAssemblyDefinition
58      implements IAssemblyDefinition,
59      IFeatureModelContainer<IModelInstance, INamedModelInstance, IFieldInstance, IAssemblyInstance, IChoiceInstance>,
60      IFeatureFlagContainer<IFlagInstance> {
61  
62    @NonNull
63    private final GlobalAssemblyDefinitionType xmlAssembly;
64    @NonNull
65    private final XmlModule metaschema;
66    @NonNull
67    private final Lazy<XmlFlagContainerSupport> flagContainer;
68    @NonNull
69    private final Lazy<XmlModelContainerSupport> modelContainer;
70    @NonNull
71    private final Lazy<AssemblyConstraintSupport> constraints;
72  
73    /**
74     * Constructs a new Metaschema Assembly definition from an XML representation
75     * bound to Java objects.
76     *
77     * @param xmlAssembly
78     *          the XML representation bound to Java objects
79     * @param metaschema
80     *          the containing Metaschema
81     */
82    public XmlGlobalAssemblyDefinition(
83        @NonNull GlobalAssemblyDefinitionType xmlAssembly,
84        @NonNull XmlModule metaschema) {
85      this.xmlAssembly = xmlAssembly;
86      this.metaschema = metaschema;
87      this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new XmlFlagContainerSupport(xmlAssembly, this)));
88      this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> new XmlModelContainerSupport(xmlAssembly, this)));
89      this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyConstraintSupport.newInstance(
90          xmlAssembly,
91          ExternalModelSource.instance(metaschema.getLocation()))));
92    }
93  
94    @Override
95    public XmlFlagContainerSupport getFlagContainer() {
96      return ObjectUtils.notNull(flagContainer.get());
97    }
98  
99    @Override
100   public XmlModelContainerSupport getModelContainer() {
101     return ObjectUtils.notNull(modelContainer.get());
102   }
103 
104   @Override
105   public IModelConstrained getConstraintSupport() {
106     return ObjectUtils.notNull(constraints.get());
107   }
108 
109   @Override
110   public XmlModule getContainingModule() {
111     return metaschema;
112   }
113 
114   // ----------------------------------------
115   // - Start Annotation driven code - CPD-OFF
116   // ----------------------------------------
117 
118   /**
119    * Get the underlying XML data.
120    *
121    * @return the underlying XML data
122    */
123   @NonNull
124   protected GlobalAssemblyDefinitionType getXmlAssembly() {
125     return xmlAssembly;
126   }
127 
128   @Override
129   public String getName() {
130     return ObjectUtils.requireNonNull(getXmlAssembly().getName());
131   }
132 
133   @Override
134   public String getUseName() {
135     return getXmlAssembly().isSetUseName() ? getXmlAssembly().getUseName() : null;
136   }
137 
138   @Override
139   public String getFormalName() {
140     return getXmlAssembly().isSetFormalName() ? getXmlAssembly().getFormalName() : null;
141   }
142 
143   @SuppressWarnings("null")
144   @Override
145   public MarkupLine getDescription() {
146     return getXmlAssembly().isSetDescription() ? MarkupStringConverter.toMarkupString(getXmlAssembly().getDescription())
147         : null;
148   }
149 
150   @Override
151   public Map<QName, Set<String>> getProperties() {
152     return ModelFactory.toProperties(CollectionUtil.listOrEmpty(getXmlAssembly().getPropList()));
153   }
154 
155   @Override
156   public boolean isRoot() {
157     return getXmlAssembly().isSetRootName();
158   }
159 
160   @Override
161   public String getRootName() {
162     return getXmlAssembly().isSetRootName() ? getXmlAssembly().getRootName() : null;
163   }
164 
165   @SuppressWarnings("null")
166   @Override
167   public ModuleScopeEnum getModuleScope() {
168     return getXmlAssembly().isSetScope() ? getXmlAssembly().getScope() : IDefinition.DEFAULT_DEFINITION_MODEL_SCOPE;
169   }
170 
171   @SuppressWarnings("null")
172   @Override
173   public MarkupMultiline getRemarks() {
174     return getXmlAssembly().isSetRemarks() ? MarkupStringConverter.toMarkupString(getXmlAssembly().getRemarks()) : null;
175   }
176 
177   // --------------------------------------
178   // - End Annotation driven code - CPD-ON
179   // --------------------------------------
180 
181   @Override
182   public boolean isInline() {
183     // global
184     return false;
185   }
186 
187   @Override
188   public IAssemblyInstance getInlineInstance() {
189     // global
190     return null;
191   }
192 }