001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.databind.io.xml;
028
029import com.ctc.wstx.api.WstxOutputProperties;
030import com.ctc.wstx.stax.WstxOutputFactory;
031
032import gov.nist.secauto.metaschema.core.util.ObjectUtils;
033import gov.nist.secauto.metaschema.databind.io.AbstractSerializer;
034import gov.nist.secauto.metaschema.databind.model.IAssemblyClassBinding;
035
036import org.codehaus.stax2.XMLOutputFactory2;
037import org.codehaus.stax2.XMLStreamWriter2;
038
039import java.io.IOException;
040import java.io.Writer;
041
042import javax.xml.stream.XMLOutputFactory;
043import javax.xml.stream.XMLStreamException;
044
045import edu.umd.cs.findbugs.annotations.NonNull;
046
047public class DefaultXmlSerializer<CLASS>
048    extends AbstractSerializer<CLASS> {
049  private XMLOutputFactory2 xmlOutputFactory;
050
051  /**
052   * Construct a new XML serializer based on the top-level assembly indicated by
053   * the provided {@code classBinding}.
054   *
055   * @param classBinding
056   *          the bound Module assembly definition that describes the data to
057   *          serialize
058   */
059  public DefaultXmlSerializer(@NonNull IAssemblyClassBinding classBinding) {
060    super(classBinding);
061  }
062
063  /**
064   * Get the configured XML output factory used to create {@link XMLStreamWriter2}
065   * instances.
066   *
067   * @return the factory
068   */
069  @NonNull
070  protected final XMLOutputFactory2 getXMLOutputFactory() {
071    synchronized (this) {
072      if (xmlOutputFactory == null) {
073        xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory.newInstance();
074        assert xmlOutputFactory instanceof WstxOutputFactory;
075        xmlOutputFactory.configureForSpeed();
076        xmlOutputFactory.setProperty(WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true);
077        xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
078      }
079      assert xmlOutputFactory != null;
080      return xmlOutputFactory;
081    }
082  }
083
084  /**
085   * Override the default {@link XMLOutputFactory2} instance with a custom
086   * factory.
087   *
088   * @param xmlOutputFactory
089   *          the new factory
090   */
091  protected void setXMLOutputFactory(@NonNull XMLOutputFactory2 xmlOutputFactory) {
092    synchronized (this) {
093      this.xmlOutputFactory = xmlOutputFactory;
094    }
095  }
096
097  /**
098   * Create a new stream writer using the provided writer.
099   *
100   * @param writer
101   *          the writer to use for output
102   * @return the stream writer created by the output factory
103   * @throws IOException
104   *           if an error occurred while creating the writer
105   */
106  @NonNull
107  protected final XMLStreamWriter2 newXMLStreamWriter(@NonNull Writer writer) throws IOException {
108    try {
109      return ObjectUtils.notNull((XMLStreamWriter2) getXMLOutputFactory().createXMLStreamWriter(writer));
110    } catch (XMLStreamException ex) {
111      throw new IOException(ex);
112    }
113  }
114
115  @Override
116  public void serialize(CLASS data, Writer writer) throws IOException {
117    XMLStreamWriter2 streamWriter = newXMLStreamWriter(writer);
118    IOException caughtException = null;
119    IAssemblyClassBinding classBinding = getClassBinding();
120
121    MetaschemaXmlWriter xmlGenerator = new MetaschemaXmlWriter(streamWriter);
122
123    try {
124      xmlGenerator.write(classBinding, data);
125      streamWriter.flush();
126    } catch (XMLStreamException ex) {
127      caughtException = new IOException(ex);
128      throw caughtException;
129    } finally { // NOPMD - exception handling is needed
130      try {
131        streamWriter.close();
132      } catch (XMLStreamException ex) {
133        if (caughtException == null) {
134          throw new IOException(ex);
135        }
136        caughtException.addSuppressed(ex);
137        throw caughtException; // NOPMD - intentional
138      }
139    }
140  }
141}