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.AbstractFieldInstance;
32  import gov.nist.secauto.metaschema.core.model.IFieldDefinition;
33  import gov.nist.secauto.metaschema.core.model.IModelContainer;
34  import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior;
35  import gov.nist.secauto.metaschema.core.model.MetaschemaModelConstants;
36  import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior;
37  import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.FieldReferenceType;
38  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
39  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
40  
41  import java.util.Collection;
42  import java.util.Collections;
43  import java.util.Map;
44  import java.util.Set;
45  
46  import javax.xml.namespace.QName;
47  
48  import edu.umd.cs.findbugs.annotations.NonNull;
49  
50  class XmlFieldInstance
51      extends AbstractFieldInstance {
52    @NonNull
53    private final FieldReferenceType xmlField;
54  
55    /**
56     * Constructs a new Metaschema field instance definition from an XML
57     * representation bound to Java objects.
58     *
59     * @param xmlField
60     *          the XML representation bound to Java objects
61     * @param container
62     *          the parent container, either a choice or assembly
63     */
64    public XmlFieldInstance(
65        @NonNull FieldReferenceType xmlField,
66        @NonNull IModelContainer container) {
67      super(container);
68      this.xmlField = xmlField;
69    }
70  
71    /**
72     * Get the underlying XML data.
73     *
74     * @return the underlying XML data
75     */
76    protected FieldReferenceType getXmlField() {
77      return xmlField;
78    }
79  
80    @Override
81    public IFieldDefinition getDefinition() {
82      // this will always be not null
83      return ObjectUtils.notNull(getContainingModule().getScopedFieldDefinitionByName(getName()));
84    }
85  
86    @Override
87    public boolean isInXmlWrapped() {
88      boolean retval;
89      if (getDefinition().getJavaTypeAdapter().isUnrappedValueAllowedInXml()) {
90        // default value
91        retval = MetaschemaModelConstants.DEFAULT_FIELD_IN_XML_WRAPPED;
92        if (getXmlField().isSetInXml()) {
93          retval = getXmlField().getInXml().booleanValue();
94        }
95      } else {
96        // All other data types get "wrapped"
97        retval = true;
98      }
99      return retval;
100   }
101 
102   @SuppressWarnings("CPD-START")
103   @Override
104   public String getFormalName() {
105     return getXmlField().isSetFormalName() ? getXmlField().getFormalName() : null;
106   }
107 
108   @SuppressWarnings("null")
109   @Override
110   public MarkupLine getDescription() {
111     return getXmlField().isSetDescription() ? MarkupStringConverter.toMarkupString(getXmlField().getDescription())
112         : null;
113   }
114 
115   @Override
116   public Map<QName, Set<String>> getProperties() {
117     return ModelFactory.toProperties(CollectionUtil.listOrEmpty(getXmlField().getPropList()));
118   }
119 
120   @SuppressWarnings("null")
121   @Override
122   public String getName() {
123     return getXmlField().getRef();
124   }
125 
126   @Override
127   public String getUseName() {
128     return getXmlField().isSetUseName() ? getXmlField().getUseName() : null;
129   }
130 
131   @Override
132   public String getGroupAsName() {
133     return getXmlField().isSetGroupAs() ? getXmlField().getGroupAs().getName() : null;
134   }
135 
136   @Override
137   public int getMinOccurs() {
138     return XmlModelParser.getMinOccurs(getXmlField().getMinOccurs());
139   }
140 
141   @Override
142   public int getMaxOccurs() {
143     return XmlModelParser.getMaxOccurs(getXmlField().getMaxOccurs());
144   }
145 
146   @Override
147   public JsonGroupAsBehavior getJsonGroupAsBehavior() {
148     return XmlModelParser.getJsonGroupAsBehavior(getXmlField().getGroupAs());
149   }
150 
151   @Override
152   public XmlGroupAsBehavior getXmlGroupAsBehavior() {
153     return XmlModelParser.getXmlGroupAsBehavior(getXmlField().getGroupAs());
154   }
155 
156   @SuppressWarnings("null")
157   @Override
158   public MarkupMultiline getRemarks() {
159     return getXmlField().isSetRemarks() ? MarkupStringConverter.toMarkupString(getXmlField().getRemarks()) : null;
160   }
161 
162   @SuppressWarnings("CPD-END")
163   @Override
164   public Object getValue(@NonNull Object parentValue) {
165     // there is no value
166     return null;
167   }
168 
169   @SuppressWarnings("null")
170   @Override
171   public Collection<?> getItemValues(Object instanceValue) {
172     // there are no item values
173     return Collections.emptyList();
174   }
175 }