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.IDataTypeAdapter;
30  import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider;
31  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
32  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
33  import gov.nist.secauto.metaschema.core.model.IDefinition;
34  import gov.nist.secauto.metaschema.core.model.IFlagDefinition;
35  import gov.nist.secauto.metaschema.core.model.IFlagInstance;
36  import gov.nist.secauto.metaschema.core.model.IModule;
37  import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum;
38  import gov.nist.secauto.metaschema.core.model.constraint.IConstraint.ExternalModelSource;
39  import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
40  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GlobalFlagDefinitionType;
41  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
42  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
43  
44  import java.util.Map;
45  import java.util.Set;
46  
47  import javax.xml.namespace.QName;
48  
49  import edu.umd.cs.findbugs.annotations.NonNull;
50  import edu.umd.cs.findbugs.annotations.Nullable;
51  import nl.talsmasoftware.lazy4j.Lazy;
52  
53  class XmlGlobalFlagDefinition implements IFlagDefinition {
54    @NonNull
55    private final GlobalFlagDefinitionType xmlFlag;
56    @NonNull
57    private final IModule module;
58    @Nullable
59    private final Object defaultValue;
60    private final Lazy<IValueConstrained> constraints;
61  
62    /**
63     * Constructs a new Metaschema flag definition from an XML representation bound
64     * to Java objects.
65     *
66     * @param xmlFlag
67     *          the XML representation bound to Java objects
68     * @param module
69     *          the containing Metaschema module
70     */
71    public XmlGlobalFlagDefinition(
72        @NonNull GlobalFlagDefinitionType xmlFlag,
73        @NonNull IModule module) {
74      this.xmlFlag = xmlFlag;
75      this.module = module;
76  
77      Object defaultValue = null;
78      if (xmlFlag.isSetDefault()) {
79        defaultValue = getJavaTypeAdapter().parse(ObjectUtils.requireNonNull(xmlFlag.getDefault()));
80      }
81      this.defaultValue = defaultValue;
82      this.constraints = Lazy.lazy(() -> {
83        IValueConstrained retval;
84        if (getXmlFlag().isSetConstraint()) {
85          retval = new ValueConstraintSupport(
86              ObjectUtils.notNull(getXmlFlag().getConstraint()),
87              ExternalModelSource.instance(
88                  ObjectUtils.requireNonNull(getContainingModule().getLocation())));
89        } else {
90          retval = new ValueConstraintSupport();
91        }
92        return retval;
93      });
94    }
95  
96    @Override
97    public IModule getContainingModule() {
98      return module;
99    }
100 
101   @Override
102   public Object getDefaultValue() {
103     return defaultValue;
104   }
105 
106   /**
107    * Used to generate the instances for the constraints in a lazy fashion when the
108    * constraints are first accessed.
109    *
110    * @return the constraints instance
111    */
112   @SuppressWarnings("null")
113   @Override
114   public IValueConstrained getConstraintSupport() {
115     return constraints.get();
116   }
117 
118   // ----------------------------------------
119   // - Start annotation driven code - CPD-OFF
120   // ----------------------------------------
121 
122   /**
123    * Get the underlying XML representation.
124    *
125    * @return the underlying XML data
126    */
127   protected final GlobalFlagDefinitionType getXmlFlag() {
128     return xmlFlag;
129   }
130 
131   @SuppressWarnings("null")
132   @Override
133   public ModuleScopeEnum getModuleScope() {
134     return getXmlFlag().isSetScope() ? getXmlFlag().getScope() : IDefinition.DEFAULT_DEFINITION_MODEL_SCOPE;
135   }
136 
137   @SuppressWarnings("null")
138   @Override
139   public String getName() {
140     return getXmlFlag().getName();
141   }
142 
143   @Override
144   public String getUseName() {
145     String retval = getXmlFlag().getUseName();
146     if (retval == null) {
147       retval = getName();
148     }
149     return retval;
150   }
151 
152   @Override
153   public String getFormalName() {
154     return getXmlFlag().isSetFormalName() ? getXmlFlag().getFormalName() : null;
155   }
156 
157   @SuppressWarnings("null")
158   @Override
159   public MarkupLine getDescription() {
160     return getXmlFlag().isSetDescription() ? MarkupStringConverter.toMarkupString(getXmlFlag().getDescription()) : null;
161   }
162 
163   @Override
164   public Map<QName, Set<String>> getProperties() {
165     return ModelFactory.toProperties(CollectionUtil.listOrEmpty(getXmlFlag().getPropList()));
166   }
167 
168   @SuppressWarnings("null")
169   @Override
170   public final IDataTypeAdapter<?> getJavaTypeAdapter() {
171     return getXmlFlag().isSetAsType() ? getXmlFlag().getAsType() : MetaschemaDataTypeProvider.DEFAULT_DATA_TYPE;
172   }
173 
174   @SuppressWarnings("null")
175   @Override
176   public MarkupMultiline getRemarks() {
177     return getXmlFlag().isSetRemarks() ? MarkupStringConverter.toMarkupString(getXmlFlag().getRemarks()) : null;
178   }
179 
180   // --------------------------------------
181   // - End annotation driven code - CPD-ON
182   // --------------------------------------
183 
184   @Override
185   public boolean isInline() {
186     // global
187     return false;
188   }
189 
190   @Override
191   public IFlagInstance getInlineInstance() {
192     // global
193     return null;
194   }
195 
196 }