Class SharedMimeInfo

java.lang.Object
org.bzdev.util.SharedMimeInfo
All Implemented Interfaces:
Closeable, AutoCloseable

public class SharedMimeInfo extends Object implements Closeable
Utility class for generating a freedesktop.org shared-mime-info file. These files are used to specify how the media type (MIME type) of a file can be determined. The BZDev class library provides support for file formats that are stylized ZIP files or stylized Java property files, both with the media type encoded in the files. This class provides an easy way to generate shared-mime-info entries.

The constructors determine the output stream. After a series of 'add' methods, the method close() must be called (either directly or via a Java try-with-resources statement) once all the entries have been added.

It is easy use the ESP scripting language. The following program is a good example:


 #!/usr/bin/scrunner -sEUN:true
 import org.bzdev.util.SharedMimeInfo;

 new SharedMimeInfo(global.getWriter())
    .addConfigPropertyType(90, "application/foo", "foo",
                           "foo type")
    .addZipDocType(80, "application/bar+zip", "bar",
                   "bar type")
    .close();
 
When run with no arguments, this script will print a shared-mime-info file to standard output. To redirect it to a file, add the scrunner argument -o FILE when the script is run: if the script is named createMime, then one can run the command

   ./createMime -o MimeInfo.xml
 
  • Constructor Details

    • SharedMimeInfo

      public SharedMimeInfo(File file) throws IOException
      Constructor given a file.
      Parameters:
      file - a file used for output
      Throws:
      IOException - if an IO error occurred
    • SharedMimeInfo

      public SharedMimeInfo() throws IOException
      Constructor to write to standard output.
      Throws:
      IOException - if an IO error occurred
    • SharedMimeInfo

      public SharedMimeInfo(Writer writer)
      Constructor given a writer.
      Parameters:
      writer - a Writer used for output
  • Method Details

    • close

      public void close() throws IOException
      Close this SharedMimeInfo. If the constructor's argument was not a file, the file being written will be closed, but not otherwise. In all cases, the output will be flushed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • addConfigPropertyType

      public SharedMimeInfo addConfigPropertyType(int priority, String mediaType, String suffix, String comment) throws IOException, IllegalArgumentException
      Add an entry to a share-mime-info file for a media type readable by ConfigPropertyEditor.
      Parameters:
      priority - the priority as an integer in the range [0,100]
      mediaType - the media type (MIME type)
      suffix - the file-name suffix for this type (null if there isn't one)
      comment - a comment describing the media type; null if there is none
      Returns:
      this object
      Throws:
      IllegalArgumentException - for an illegal argument
      IOException - if an IO error occurs
    • addZipDocType

      public SharedMimeInfo addZipDocType(int priority, String mediaType, String suffix, String comment) throws IOException, IllegalArgumentException
      Add an entry to a share-mime-info file for a media type readable by ZipDocFile.

      This format is a stylized ZIP format, so the media type should end with the string "+zip".

      Parameters:
      priority - the priority as an integer in the range [0,100]
      mediaType - the media type (MIME type)
      suffix - the file-name suffix for this type (null if there isn't one)
      comment - a comment describing the media type; null if there is none
      Returns:
      this object
      Throws:
      IllegalArgumentException - for an illegal argument
      IOException - if an IO error occurs