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.util.CollectionUtil;
009
010import java.util.Arrays;
011import java.util.HashSet;
012import java.util.List;
013import java.util.Locale;
014import java.util.Set;
015import java.util.stream.Collectors;
016
017import edu.umd.cs.findbugs.annotations.NonNull;
018import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
019
020/**
021 * Selections of serialization formats.
022 */
023public enum Format {
024  /**
025   * The <a href="https://www.w3.org/XML/">Extensible Markup Language</a> format.
026   */
027  XML(".xml", Set.of()),
028  /**
029   * The <a href="https://www.json.org/">JavaScript Object Notation</a> format.
030   */
031  JSON(".json", Set.of()),
032  /**
033   * The <a href="https://yaml.org/">YAML Ain't Markup Language</a> format.
034   */
035  YAML(".yaml", Set.of(".yml"));
036
037  private static final List<String> NAMES;
038
039  @NonNull
040  private final String defaultExtension;
041  @NonNull
042  private final Set<String> recognizedExtensions;
043
044  static {
045    NAMES = Arrays.stream(values())
046        .map(format -> format.name().toLowerCase(Locale.ROOT))
047        .collect(Collectors.toUnmodifiableList());
048  }
049
050  /**
051   * Get a list of all format names in lowercase.
052   *
053   * @return the list of names
054   */
055  @SuppressFBWarnings(value = "MS_EXPOSE_REP", justification = "Exposes names provided by the enum")
056  public static List<String> names() {
057    return NAMES;
058  }
059
060  Format(@NonNull String defaultExtension, Set<String> otherExtensions) {
061    this.defaultExtension = defaultExtension;
062
063    Set<String> recognizedExtensions = new HashSet<>();
064    recognizedExtensions.add(defaultExtension);
065    recognizedExtensions.addAll(otherExtensions);
066
067    this.recognizedExtensions = CollectionUtil.unmodifiableSet(recognizedExtensions);
068  }
069
070  /**
071   * Get the default extension to use for the format.
072   *
073   * @return the default extension
074   */
075  @NonNull
076  public Set<String> getRecognizedExtensions() {
077    return recognizedExtensions;
078  }
079
080  /**
081   * Get the default extension to use for the format.
082   *
083   * @return the default extension
084   */
085  @NonNull
086  public String getDefaultExtension() {
087    return defaultExtension;
088  }
089}