package jalview.io; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * A singleton registry of alignment file formats known to Jalview. On startup, * the 'built-in' formats are added (from the FileFormat enum). Additional * formats can be registered (or formats deregistered) programmatically, for * example with a Groovy script. * * @author gmcarstairs * */ public class FileFormats { private static FileFormats instance = new FileFormats(); /* * A lookup map of file formats by upper-cased name */ private static Map formats; /* * Formats in this set are built in to Jalview and instantiated * on startup, any others are added dynamically */ private static Set builtIn; public static FileFormats getInstance() { return instance; } /** * Private constructor registers Jalview's built-in file formats */ private FileFormats() { reset(); } /** * Reset to just the built-in file formats packaged with Jalview. These are * added (and will be shown in menus) in the order of their declaration in the * FileFormat enum. */ public synchronized void reset() { formats = new LinkedHashMap(); builtIn = new HashSet(); for (FileFormat format : FileFormat.values()) { registerFileFormat(format, true); } } /** * Answers false if the format is one 'built in' to Jalview, or true if not * (meaning it has been added dynamically at runtime). Only built-in formats * can be validated by IdentifyFile. Answers true for a null input. */ public boolean isDynamic(FileFormatI f) { return !builtIn.contains(f); } /** * Registers a file format for case-insensitive lookup by name * * @param format */ public void registerFileFormat(FileFormatI format) { registerFileFormat(format, false); } protected void registerFileFormat(FileFormatI format, boolean isBuiltIn) { String name = format.getName().toUpperCase(); if (formats.containsKey(name)) { System.err.println("Overwriting file format: " + format.getName()); } formats.put(name, format); if (isBuiltIn) { builtIn.add(format); } } /** * Deregisters a file format so it is no longer shown in menus * * @param name */ public void deregisterFileFormat(String name) { FileFormatI ff = formats.remove(name.toUpperCase()); builtIn.remove(ff); } /** * Answers a list of writeable file formats (as strings, corresponding to the * getName() and forName() methods) * * @param textOnly * if true, only text (not binary) formats are included * @return */ public List getWritableFormats(boolean textOnly) { List l = new ArrayList(); for (FileFormatI ff : formats.values()) { if (ff.isWritable() && (!textOnly || ff.isTextFormat())) { l.add(ff.getName()); } } return l; } /** * Answers a list of readable file formats (as strings, corresponding to the * getName() and forName() methods) * * @return */ public List getReadableFormats() { List l = new ArrayList(); for (FileFormatI ff : formats.values()) { if (ff.isReadable()) { l.add(ff.getName()); } } return l; } /** * Returns the file format with the given name, or null if format is null or * invalid. This is not case-sensitive. * * @param format * @return */ public FileFormatI forName(String format) { return format == null ? null : formats.get(format.toUpperCase()); } /** * Returns an iterable collection of registered file formats (in the order in * which they were registered) * * @return */ public Iterable getFormats() { return formats.values(); } }