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.io.xml;
28  
29  import gov.nist.secauto.metaschema.databind.io.json.DefaultJsonProblemHandler;
30  import gov.nist.secauto.metaschema.databind.model.IAssemblyClassBinding;
31  import gov.nist.secauto.metaschema.databind.model.IBoundAssemblyInstance;
32  import gov.nist.secauto.metaschema.databind.model.IBoundFieldInstance;
33  import gov.nist.secauto.metaschema.databind.model.IBoundFieldValueInstance;
34  import gov.nist.secauto.metaschema.databind.model.IBoundFlagInstance;
35  import gov.nist.secauto.metaschema.databind.model.IBoundNamedModelInstance;
36  import gov.nist.secauto.metaschema.databind.model.IClassBinding;
37  import gov.nist.secauto.metaschema.databind.model.IFieldClassBinding;
38  import gov.nist.secauto.metaschema.databind.model.info.IDataTypeHandler;
39  import gov.nist.secauto.metaschema.databind.model.info.IModelPropertyInfo;
40  
41  import org.codehaus.stax2.XMLStreamWriter2;
42  
43  import java.io.IOException;
44  
45  import javax.xml.namespace.NamespaceContext;
46  import javax.xml.namespace.QName;
47  import javax.xml.stream.XMLStreamException;
48  
49  import edu.umd.cs.findbugs.annotations.NonNull;
50  
51  public class MetaschemaXmlWriter implements IXmlWritingContext {
52    @NonNull
53    private final XMLStreamWriter2 writer;
54  
55    /**
56     * Construct a new Module-aware JSON writer.
57     *
58     * @param writer
59     *          the XML stream writer to write with
60     * @see DefaultJsonProblemHandler
61     */
62    public MetaschemaXmlWriter(
63        @NonNull XMLStreamWriter2 writer) {
64      this.writer = writer;
65    }
66  
67    @Override
68    public XMLStreamWriter2 getWriter() {
69      return writer;
70    }
71  
72    /**
73     * Writes data in a bound object to XML. This assembly must be a root assembly
74     * for which a call to {@link IAssemblyClassBinding#isRoot()} will return
75     * {@code true}.
76     *
77     * @param targetDefinition
78     *          the definition describing the root element data to write
79     * @param targetObject
80     *          the bound object
81     * @throws XMLStreamException
82     *           if an error occurred while parsing into XML
83     * @throws IOException
84     *           if an error occurred while writing the output
85     */
86    public void write(
87        @NonNull IAssemblyClassBinding targetDefinition,
88        @NonNull Object targetObject) throws XMLStreamException, IOException {
89      writer.writeStartDocument("UTF-8", "1.0");
90  
91      QName rootQName = targetDefinition.getRootXmlQName();
92  
93      NamespaceContext nsContext = writer.getNamespaceContext();
94      String prefix = nsContext.getPrefix(rootQName.getNamespaceURI());
95      if (prefix == null) {
96        prefix = "";
97      }
98  
99      writer.writeStartElement(prefix, rootQName.getLocalPart(), rootQName.getNamespaceURI());
100 
101     writeDefinitionValue(targetDefinition, targetObject, rootQName);
102 
103     writer.writeEndElement();
104   }
105 
106   @Override
107   public void writeDefinitionValue(
108       @NonNull IClassBinding targetDefinition,
109       @NonNull Object targetObject,
110       @NonNull QName parentName) throws IOException {
111     // write flags
112     for (IBoundFlagInstance flag : targetDefinition.getFlagInstances()) {
113       assert flag != null;
114       writeFlagInstance(flag, targetObject);
115     }
116 
117     if (targetDefinition instanceof IAssemblyClassBinding) {
118       for (IBoundNamedModelInstance modelProperty : ((IAssemblyClassBinding) targetDefinition).getModelInstances()) {
119         assert modelProperty != null;
120         writeModelInstanceValues(modelProperty, targetObject, parentName);
121       }
122     } else if (targetDefinition instanceof IFieldClassBinding) {
123       IBoundFieldValueInstance fieldValueInstance = ((IFieldClassBinding) targetDefinition).getFieldValueInstance();
124 
125       Object value = fieldValueInstance.getValue(targetObject);
126       if (value != null) {
127         try {
128           fieldValueInstance.getJavaTypeAdapter().writeXmlValue(value, parentName, writer);
129         } catch (XMLStreamException ex) {
130           throw new IOException(ex);
131         }
132       }
133     } else {
134       throw new UnsupportedOperationException(
135           String.format("Unsupported class binding type: %s", targetDefinition.getClass().getName()));
136     }
137   }
138 
139   /**
140    * Write the data described by the provided {@code targetInstance} as an XML
141    * attribute.
142    *
143    * @param targetInstance
144    *          the model instance that describes the syntax of the data to write
145    * @param parentObject
146    *          the Java object that data written by this method is stored in
147    * @throws IOException
148    *           if an error occurred while writing the XML
149    */
150   protected void writeFlagInstance(
151       @NonNull IBoundFlagInstance targetInstance,
152       @NonNull Object parentObject)
153       throws IOException {
154     Object objectValue = targetInstance.getValue(parentObject);
155     String value
156         = objectValue == null ? null : targetInstance.getDefinition().getJavaTypeAdapter().asString(objectValue);
157 
158     if (value != null) {
159       QName name = targetInstance.getXmlQName();
160       try {
161         if (name.getNamespaceURI().isEmpty()) {
162           writer.writeAttribute(name.getLocalPart(), value);
163         } else {
164           writer.writeAttribute(name.getNamespaceURI(), name.getLocalPart(), value);
165         }
166       } catch (XMLStreamException ex) {
167         throw new IOException(ex);
168       }
169     }
170   }
171 
172   /**
173    * Write the data described by the provided {@code targetInstance} as an XML
174    * element.
175    *
176    * @param targetInstance
177    *          the model instance that describes the syntax of the data to write
178    * @param parentObject
179    *          the Java object that data written by this method is stored in
180    * @param parentName
181    *          the qualified name of the XML data's parent element
182    * @return {@code true} id the value was written or {@code false} otherwise
183    * @throws IOException
184    *           if an error occurred while writing the XML
185    */
186   protected boolean writeModelInstanceValues(
187       @NonNull IBoundNamedModelInstance targetInstance,
188       @NonNull Object parentObject,
189       @NonNull QName parentName)
190       throws IOException {
191     Object value = targetInstance.getValue(parentObject);
192     if (value == null) {
193       return false; // NOPMD - intentional
194     }
195 
196     IModelPropertyInfo propertyInfo = targetInstance.getPropertyInfo();
197     if (targetInstance.getMinOccurs() > 0 || propertyInfo.getItemCount(value) > 0) {
198       // only write a property if the wrapper is required or if it has contents
199       QName currentStart = parentName;
200 
201       try {
202         QName groupQName = targetInstance.getXmlGroupAsQName();
203         if (groupQName != null) {
204           // write the grouping element
205           writer.writeStartElement(groupQName.getNamespaceURI(), groupQName.getLocalPart());
206           currentStart = groupQName;
207         }
208 
209         // There are one or more named values based on cardinality
210         propertyInfo.writeValues(value, currentStart, this);
211 
212         if (groupQName != null) {
213           writer.writeEndElement();
214         }
215       } catch (XMLStreamException ex) {
216         throw new IOException(ex);
217       }
218     }
219     return true;
220   }
221 
222   @Override
223   public void writeInstanceValue(
224       @NonNull IBoundNamedModelInstance targetInstance,
225       @NonNull Object targetObject,
226       @NonNull QName parentName) throws IOException {
227     // figure out how to parse the item
228     IDataTypeHandler handler = targetInstance.getDataTypeHandler();
229 
230     // figure out if we need to write the wrapper or not
231     boolean writeWrapper = targetInstance instanceof IBoundAssemblyInstance
232         || ((IBoundFieldInstance) targetInstance).isInXmlWrapped()
233         || !handler.isUnwrappedValueAllowedInXml();
234 
235     try {
236       QName currentParentName;
237       if (writeWrapper) {
238         currentParentName = targetInstance.getXmlQName();
239         writer.writeStartElement(currentParentName.getNamespaceURI(), currentParentName.getLocalPart());
240       } else {
241         currentParentName = parentName;
242       }
243 
244       // write the value
245       handler.writeItem(targetObject, currentParentName, this);
246 
247       if (writeWrapper) {
248         writer.writeEndElement();
249       }
250     } catch (XMLStreamException ex) {
251       throw new IOException(ex);
252     }
253   }
254 }