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;
28  
29  import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
30  import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
31  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.io.OutputStream;
36  import java.io.OutputStreamWriter;
37  import java.io.Writer;
38  import java.nio.charset.StandardCharsets;
39  import java.nio.file.Files;
40  import java.nio.file.OpenOption;
41  import java.nio.file.Path;
42  import java.nio.file.StandardOpenOption;
43  
44  import edu.umd.cs.findbugs.annotations.NonNull;
45  
46  /**
47   * Implementations of this interface are able to write data in a bound object
48   * instance of the parameterized type to a structured data format.
49   *
50   * @param <CLASS>
51   *          the Java type from which data can be written
52   */
53  public interface ISerializer<CLASS> extends IMutableConfiguration<SerializationFeature<?>> {
54  
55    @Override
56    ISerializer<CLASS> enableFeature(SerializationFeature<?> feature);
57  
58    @Override
59    ISerializer<CLASS> disableFeature(SerializationFeature<?> feature);
60  
61    @Override
62    ISerializer<CLASS> applyConfiguration(IConfiguration<SerializationFeature<?>> other);
63  
64    @Override
65    ISerializer<CLASS> set(SerializationFeature<?> feature, Object value);
66  
67    /**
68     * Write data from a bound class instance to the {@link OutputStream}.
69     * <p>
70     * This method does not have ownership of the the provided output stream and
71     * will not close it.
72     *
73     * @param data
74     *          the instance data
75     * @param os
76     *          the output stream to write to
77     * @throws IOException
78     *           if an error occurred while writing data to the stream
79     */
80    default void serialize(@NonNull CLASS data, @NonNull OutputStream os) throws IOException {
81      OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
82      serialize(data, writer);
83      writer.flush();
84    }
85  
86    /**
87     * Write data from a bound class instance to the {@link File}.
88     *
89     * @param data
90     *          the instance data
91     * @param path
92     *          the file to write to
93     * @param openOptions
94     *          options specifying how the file is opened
95     * @throws IOException
96     *           if an error occurred while writing data to the file indicated by
97     *           the {@code path} parameter
98     */
99    default void serialize(@NonNull CLASS data, @NonNull Path path, OpenOption... openOptions) throws IOException {
100     try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, openOptions)) {
101       assert writer != null;
102       serialize(data, writer);
103     }
104   }
105 
106   /**
107    * Write data from a bound class instance to the {@link File}.
108    *
109    * @param data
110    *          the instance data
111    * @param file
112    *          the file to write to
113    * @throws IOException
114    *           if an error occurred while writing data to the stream
115    */
116   default void serialize(@NonNull CLASS data, @NonNull File file) throws IOException {
117     serialize(data, ObjectUtils.notNull(file.toPath()), StandardOpenOption.CREATE, StandardOpenOption.WRITE,
118         StandardOpenOption.TRUNCATE_EXISTING);
119   }
120 
121   /**
122    * Write data from a bound class instance to the {@link Writer}.
123    *
124    * @param data
125    *          the instance data
126    * @param writer
127    *          the writer to write to
128    * @throws IOException
129    *           if an error occurred while writing data to the stream
130    */
131   void serialize(@NonNull CLASS data, @NonNull Writer writer) throws IOException;
132 }