Class AbstractCSSParser

java.lang.Object
org.htmlunit.cssparser.parser.AbstractCSSParser
Direct Known Subclasses:
CSS3Parser

public abstract class AbstractCSSParser extends Object
Base implementation.
Author:
Ronald Brill
  • Constructor Details

    • AbstractCSSParser

      public AbstractCSSParser()
  • Method Details

    • getDocumentHandler

      protected DocumentHandler getDocumentHandler()

      getDocumentHandler.

      Returns:
      the document handler
    • setDocumentHandler

      public void setDocumentHandler(DocumentHandler handler)
      Allow an application to register a document event handler.

      If the application does not register a document handler, all document events reported by the CSS parser will be silently ignored (this is the default behaviour implemented by HandlerBase).

      Applications may register a new or different handler in the middle of a parse, and the CSS parser must begin using the new handler immediately.

      Parameters:
      handler - The document handler.
      See Also:
    • getErrorHandler

      protected CSSErrorHandler getErrorHandler()

      getErrorHandler.

      Returns:
      the error handler
    • setErrorHandler

      public void setErrorHandler(CSSErrorHandler handler)
      Allow an application to register an error event handler.

      If the application does not register an error event handler, all error events reported by the CSS parser will be silently ignored, except for fatalError, which will throw a CSSException (this is the default behaviour implemented by HandlerBase).

      Applications may register a new or different handler in the middle of a parse, and the CSS parser must begin using the new handler immediately.

      Parameters:
      handler - The error handler.
      See Also:
    • getInputSource

      protected InputSource getInputSource()

      getInputSource.

      Returns:
      the input source
    • getParserMessage

      protected String getParserMessage(String key)
      Parameters:
      key - the lookup key
      Returns:
      the parser message
    • createLocator

      protected Locator createLocator(Token t)
      Returns a new locator for the given token.
      Parameters:
      t - the token to generate the locator for
      Returns:
      a new locator
    • addEscapes

      protected String addEscapes(String str)
      Escapes some chars in the given string.
      Parameters:
      str - the input
      Returns:
      a new string with the escaped values
    • toCSSParseException

      protected CSSParseException toCSSParseException(String key, ParseException e)

      toCSSParseException.

      Parameters:
      key - the message lookup key
      e - the parse exception
      Returns:
      a new CSSParseException
    • toCSSParseException

      protected CSSParseException toCSSParseException(DOMException e)

      toCSSParseException.

      Parameters:
      e - the DOMException
      Returns:
      a new CSSParseException
    • toCSSParseException

      protected CSSParseException toCSSParseException(TokenMgrException e)

      toCSSParseException.

      Parameters:
      e - the TokenMgrException
      Returns:
      a new CSSParseException
    • toCSSParseException

      protected CSSParseException toCSSParseException(String messageKey, Object[] msgParams, Locator locator)
      Parameters:
      messageKey - the message key
      msgParams - the params
      locator - the locator
      Returns:
      a new CSSParseException
    • createSkipWarning

      protected CSSParseException createSkipWarning(String messageKey, CSSParseException e)
      Parameters:
      messageKey - the message key
      e - a CSSParseException
      Returns:
      a new CSSParseException
    • parseStyleSheet

      public void parseStyleSheet(InputSource source) throws IOException
      Parse a CSS document.

      The application can use this method to instruct the CSS parser to begin parsing an CSS document from any valid input source (a character stream, a byte stream, or a URI).

      Applications may not invoke this method while a parse is in progress (they should create a new Parser instead for each additional CSS document). Once a parse is complete, an application may reuse the same Parser object, possibly with a different input source.

      Parameters:
      source - The input source for the top-level of the CSS document.
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
      See Also:
    • parseStyleDeclaration

      public void parseStyleDeclaration(InputSource source) throws IOException
      Parse a CSS style declaration (without '{' and '}').
      Parameters:
      source - source to be parsed
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
    • parseRule

      public void parseRule(InputSource source) throws IOException
      Parse a CSS rule.
      Parameters:
      source - source to be parsed
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
    • parseSelectors

      public SelectorList parseSelectors(InputSource source) throws IOException
      Parse a comma separated list of selectors.
      Parameters:
      source - source to be parsed
      Returns:
      a selector list
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
    • parsePropertyValue

      public LexicalUnit parsePropertyValue(InputSource source) throws IOException
      Parse a CSS property value.
      Parameters:
      source - source to be parsed
      Returns:
      a lexical unit
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
    • parsePriority

      public boolean parsePriority(InputSource source) throws IOException
      Parse a CSS priority value (e.g. "!important").
      Parameters:
      source - source to be parsed
      Returns:
      true or flase
      Throws:
      CSSException - Any CSS exception, possibly wrapping another exception.
      IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
    • parseMedia

      public MediaQueryList parseMedia(InputSource source) throws IOException
      Parse the given input source and return the media list.
      Parameters:
      source - the input source
      Returns:
      new media list
      Throws:
      IOException - in case of errors
    • getParserVersion

      public abstract String getParserVersion()
      Returns:
      a string about which CSS language is supported by this parser. For CSS Level 1, it returns "http://www.w3.org/TR/REC-CSS1", for CSS Level 2, it returns "http://www.w3.org/TR/REC-CSS2". Note that a "CSSx" parser can return lexical unit other than those allowed by CSS Level x but this usage is not recommended.
    • ReInit

      protected abstract void ReInit(CharStream charStream)
      Re intit the stream.
      Parameters:
      charStream - the stream
    • styleSheet

      protected abstract void styleSheet() throws CSSParseException, ParseException
      Process a style sheet.
      Throws:
      CSSParseException - in case of error
      ParseException - in case of error
    • styleDeclaration

      protected abstract void styleDeclaration() throws ParseException
      Process a style sheet declaration.
      Throws:
      ParseException - in case of error
    • styleSheetRuleSingle

      protected abstract void styleSheetRuleSingle() throws ParseException
      Process a style sheet rule.
      Throws:
      ParseException - in case of error
    • parseSelectorsInternal

      protected abstract SelectorList parseSelectorsInternal() throws ParseException
      Process a selector list.
      Returns:
      the selector list
      Throws:
      ParseException - in case of error
    • expr

      protected abstract LexicalUnit expr() throws ParseException
      Process an expression.
      Returns:
      the lexical unit
      Throws:
      ParseException - in case of error
    • prio

      protected abstract boolean prio() throws ParseException
      Process a prio.
      Returns:
      true or false
      Throws:
      ParseException - in case of error
    • mediaList

      protected abstract void mediaList(MediaQueryList ml) throws ParseException
      Process a media list.
      Parameters:
      ml - the media list
      Throws:
      ParseException - in case of error
    • handleStartDocument

      protected void handleStartDocument()
      start document handler.
    • handleEndDocument

      protected void handleEndDocument()
      end document handler.
    • handleIgnorableAtRule

      protected void handleIgnorableAtRule(String s, Locator locator)
      ignorable at rule handler.
      Parameters:
      s - the rule
      locator - the locator
    • handleCharset

      protected void handleCharset(String characterEncoding, Locator locator)
      charset handler.
      Parameters:
      characterEncoding - the encoding
      locator - the locator
    • handleImportStyle

      protected void handleImportStyle(String uri, MediaQueryList media, String defaultNamespaceURI, Locator locator)
      import style handler.
      Parameters:
      uri - the uri
      media - the media query list
      defaultNamespaceURI - the namespace uri
      locator - the locator
    • handleStartMedia

      protected void handleStartMedia(MediaQueryList media, Locator locator)
      start media handler.
      Parameters:
      media - the media query list
      locator - the locator
    • handleEndMedia

      protected void handleEndMedia(MediaQueryList media)
      end media handler.
      Parameters:
      media - the media query list
    • handleStartPage

      protected void handleStartPage(String name, String pseudoPage, Locator locator)
      start page handler.
      Parameters:
      name - the name
      pseudoPage - the pseudo page
      locator - the locator
    • handleEndPage

      protected void handleEndPage(String name, String pseudoPage)
      end page handler.
      Parameters:
      name - the name
      pseudoPage - the pseudo page
    • handleStartFontFace

      protected void handleStartFontFace(Locator locator)
      start font face handler.
      Parameters:
      locator - the locator
    • handleEndFontFace

      protected void handleEndFontFace()
      end font face handler.
    • handleStartSelector

      protected void handleStartSelector(SelectorList selectors, Locator locator)
      selector start handler.
      Parameters:
      selectors - the selector list
      locator - the locator
    • handleEndSelector

      protected void handleEndSelector(SelectorList selectors)
      selector end handler.
      Parameters:
      selectors - the selector list
    • handleProperty

      protected void handleProperty(String name, LexicalUnit value, boolean important, Locator locator)
      property handler.
      Parameters:
      name - the name
      value - the value
      important - important flag
      locator - the locator
    • functionInternal

      protected LexicalUnit functionInternal(LexicalUnit prev, String funct, LexicalUnit params)
      Process a function decl.
      Parameters:
      prev - the previous lexical unit
      funct - the function
      params - the params
      Returns:
      a lexical unit
    • rgbColorInternal

      protected LexicalUnit rgbColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
    • hslColorInternal

      protected LexicalUnit hslColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
    • hwbColorInternal

      protected LexicalUnit hwbColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
    • labColorInternal

      protected LexicalUnit labColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
    • lchColorInternal

      protected LexicalUnit lchColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
    • hexColorInternal

      protected LexicalUnit hexColorInternal(LexicalUnit prev, Token t)
      Processes a hexadecimal color definition.
      Parameters:
      prev - the previous lexical unit
      t - the token
      Returns:
      a new lexical unit
    • intValue

      protected int intValue(char op, String s)
      Parses the sting into an integer.
      Parameters:
      op - the sign char
      s - the string to parse
      Returns:
      the int value
    • doubleValue

      protected double doubleValue(char op, String s)
      Parses the sting into an double.
      Parameters:
      op - the sign char
      s - the string to parse
      Returns:
      the double value
    • getLastNumPos

      protected int getLastNumPos(String s)
      Returns the pos of the last numeric char in the given string.
      Parameters:
      s - the string to parse
      Returns:
      the pos
    • unescape

      public String unescape(String s, boolean unescapeDoubleQuotes)
      Unescapes escaped characters in the specified string, according to the CSS specification. This could be done directly in the parser, but portions of the lexer would have to be moved to the parser, meaning that the grammar would no longer match the standard grammar specified by the W3C. This would make the parser and lexer much less maintainable.
      Parameters:
      s - the string to unescape
      unescapeDoubleQuotes - if true unescape double quotes also
      Returns:
      the unescaped string
    • normalizeAndValidatePagePseudoClass

      protected String normalizeAndValidatePagePseudoClass(Token t)