2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.LinkedHashMap;
26 import java.util.List;
31 * A singleton registry of alignment file formats known to Jalview. On startup,
32 * the 'built-in' formats are added (from the FileFormat enum). Additional
33 * formats can be registered (or formats deregistered) programmatically, for
34 * example with a Groovy script.
39 public class FileFormats
41 private static FileFormats instance = new FileFormats();
44 * A lookup map of file formats by upper-cased name
46 private static Map<String, FileFormatI> formats;
49 * Formats in this set are capable of being identified by IdentifyFile
51 private static Set<FileFormatI> identifiable;
53 public static FileFormats getInstance()
59 * Private constructor registers Jalview's built-in file formats
67 * Reset to just the built-in file formats packaged with Jalview. These are
68 * added (and will be shown in menus) in the order of their declaration in the
71 public synchronized void reset()
73 formats = new LinkedHashMap<String, FileFormatI>();
74 identifiable = new HashSet<FileFormatI>();
75 for (FileFormat format : FileFormat.values())
77 registerFileFormat(format, format.isIdentifiable());
82 * Answers true if the format is one that can be identified by IdentifyFile.
83 * Answers false for a null value.
85 public boolean isIdentifiable(FileFormatI f)
87 return identifiable.contains(f);
91 * Registers a file format for case-insensitive lookup by name
95 public void registerFileFormat(FileFormatI format)
97 boolean isIdentifiable = format instanceof FileFormat
98 && ((FileFormat) format).isIdentifiable();
99 registerFileFormat(format, isIdentifiable);
102 protected void registerFileFormat(FileFormatI format,
103 boolean isIdentifiable)
105 String name = format.getName().toUpperCase();
106 if (formats.containsKey(name))
108 System.err.println("Overwriting file format: " + format.getName());
110 formats.put(name, format);
113 identifiable.add(format);
118 * Deregisters a file format so it is no longer shown in menus
122 public void deregisterFileFormat(String name)
124 FileFormatI ff = formats.remove(name.toUpperCase());
125 identifiable.remove(ff);
129 * Answers a list of writeable file formats (as strings, corresponding to the
130 * getName() and forName() methods)
133 * if true, only text (not binary) formats are included
136 public List<String> getWritableFormats(boolean textOnly)
138 List<String> l = new ArrayList<String>();
139 for (FileFormatI ff : formats.values())
141 if (ff.isWritable() && (!textOnly || ff.isTextFormat()))
150 * Answers a list of readable file formats (as strings, corresponding to the
151 * getName() and forName() methods)
155 public List<String> getReadableFormats()
157 List<String> l = new ArrayList<String>();
158 for (FileFormatI ff : formats.values())
169 * Returns the file format with the given name, or null if format is null or
170 * invalid. This is not case-sensitive.
175 public FileFormatI forName(String format)
177 return format == null ? null : formats.get(format.toUpperCase());
181 * Returns an iterable collection of registered file formats (in the order in
182 * which they were registered)
186 public Iterable<FileFormatI> getFormats()
188 return formats.values();