Class AbstractCSSParser

java.lang.Object
org.htmlunit.cssparser.parser.AbstractCSSParser

public abstract class AbstractCSSParser extends Object
Abstract base class for CSS parser implementations.
Author:
Ronald Brill
  • Constructor Details

    • AbstractCSSParser

      public AbstractCSSParser()
  • Method Details

    • getDocumentHandler

      protected DocumentHandler getDocumentHandler()
      Returns the document handler.
      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 behavior 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()
      Returns the error handler.
      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 behavior 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()
      Returns the input source.
      Returns:
      the input source
    • getParserMessage

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

      protected Locator createLocator(org.htmlunit.cssparser.parser.javacc.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, org.htmlunit.cssparser.parser.javacc.ParseException e)
      Converts the specified ParseException into a CSSParseException.
      Parameters:
      key - the message lookup key
      e - the parse exception
      Returns:
      a new CSSParseException
    • toCSSParseException

      protected CSSParseException toCSSParseException(DOMException e)
      Converts the specified DOMException into a CSSParseException.
      Parameters:
      e - the DOMException
      Returns:
      a new CSSParseException
    • toCSSParseException

      protected CSSParseException toCSSParseException(org.htmlunit.cssparser.parser.javacc.TokenMgrException e)
      Converts the specified TokenMgrException into a CSSParseException.
      Parameters:
      e - the TokenMgrException
      Returns:
      a new CSSParseException
    • toCSSParseException

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

      protected CSSParseException createSkipWarning(String messageKey, CSSParseException e)
      Creates a CSSParseException for a skipped rule/declaration warning.
      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 false
      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.
      Returns:
      a string about which CSS language is supported by this parser
    • ReInit

      protected abstract void ReInit(org.htmlunit.cssparser.parser.javacc.CharStream charStream)
      Re-initializes the stream.
      Parameters:
      charStream - the stream
    • styleSheet

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

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

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

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

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

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

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

      protected void handleStartDocument()
      Handles the start of a document.
    • handleEndDocument

      protected void handleEndDocument()
      Handles the end of a document.
    • handleIgnorableAtRule

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

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

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

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

      protected void handleEndMedia(MediaQueryList media)
      Handles the end of a media rule.
      Parameters:
      media - the media query list
    • handleStartPage

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

      protected void handleEndPage(String name, String pseudoPage)
      Handles the end of a page rule.
      Parameters:
      name - the name
      pseudoPage - the pseudo page
    • handleStartFontFace

      protected void handleStartFontFace(Locator locator)
      Handles the start of a font-face rule.
      Parameters:
      locator - the locator
    • handleEndFontFace

      protected void handleEndFontFace()
      Handles the end of a font-face rule.
    • handleStartSelector

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

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

      protected void handleProperty(String name, LexicalUnit value, boolean important, Locator locator)
      Handles a property declaration.
      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)
      Processes an RGB or RGBA color definition.
      Parameters:
      prev - the previous lexical unit
      funct - the function name
      param - the parameters lexical unit
      Returns:
      a new lexical unit
    • hslColorInternal

      protected LexicalUnit hslColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
      Processes an HSL or HSLA color definition.
      Parameters:
      prev - the previous lexical unit
      funct - the function name
      param - the parameters lexical unit
      Returns:
      a new lexical unit
    • hwbColorInternal

      protected LexicalUnit hwbColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
      Processes an HWB color definition.
      Parameters:
      prev - the previous lexical unit
      funct - the function name
      param - the parameters lexical unit
      Returns:
      a new lexical unit
    • labColorInternal

      protected LexicalUnit labColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
      Processes a LAB color definition.
      Parameters:
      prev - the previous lexical unit
      funct - the function name
      param - the parameters lexical unit
      Returns:
      a new lexical unit
    • lchColorInternal

      protected LexicalUnit lchColorInternal(LexicalUnit prev, String funct, LexicalUnit param)
      Processes an LCH color definition.
      Parameters:
      prev - the previous lexical unit
      funct - the function name
      param - the parameters lexical unit
      Returns:
      a new lexical unit
    • hexColorInternal

      protected LexicalUnit hexColorInternal(LexicalUnit prev, org.htmlunit.cssparser.parser.javacc.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 string 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 string into a 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(org.htmlunit.cssparser.parser.javacc.Token t)
      Normalizes and validates the page pseudo-class from the given token.
      Parameters:
      t - the token representing the page pseudo-class
      Returns:
      the normalized pseudo-class string in lowercase
      Throws:
      CSSParseException - if the pseudo-class is invalid