001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.io;
007
008import gov.nist.secauto.metaschema.core.configuration.IConfiguration;
009import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
010import gov.nist.secauto.metaschema.core.model.IBoundObject;
011import gov.nist.secauto.metaschema.core.util.ObjectUtils;
012
013import java.io.File;
014import java.io.IOException;
015import java.io.OutputStream;
016import java.io.OutputStreamWriter;
017import java.io.Writer;
018import java.nio.charset.StandardCharsets;
019import java.nio.file.Files;
020import java.nio.file.OpenOption;
021import java.nio.file.Path;
022import java.nio.file.StandardOpenOption;
023
024import edu.umd.cs.findbugs.annotations.NonNull;
025
026/**
027 * Implementations of this interface are able to write data in a bound object
028 * instance of the parameterized type to a structured data format.
029 *
030 * @param <CLASS>
031 *          the Java type from which data can be written
032 */
033public interface ISerializer<CLASS extends IBoundObject> extends IMutableConfiguration<SerializationFeature<?>> {
034
035  @Override
036  ISerializer<CLASS> enableFeature(SerializationFeature<?> feature);
037
038  @Override
039  ISerializer<CLASS> disableFeature(SerializationFeature<?> feature);
040
041  @Override
042  ISerializer<CLASS> applyConfiguration(IConfiguration<SerializationFeature<?>> other);
043
044  @Override
045  ISerializer<CLASS> set(SerializationFeature<?> feature, Object value);
046
047  /**
048   * Write data from a bound class instance to the {@link OutputStream}.
049   * <p>
050   * This method does not have ownership of the the provided output stream and
051   * will not close it.
052   *
053   * @param data
054   *          the instance data
055   * @param os
056   *          the output stream to write to
057   * @throws IOException
058   *           if an error occurred while writing data to the stream
059   */
060  default void serialize(@NonNull IBoundObject data, @NonNull OutputStream os) throws IOException {
061    OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
062    serialize(data, writer);
063    writer.flush();
064  }
065
066  /**
067   * Write data from a bound class instance to the {@link File}.
068   *
069   * @param data
070   *          the instance data
071   * @param path
072   *          the file to write to
073   * @param openOptions
074   *          options specifying how the file is opened
075   * @throws IOException
076   *           if an error occurred while writing data to the file indicated by
077   *           the {@code path} parameter
078   */
079  default void serialize(@NonNull IBoundObject data, @NonNull Path path, OpenOption... openOptions) throws IOException {
080    try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, openOptions)) {
081      assert writer != null;
082      serialize(data, writer);
083    }
084  }
085
086  /**
087   * Write data from a bound class instance to the {@link File}.
088   *
089   * @param data
090   *          the instance data
091   * @param file
092   *          the file to write to
093   * @throws IOException
094   *           if an error occurred while writing data to the stream
095   */
096  default void serialize(@NonNull IBoundObject data, @NonNull File file) throws IOException {
097    serialize(data, ObjectUtils.notNull(file.toPath()), StandardOpenOption.CREATE, StandardOpenOption.WRITE,
098        StandardOpenOption.TRUNCATE_EXISTING);
099  }
100
101  /**
102   * Write data from a bound class instance to the {@link Writer}.
103   *
104   * @param data
105   *          the instance data
106   * @param writer
107   *          the writer to write to
108   * @throws IOException
109   *           if an error occurred while writing data to the stream
110   */
111  void serialize(@NonNull IBoundObject data, @NonNull Writer writer) throws IOException;
112}