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.databind.model.info;
28  
29  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
30  import gov.nist.secauto.metaschema.databind.io.BindingException;
31  import gov.nist.secauto.metaschema.databind.io.json.IJsonParsingContext;
32  import gov.nist.secauto.metaschema.databind.io.json.IJsonWritingContext;
33  import gov.nist.secauto.metaschema.databind.io.xml.IXmlParsingContext;
34  import gov.nist.secauto.metaschema.databind.io.xml.IXmlWritingContext;
35  import gov.nist.secauto.metaschema.databind.model.IBoundNamedModelInstance;
36  
37  import java.io.IOException;
38  import java.util.List;
39  
40  import javax.xml.namespace.QName;
41  import javax.xml.stream.XMLStreamException;
42  import javax.xml.stream.events.StartElement;
43  
44  import edu.umd.cs.findbugs.annotations.NonNull;
45  
46  class SingletonPropertyInfo
47      extends AbstractModelPropertyInfo {
48  
49    public SingletonPropertyInfo(
50        @NonNull IBoundNamedModelInstance property) {
51      super(property);
52    }
53  
54    @SuppressWarnings("null")
55    @Override
56    public List<?> getItemsFromValue(Object value) {
57      return value == null ? List.of() : List.of(value);
58    }
59  
60    @Override
61    public int getItemCount(Object value) {
62      return value == null ? 0 : 1;
63    }
64  
65    @Override
66    public void readValues(IPropertyCollector collector, Object parentInstance, IJsonParsingContext context)
67        throws IOException {
68  
69      // JsonParser parser = context.getReader();
70      //
71      // boolean isObject = JsonToken.START_OBJECT.equals(parser.currentToken()); //
72      // TODO: is this object
73      // check needed?
74      // if (isObject) {
75      // // read the object's START_OBJECT
76      // JsonUtil.assertAndAdvance(parser, JsonToken.START_OBJECT);
77      // }
78  
79      Object value = getProperty().getDataTypeHandler().readItem(parentInstance, context);
80      collector.add(value);
81  
82      // if (isObject) {
83      // // read the object's END_OBJECT
84      // JsonUtil.assertAndAdvance(context.getReader(), JsonToken.END_OBJECT);
85      // }
86    }
87  
88    @Override
89    public boolean readValues(IPropertyCollector collector, Object parentInstance, StartElement start,
90        IXmlParsingContext context) throws IOException, XMLStreamException {
91      boolean handled = true;
92      Object value = context.readModelInstanceValue(getProperty(), parentInstance, start);
93      if (value != null) {
94        collector.add(value);
95        handled = true;
96      }
97      return handled;
98    }
99  
100   @Override
101   public Class<?> getItemType() {
102     return (Class<?>) getProperty().getType();
103   }
104 
105   @Override
106   public IPropertyCollector newPropertyCollector() {
107     return new SingletonPropertyCollector();
108   }
109 
110   @Override
111   public void writeValues(@NonNull Object value, QName parentName, IXmlWritingContext context)
112       throws XMLStreamException, IOException {
113     context.writeInstanceValue(getProperty(), value, parentName);
114   }
115 
116   @Override
117   public void writeValues(Object parentObject, IJsonWritingContext context) throws IOException {
118     IBoundNamedModelInstance property = getProperty();
119     getProperty().getDataTypeHandler().writeItem(
120         ObjectUtils.notNull(property.getValue(parentObject)),
121         context);
122   }
123 
124   @Override
125   public boolean isValueSet(Object parentInstance) throws IOException {
126     return getProperty().getValue(parentInstance) != null;
127   }
128 
129   @Override
130   public void copy(@NonNull Object fromInstance, @NonNull Object toInstance, @NonNull IPropertyCollector collector)
131       throws BindingException {
132     IBoundNamedModelInstance property = getProperty();
133 
134     Object value = property.getValue(fromInstance);
135 
136     Object copiedValue = property.copyItem(ObjectUtils.requireNonNull(value), toInstance);
137 
138     collector.add(copiedValue);
139   }
140 
141 }