Class FileFormat

  • Direct Known Subclasses:
    QueryResultFormat, RDFFormat

    public class FileFormat
    extends Object
    Abstract representation of a file format. File formats are identified by a name and can have one or more associated MIME types, zero or more associated file extensions and can specify a (default) character encoding.
    Author:
    Arjohn Kampman
    • Constructor Detail

      • FileFormat

        public FileFormat​(String name,
                          String mimeType,
                          Charset charset,
                          String fileExtension)
        Creates a new FileFormat object.
        Parameters:
        name - The name of the file format, e.g. "PLAIN TEXT".
        mimeType - The (default) MIME type of the file format, e.g. text/plain for plain text files.
        charset - The default character encoding of the file format. Specify null if not applicable.
        fileExtension - The (default) file extension for the file format, e.g. txt for plain text files.
      • FileFormat

        public FileFormat​(String name,
                          String mimeType,
                          Charset charset,
                          Collection<String> fileExtensions)
        Creates a new FileFormat object.
        Parameters:
        name - The name of the file format, e.g. "PLAIN TEXT".
        mimeType - The (default) MIME type of the file format, e.g. text/plain for plain text files.
        charset - The default character encoding of the file format. Specify null if not applicable.
        fileExtensions - The file format's file extension(s), e.g. txt for plain text files. The first item in the list is interpreted as the default file extension for the format.
      • FileFormat

        public FileFormat​(String name,
                          Collection<String> mimeTypes,
                          Charset charset,
                          Collection<String> fileExtensions)
        Creates a new FileFormat object.
        Parameters:
        name - The name of the file format, e.g. "PLAIN TEXT".
        mimeTypes - The MIME type(s) of the file format, e.g. text/plain for theplain text files. The first item in the list is interpreted as the default MIME type for the format. The supplied list should contain at least one MIME type.
        charset - The default character encoding of the file format. Specify null if not applicable.
        fileExtensions - The file format's file extension(s), e.g. txt for plain text files. The first item in the list is interpreted as the default file extension for the format.
    • Method Detail

      • getName

        public String getName()
        Gets the name of this file format.
        Returns:
        A human-readable format name, e.g. "PLAIN TEXT".
      • getDefaultMIMEType

        public String getDefaultMIMEType()
        Gets the default MIME type for this file format.
        Returns:
        A MIME type string, e.g. "text/plain".
      • hasDefaultMIMEType

        public boolean hasDefaultMIMEType​(String mimeType)
        Checks if the specified MIME type matches the FileFormat's default MIME type. The MIME types are compared ignoring upper/lower-case differences.
        Parameters:
        mimeType - The MIME type to compare to the FileFormat's default MIME type.
        Returns:
        true if the specified MIME type matches the FileFormat's default MIME type.
      • getMIMETypes

        public List<String> getMIMETypes()
        Gets the file format's MIME types.
        Returns:
        An unmodifiable list of MIME type strings, e.g. "text/plain".
      • hasMIMEType

        public boolean hasMIMEType​(String mimeType)
        Checks if specified MIME type matches one of the FileFormat's MIME types. The MIME types are compared ignoring upper/lower-case differences.
        Parameters:
        mimeType - The MIME type to compare to the FileFormat's MIME types.
        Returns:
        true if the specified MIME type matches one of the FileFormat's MIME types.
      • getDefaultFileExtension

        public String getDefaultFileExtension()
        Gets the default file name extension for this file format.
        Returns:
        A file name extension (excluding the dot), e.g. "txt", or null if there is no common file extension for the format.
      • hasDefaultFileExtension

        public boolean hasDefaultFileExtension​(String extension)
        Checks if the specified file name extension matches the FileFormat's default file name extension. The file name extension MIME types are compared ignoring upper/lower-case differences.
        Parameters:
        extension - The file extension to compare to the FileFormat's file extension.
        Returns:
        true if the file format has a default file name extension and if it matches the specified extension, false otherwise.
      • getFileExtensions

        public List<String> getFileExtensions()
        Gets the file format's file extensions.
        Returns:
        An unmodifiable list of file extension strings, e.g. "txt".
      • hasFileExtension

        public boolean hasFileExtension​(String extension)
        Checks if the FileFormat's file extension is equal to the specified file extension. The file extensions are compared ignoring upper/lower-case differences.
        Parameters:
        extension - The file extension to compare to the FileFormat's file extension.
        Returns:
        true if the specified file extension is equal to the FileFormat's file extension.
      • getCharset

        public Charset getCharset()
        Get the (default) charset for this file format.
        Returns:
        the (default) charset for this file format, or null if this format does not have a default charset.
      • hasCharset

        public boolean hasCharset()
        Checks if the FileFormat has a (default) charset.
        Returns:
        true if the FileFormat has a (default) charset.
      • equals

        public boolean equals​(Object other)
        Compares FileFormat objects based on their name, ignoring case.
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • matchMIMEType

        public static <FF extends FileFormatOptional<FF> matchMIMEType​(String mimeType,
                                                                         Iterable<FF> fileFormats)
        Tries to match the specified MIME type with the MIME types of the supplied file formats.
        Parameters:
        mimeType - A MIME type, e.g. "text/plain".
        fileFormats - The file formats to match the MIME type against.
        Returns:
        A FileFormat object if the MIME type was recognized, or Optional.empty() otherwise.
      • matchFileName

        public static <FF extends FileFormatOptional<FF> matchFileName​(String fileName,
                                                                         Iterable<FF> fileFormats)
        Tries to match the specified file name with the file extensions of the supplied file formats.
        Parameters:
        fileName - A file name.
        fileFormats - The file formats to match the file name extension against.
        Returns:
        A FileFormat that matches the file name extension, or Optional.empty() otherwise.