1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.io;
7   
8   import gov.nist.secauto.metaschema.core.util.CollectionUtil;
9   
10  import java.util.Arrays;
11  import java.util.HashSet;
12  import java.util.List;
13  import java.util.Locale;
14  import java.util.Set;
15  import java.util.stream.Collectors;
16  
17  import edu.umd.cs.findbugs.annotations.NonNull;
18  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19  
20  /**
21   * Selections of serialization formats.
22   */
23  public enum Format {
24    /**
25     * The <a href="https://www.w3.org/XML/">Extensible Markup Language</a> format.
26     */
27    XML(".xml", Set.of()),
28    /**
29     * The <a href="https://www.json.org/">JavaScript Object Notation</a> format.
30     */
31    JSON(".json", Set.of()),
32    /**
33     * The <a href="https://yaml.org/">YAML Ain't Markup Language</a> format.
34     */
35    YAML(".yaml", Set.of(".yml"));
36  
37    private static final List<String> NAMES;
38  
39    @NonNull
40    private final String defaultExtension;
41    @NonNull
42    private final Set<String> recognizedExtensions;
43  
44    static {
45      NAMES = Arrays.stream(values())
46          .map(format -> format.name().toLowerCase(Locale.ROOT))
47          .collect(Collectors.toUnmodifiableList());
48    }
49  
50    /**
51     * Get a list of all format names in lowercase.
52     *
53     * @return the list of names
54     */
55    @SuppressFBWarnings(value = "MS_EXPOSE_REP", justification = "Exposes names provided by the enum")
56    public static List<String> names() {
57      return NAMES;
58    }
59  
60    Format(@NonNull String defaultExtension, Set<String> otherExtensions) {
61      this.defaultExtension = defaultExtension;
62  
63      Set<String> recognizedExtensions = new HashSet<>();
64      recognizedExtensions.add(defaultExtension);
65      recognizedExtensions.addAll(otherExtensions);
66  
67      this.recognizedExtensions = CollectionUtil.unmodifiableSet(recognizedExtensions);
68    }
69  
70    /**
71     * Get the default extension to use for the format.
72     *
73     * @return the default extension
74     */
75    @NonNull
76    public Set<String> getRecognizedExtensions() {
77      return recognizedExtensions;
78    }
79  
80    /**
81     * Get the default extension to use for the format.
82     *
83     * @return the default extension
84     */
85    @NonNull
86    public String getDefaultExtension() {
87      return defaultExtension;
88    }
89  }