Class AbstractCSSParser

  • Direct Known Subclasses:
    CSS3Parser

    public abstract class AbstractCSSParser
    extends java.lang.Object
    Base implementation.
    • Field Detail

      • parserMessages_

        private static final java.util.HashMap<java.lang.String,​java.lang.String> parserMessages_
    • Constructor Detail

      • AbstractCSSParser

        public AbstractCSSParser()
    • Method Detail

      • getDocumentHandler

        protected DocumentHandler 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:
        DocumentHandler
      • getErrorHandler

        protected CSSErrorHandler 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:
        CSSErrorHandler, CSSException
      • getInputSource

        protected InputSource getInputSource()
        Returns:
        the input source
      • getParserMessage

        protected java.lang.String getParserMessage​(java.lang.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 java.lang.String addEscapes​(java.lang.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​(java.lang.String key,
                                                        ParseException e)
        Parameters:
        key - the message lookup key
        e - the parse exception
        Returns:
        a new CSSParseException
      • toCSSParseException

        protected CSSParseException toCSSParseException​(org.w3c.dom.DOMException e)
        Parameters:
        e - the DOMException
        Returns:
        a new CSSParseException
      • toCSSParseException

        protected CSSParseException toCSSParseException​(TokenMgrError e)
        Parameters:
        e - the TokenMgrError
        Returns:
        a new CSSParseException
      • toCSSParseException

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

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

        public void parseStyleSheet​(InputSource source)
                             throws java.io.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.
        java.io.IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
        See Also:
        InputSource, setDocumentHandler(org.htmlunit.cssparser.parser.DocumentHandler), setErrorHandler(org.htmlunit.cssparser.parser.CSSErrorHandler)
      • parseStyleDeclaration

        public void parseStyleDeclaration​(InputSource source)
                                   throws java.io.IOException
        Parse a CSS style declaration (without '{' and '}').
        Parameters:
        source - source to be parsed
        Throws:
        CSSException - Any CSS exception, possibly wrapping another exception.
        java.io.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 java.io.IOException
        Parse a CSS rule.
        Parameters:
        source - source to be parsed
        Throws:
        CSSException - Any CSS exception, possibly wrapping another exception.
        java.io.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 java.io.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.
        java.io.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 java.io.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.
        java.io.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 java.io.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.
        java.io.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 java.io.IOException
        Parse the given input source and return the media list.
        Parameters:
        source - the input source
        Returns:
        new media list
        Throws:
        java.io.IOException - in case of errors
      • getCharStream

        private static CharStream getCharStream​(InputSource source)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • getParserVersion

        public abstract java.lang.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
      • 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
      • prio

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

        protected void handleStartDocument()
        start document handler.
      • handleEndDocument

        protected void handleEndDocument()
        end document handler.
      • handleIgnorableAtRule

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

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

        protected void handleImportStyle​(java.lang.String uri,
                                         MediaQueryList media,
                                         java.lang.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​(java.lang.String name,
                                       java.lang.String pseudoPage,
                                       Locator locator)
        start page handler.
        Parameters:
        name - the name
        pseudoPage - the pseudo page
        locator - the locator
      • handleEndPage

        protected void handleEndPage​(java.lang.String name,
                                     java.lang.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​(java.lang.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,
                                               java.lang.String funct,
                                               LexicalUnit params)
        Process a function decl.
        Parameters:
        prev - the previous lexical unit
        funct - the function
        params - the params
        Returns:
        a lexical unit
      • 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,
                               java.lang.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,
                                     java.lang.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​(java.lang.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 java.lang.String unescape​(java.lang.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
      • hexval

        private static int hexval​(char c)
      • normalizeAndValidatePagePseudoClass

        protected java.lang.String normalizeAndValidatePagePseudoClass​(Token t)