Class CSSReader

java.lang.Object
com.helger.css.reader.CSSReader

@ThreadSafe public final class CSSReader extends Object
This is the central user class for reading and parsing CSS from different sources. This class reads full CSS declarations only. To read only a declaration list (like from an HTML <style> attribute) the CSSReaderDeclarationList is available.
Author:
Philip Helger
  • Method Details

    • getDefaultParseErrorHandler

      @Nullable public static ICSSParseErrorHandler getDefaultParseErrorHandler()
      Returns:
      The default CSS parse error handler. May be null. For backwards compatibility reasons this is be default an instance of LoggingCSSParseErrorHandler.
    • setDefaultParseErrorHandler

      public static void setDefaultParseErrorHandler(@Nullable ICSSParseErrorHandler aDefaultParseErrorHandler)
      Set the default CSS parse error handler (for recoverable errors).
      Parameters:
      aDefaultParseErrorHandler - The new default error handler to be used. May be null to indicate that no special error handler should be used.
    • getDefaultParseExceptionHandler

      @Nonnull public static ICSSParseExceptionCallback getDefaultParseExceptionHandler()
      Returns:
      The default CSS parse exception handler. May not be null. For backwards compatibility reasons this is be default an instance of LoggingCSSParseExceptionCallback.
      Since:
      3.7.4
    • setDefaultParseExceptionHandler

      public static void setDefaultParseExceptionHandler(@Nonnull ICSSParseExceptionCallback aDefaultParseExceptionHandler)
      Set the default CSS parse exception handler (for unrecoverable errors).
      Parameters:
      aDefaultParseExceptionHandler - The new default exception handler to be used. May not be null.
      Since:
      3.7.4
    • getDefaultInterpretErrorHandler

      @Nonnull public static ICSSInterpretErrorHandler getDefaultInterpretErrorHandler()
      Returns:
      The default interpret error handler to handle interpretation errors in successfully parsed CSS. Never null.
      Since:
      5.0.2
    • setDefaultInterpretErrorHandler

      public static void setDefaultInterpretErrorHandler(@Nonnull ICSSInterpretErrorHandler aDefaultErrorHandler)
      Set the default interpret error handler to handle interpretation errors in successfully parsed CSS.
      Parameters:
      aDefaultErrorHandler - The default error handler to be used. May not be null.
      Since:
      5.0.2
    • isValidCSS

      public static boolean isValidCSS(@Nonnull File aFile, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Check if the passed CSS file can be parsed without error
      Parameters:
      aFile - The file to be parsed. May not be null.
      aFallbackCharset - The charset to be used for reading the CSS file in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to be used for scanning. May not be null.
      Returns:
      true if the file can be parsed without error, false if not
    • isValidCSS

      public static boolean isValidCSS(@Nonnull com.helger.commons.io.resource.IReadableResource aRes, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Check if the passed CSS resource can be parsed without error
      Parameters:
      aRes - The resource to be parsed. May not be null.
      aFallbackCharset - The charset to be used for reading the CSS file in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to be used for scanning. May not be null.
      Returns:
      true if the file can be parsed without error, false if not
    • isValidCSS

      public static boolean isValidCSS(@Nonnull @WillClose InputStream aIS, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Check if the passed input stream can be resembled to valid CSS content. This is accomplished by fully parsing the CSS file each time the method is called. This is similar to calling readFromStream(IHasInputStream,Charset, ECSSVersion) and checking for a non-null result.
      Parameters:
      aIS - The input stream to use. Is automatically closed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      true if the CSS is valid according to the version, false if not
    • isValidCSS

      public static boolean isValidCSS(@Nonnull String sCSS, @Nonnull ECSSVersion eVersion)
      Check if the passed String can be resembled to valid CSS content. This is accomplished by fully parsing the CSS file each time the method is called. This is similar to calling readFromString(String, Charset, ECSSVersion) and checking for a non-null result.
      Parameters:
      sCSS - The CSS string to scan. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      true if the CSS is valid according to the version, false if not
    • isValidCSS

      public static boolean isValidCSS(@Nonnull @WillClose Reader aReader, @Nonnull ECSSVersion eVersion)
      Check if the passed reader can be resembled to valid CSS content. This is accomplished by fully parsing the CSS each time the method is called. This is similar to calling readFromStream(IHasInputStream, Charset, ECSSVersion) and checking for a non-null result.
      Parameters:
      aReader - The reader to use. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      true if the CSS is valid according to the version, false if not
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Read the CSS from the passed String using a byte stream.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler)
      Read the CSS from the passed String using a byte stream.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed String using a byte stream.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed String using a byte stream.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromStringStream

      @Nullable public static CascadingStyleSheet readFromStringStream(@Nonnull String sCSS, @Nonnull CSSReaderSettings aSettings)
      Read the CSS from the passed String using a byte stream.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aSettings - The settings to be used for reading the CSS. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.8.2
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull ECSSVersion eVersion)
      Read the CSS from the passed String using a character stream. An eventually contained @charset rule is ignored.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler)
      Read the CSS from the passed String using a character stream. An eventually contained @charset rule is ignored.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed String using a character stream. An eventually contained @charset rule is ignored.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromString

      @Nullable public static CascadingStyleSheet readFromString(@Nonnull String sCSS, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed String using a character stream. An eventually contained @charset rule is ignored.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromStringReader

      @Nullable public static CascadingStyleSheet readFromStringReader(@Nonnull String sCSS, @Nonnull CSSReaderSettings aSettings)
      Read the CSS from the passed String using a character stream. An eventually contained @charset rule is ignored.
      Parameters:
      sCSS - The source string containing the CSS to be parsed. May not be null.
      aSettings - The settings to be used for reading the CSS. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.8.2
    • readFromFile

      @Nullable public static CascadingStyleSheet readFromFile(@Nonnull File aFile, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Read the CSS from the passed File.
      Parameters:
      aFile - The file containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromFile

      @Nullable public static CascadingStyleSheet readFromFile(@Nonnull File aFile, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler)
      Read the CSS from the passed File.
      Parameters:
      aFile - The file containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromFile

      @Nullable public static CascadingStyleSheet readFromFile(@Nonnull File aFile, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed File.
      Parameters:
      aFile - The file containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromFile

      @Nullable public static CascadingStyleSheet readFromFile(@Nonnull File aFile, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed File.
      Parameters:
      aFile - The file containing the CSS to be parsed. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromFile

      @Nullable public static CascadingStyleSheet readFromFile(@Nonnull File aFile, @Nonnull CSSReaderSettings aSettings)
      Read the CSS from the passed File.
      Parameters:
      aFile - The file containing the CSS to be parsed. May not be null.
      aSettings - The settings to be used for reading the CSS. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.8.2
    • readFromStream

      @Nullable public static CascadingStyleSheet readFromStream(@Nonnull com.helger.commons.io.IHasInputStream aISP, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion)
      Read the CSS from the passed IHasInputStream. If the CSS contains an explicit charset, the whole CSS is parsed again, with the charset found inside the file, so the passed IHasInputStream must be able to create a new input stream on second invocation!
      Parameters:
      aISP - The input stream provider to use. Must be able to create new input streams on every invocation, in case an explicit charset node was found. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromStream

      @Nullable public static CascadingStyleSheet readFromStream(@Nonnull com.helger.commons.io.IHasInputStream aISP, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler)
      Read the CSS from the passed IHasInputStream. If the CSS contains an explicit charset, the whole CSS is parsed again, with the charset found inside the file, so the passed IHasInputStream must be able to create a new input stream on second invocation!
      Parameters:
      aISP - The input stream provider to use. Must be able to create new input streams on every invocation, in case an explicit charset node was found. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • getCharsetDeclaredInCSS

      @Nullable public static Charset getCharsetDeclaredInCSS(@Nonnull com.helger.commons.io.IHasInputStream aISP)
      Determine the charset to read the CSS file. The logic is as follows:
      1. Determine the charset used to read the @charset from the stream. If a BOM is present and a matching Charset is present, this charset is used. As a fallback the CSS file is initially read with ISO-8859-1.
      2. If the CSS content contains a valid @charset rule, the defined charset is returned even if a different BOM is present.
      3. If the CSS content does not contain a valid @charset rule than the charset of the BOM is returned (if any).
      4. Otherwise null is returned.
      Parameters:
      aISP - The input stream provider to read from. May not be null .
      Returns:
      null if the input stream could not be opened or if neither a BOM nor a charset is specified. Otherwise a non- null Charset is returned.
      Throws:
      IllegalStateException - if an invalid charset is supplied
    • readFromStream

      @Nullable public static CascadingStyleSheet readFromStream(@Nonnull com.helger.commons.io.IHasInputStream aISP, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed IHasInputStream. If the CSS contains an explicit charset, the whole CSS is parsed again, with the charset found inside the file, so the passed IHasInputStream must be able to create a new input stream on second invocation!
      Parameters:
      aISP - The input stream provider to use. Must be able to create new input streams on every invocation, in case an explicit charset node was found. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromStream

      @Nullable public static CascadingStyleSheet readFromStream(@Nonnull com.helger.commons.io.IHasInputStream aISP, @Nonnull Charset aFallbackCharset, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed IHasInputStream. If the CSS contains an explicit charset, the whole CSS is parsed again, with the charset found inside the file, so the passed IHasInputStream must be able to create a new input stream on second invocation!
      Parameters:
      aISP - The input stream provider to use. Must be able to create new input streams on every invocation, in case an explicit charset node was found. May not be null.
      aFallbackCharset - The charset to be used in case neither a @charset rule nor a BOM is present. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
    • readFromStream

      @Nullable public static CascadingStyleSheet readFromStream(@Nonnull com.helger.commons.io.IHasInputStream aISP, @Nonnull CSSReaderSettings aSettings)
      Read the CSS from the passed IHasInputStream. If the CSS contains an explicit charset, the whole CSS is parsed again, with the charset found inside the file, so the passed IHasInputStream must be able to create a new input stream on second invocation!
      Parameters:
      aISP - The input stream provider to use. Must be able to create new input streams on every invocation, in case an explicit charset node was found. May not be null.
      aSettings - The settings to be used for reading the CSS. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.8.2
    • readFromReader

      @Nullable public static CascadingStyleSheet readFromReader(@Nonnull com.helger.commons.io.IHasReader aRP, @Nonnull ECSSVersion eVersion, @Nullable ICSSParseErrorHandler aCustomErrorHandler, @Nullable ICSSParseExceptionCallback aCustomExceptionHandler)
      Read the CSS from the passed IHasReader. If the CSS contains an explicit @charset rule, it is ignored and the charset used to create the reader is used instead!
      Parameters:
      aRP - The reader provider to use. The reader is retrieved exactly once and closed anyway. May not be null.
      eVersion - The CSS version to use. May not be null.
      aCustomErrorHandler - An optional custom error handler that can be used to collect the recoverable parsing errors. May be null.
      aCustomExceptionHandler - An optional custom exception handler that can be used to collect the unrecoverable parsing errors. May be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.7.3
    • readFromReader

      @Nullable public static CascadingStyleSheet readFromReader(@Nonnull com.helger.commons.io.IHasReader aRP, @Nonnull CSSReaderSettings aSettings)
      Read the CSS from the passed IHasReader. If the CSS contains an explicit @charset rule, it is ignored and the charset used to create the reader is used instead! Also the fallback charset from the CSSReaderSettings is ignored.
      Parameters:
      aRP - The reader provider to use. The reader is retrieved exactly once and closed anyway. May not be null.
      aSettings - The settings to be used for reading the CSS. May not be null.
      Returns:
      null if reading failed, the CSS declarations otherwise.
      Since:
      3.8.2