Class Minify

java.lang.Object
io.sf.carte.doc.style.css.util.Minify

public class Minify extends Object
Minify a CSS style sheet.

The regular minification methods first attempt a full minification by using the CSS parser, but in case that an unrecognized syntax is found, a safe, shallow minified serialization is used (with no CSS parsing involved).

  • Constructor Details

    • Minify

      public Minify()
  • Method Details

    • main

      public static void main(String[] args) throws URISyntaxException, IllegalArgumentException, IOException
      Prints to standard output a minification of the CSS style sheet located at the supplied URI.

      The argument array must contain the style sheet path or URI, and any additional configuration:

      • --charset, followed by a valid character set name.
      • --disable-shorthand. If followed by a comma-separated list of shorthand names, it disables the advanced minification for those shorthands. If alone, all advanced shorthand minification is disabled (same as --disable-shorthand all).

      If the configuration options are invalid, exits with a status code of 2.

      If the given sheet contains error(s), a shallower minification is performed and the process exits with a status of 1.

      Parameters:
      args - the arguments, including at least the URI or path to a style sheet.
      Throws:
      URISyntaxException - if the URI has wrong syntax.
      IllegalArgumentException - if the URI is otherwise incorrect.
      IOException - if an I/O error happened.
    • minifyCSS

      public static String minifyCSS(String css)
      Minifies a CSS style sheet.
      Parameters:
      css - the serialized style sheet.
      Returns:
      the minified serialization.
    • minifyCSS

      public static String minifyCSS(String css, Minify.Config config, PrintStream err)
      Minifies a CSS style sheet and prints parsing errors to a stream.
      Parameters:
      css - the serialized style sheet.
      config - the minification configuration, or null if defaults should be used.
      err - the error reporting stream, or null if no stream.
      Returns:
      the minified serialization.
    • minifyCSS

      public static String minifyCSS(Path cssPath) throws IOException
      Minifies a CSS style sheet.
      Parameters:
      cssPath - the path to a utf-8-encoded style sheet.
      Returns:
      the minified serialization.
      Throws:
      IOException
    • minifyCSS

      public static boolean minifyCSS(Path cssPath, Minify.Config config, StringBuilder buffer) throws IOException
      Minifies a CSS style sheet.
      Parameters:
      cssPath - the path to the style sheet.
      config - the minification configuration, or null if defaults should be used.
      buffer - the buffer to write the minified serialization, or the original file if an error is found.
      Returns:
      true if the style sheet was fully minified, false if an error was found and the minification was shallow.
      Throws:
      IOException
    • minifyCSS

      public static boolean minifyCSS(Path cssPath, Minify.Config config, StringBuilder buffer, PrintStream err) throws IOException
      Minifies a CSS style sheet.
      Parameters:
      cssPath - the path to the style sheet.
      config - the minification configuration, or null if defaults should be used.
      buffer - the buffer to write the minified serialization.
      err - the error reporting stream, or null if no stream.
      Returns:
      true if the style sheet was fully minified, false if a syntax error was found and the minification was shallow.
      Throws:
      IOException
    • minifyCSS

      public static boolean minifyCSS(URL url, Minify.Config config, StringBuilder buffer, PrintStream err) throws IOException
      Minifies a CSS style sheet.
      Parameters:
      url - the url to the style sheet.
      config - the minification configuration, or null if defaults should be used.
      buffer - the buffer to write the minified serialization.
      err - the error reporting stream, or null if no stream.
      Returns:
      true if the style sheet was fully minified, false if a syntax error was found and the minification was shallow.
      Throws:
      IOException
    • shallowMinify

      public static void shallowMinify(Reader cssReader, StringBuilder buffer) throws IOException
      Perform a safe, shallow minification that does not attempt to minify values nor media features.
      Parameters:
      cssReader - the Reader with the style sheet.
      buffer - the output buffer.
      Throws:
      IOException - if an I/O error happened reading the style sheet.
    • shallowMinify

      public static void shallowMinify(Reader cssReader, Minify.ShallowConfig config, StringBuilder buffer) throws IOException
      Perform a safe, shallow minification that does not attempt to minify values nor media features.
      Parameters:
      cssReader - the Reader with the style sheet.
      config - the minification configuration, or null if defaults should be used.
      buffer - the output buffer.
      Throws:
      IOException - if an I/O error happened reading the style sheet.