java.lang.Object
org.htmlunit.cssparser.parser.AbstractCSSParser
Abstract base class for CSS parser implementations.
- Author:
- Ronald Brill
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringaddEscapes(String str) Escapes some chars in the given string.protected LocatorcreateLocator(org.htmlunit.cssparser.parser.javacc.Token t) Returns a new locator for the given token.protected CSSParseExceptioncreateSkipWarning(String messageKey, CSSParseException e) Creates aCSSParseExceptionfor a skipped rule/declaration warning.protected doubledoubleValue(char op, String s) Parses the string into a double.protected abstract LexicalUnitexpr()Process an expression.protected LexicalUnitfunctionInternal(LexicalUnit prev, String funct, LexicalUnit params) Process a function decl.protected DocumentHandlerReturns the document handler.protected CSSErrorHandlerReturns the error handler.protected InputSourceReturns the input source.protected intReturns the pos of the last numeric char in the given string.protected StringgetParserMessage(String key) Returns the message for the given key.abstract StringReturns a string about which CSS language is supported by this parser.protected voidhandleCharset(String characterEncoding, Locator locator) Handles a charset declaration.protected voidHandles the end of a document.protected voidHandles the end of a font-face rule.protected voidhandleEndMedia(MediaQueryList media) Handles the end of a media rule.protected voidhandleEndPage(String name, String pseudoPage) Handles the end of a page rule.protected voidhandleEndSelector(SelectorList selectors) Handles the end of a selector.protected voidhandleIgnorableAtRule(String s, Locator locator) Handles an ignorable at-rule.protected voidhandleImportStyle(String uri, MediaQueryList media, String defaultNamespaceURI, Locator locator) Handles an import style rule.protected voidhandleProperty(String name, LexicalUnit value, boolean important, Locator locator) Handles a property declaration.protected voidHandles the start of a document.protected voidhandleStartFontFace(Locator locator) Handles the start of a font-face rule.protected voidhandleStartMedia(MediaQueryList media, Locator locator) Handles the start of a media rule.protected voidhandleStartPage(String name, String pseudoPage, Locator locator) Handles the start of a page rule.protected voidhandleStartSelector(SelectorList selectors, Locator locator) Handles the start of a selector.protected LexicalUnithexColorInternal(LexicalUnit prev, org.htmlunit.cssparser.parser.javacc.Token t) Processes a hexadecimal color definition.protected LexicalUnithslColorInternal(LexicalUnit prev, String funct, LexicalUnit param) Processes an HSL or HSLA color definition.protected LexicalUnithwbColorInternal(LexicalUnit prev, String funct, LexicalUnit param) Processes an HWB color definition.protected intParses the string into an integer.protected LexicalUnitlabColorInternal(LexicalUnit prev, String funct, LexicalUnit param) Processes a LAB color definition.protected LexicalUnitlchColorInternal(LexicalUnit prev, String funct, LexicalUnit param) Processes an LCH color definition.protected abstract voidProcess a media list.protected StringnormalizeAndValidatePagePseudoClass(org.htmlunit.cssparser.parser.javacc.Token t) Normalizes and validates the page pseudo-class from the given token.parseMedia(InputSource source) Parse the given input source and return the media list.booleanparsePriority(InputSource source) Parse a CSS priority value (e.g. "!parsePropertyValue(InputSource source) Parse a CSS property value.voidparseRule(InputSource source) Parse a CSS rule.parseSelectors(InputSource source) Parse a comma separated list of selectors.protected abstract SelectorListProcess a selector list.voidparseStyleDeclaration(InputSource source) Parse a CSS style declaration (without '{' and '}').voidparseStyleSheet(InputSource source) Parse a CSS document.protected abstract booleanprio()Process a prio.protected abstract voidReInit(org.htmlunit.cssparser.parser.javacc.CharStream charStream) Re-initializes the stream.protected LexicalUnitrgbColorInternal(LexicalUnit prev, String funct, LexicalUnit param) Processes an RGB or RGBA color definition.voidsetDocumentHandler(DocumentHandler handler) Allow an application to register a document event handler.voidsetErrorHandler(CSSErrorHandler handler) Allow an application to register an error event handler.protected abstract voidProcess a style sheet declaration.protected abstract voidProcess a style sheet.protected abstract voidProcess a style sheet rule.protected CSSParseExceptiontoCSSParseException(String messageKey, Object[] msgParams, Locator locator) Converts the specified parameters into aCSSParseException.protected CSSParseExceptiontoCSSParseException(String key, org.htmlunit.cssparser.parser.javacc.ParseException e) Converts the specified ParseException into aCSSParseException.protected CSSParseExceptiontoCSSParseException(org.htmlunit.cssparser.parser.javacc.TokenMgrException e) Converts the specified TokenMgrException into aCSSParseException.protected CSSParseExceptionConverts the specified DOMException into aCSSParseException.Unescapes escaped characters in the specified string, according to the CSS specification.
-
Constructor Details
-
AbstractCSSParser
public AbstractCSSParser()
-
-
Method Details
-
getDocumentHandler
Returns the document handler.- Returns:
- the document handler
-
setDocumentHandler
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
Returns the error handler.- Returns:
- the error handler
-
setErrorHandler
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
Returns the input source.- Returns:
- the input source
-
getParserMessage
Returns the message for the given key.- Parameters:
key- the lookup key- Returns:
- the parser message
-
createLocator
Returns a new locator for the given token.- Parameters:
t- the token to generate the locator for- Returns:
- a new locator
-
addEscapes
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 aCSSParseException.- Parameters:
key- the message lookup keye- the parse exception- Returns:
- a new CSSParseException
-
toCSSParseException
Converts the specified DOMException into aCSSParseException.- Parameters:
e- the DOMException- Returns:
- a new CSSParseException
-
toCSSParseException
protected CSSParseException toCSSParseException(org.htmlunit.cssparser.parser.javacc.TokenMgrException e) Converts the specified TokenMgrException into aCSSParseException.- Parameters:
e- the TokenMgrException- Returns:
- a new CSSParseException
-
toCSSParseException
protected CSSParseException toCSSParseException(String messageKey, Object[] msgParams, Locator locator) Converts the specified parameters into aCSSParseException.- Parameters:
messageKey- the message keymsgParams- the paramslocator- the locator- Returns:
- a new CSSParseException
-
createSkipWarning
Creates aCSSParseExceptionfor a skipped rule/declaration warning.- Parameters:
messageKey- the message keye- a CSSParseException- Returns:
- a new CSSParseException
-
parseStyleSheet
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
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
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
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
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
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
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
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.ParseExceptionProcess a style sheet.- Throws:
CSSParseException- in case of errororg.htmlunit.cssparser.parser.javacc.ParseException- in case of error
-
styleDeclaration
protected abstract void styleDeclaration() throws org.htmlunit.cssparser.parser.javacc.ParseExceptionProcess 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.ParseExceptionProcess 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.ParseExceptionProcess a selector list.- Returns:
- the selector list
- Throws:
org.htmlunit.cssparser.parser.javacc.ParseException- in case of error
-
expr
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.ParseExceptionProcess 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
Handles an ignorable at-rule.- Parameters:
s- the rulelocator- the locator
-
handleCharset
Handles a charset declaration.- Parameters:
characterEncoding- the encodinglocator- the locator
-
handleImportStyle
protected void handleImportStyle(String uri, MediaQueryList media, String defaultNamespaceURI, Locator locator) Handles an import style rule.- Parameters:
uri- the urimedia- the media query listdefaultNamespaceURI- the namespace urilocator- the locator
-
handleStartMedia
Handles the start of a media rule.- Parameters:
media- the media query listlocator- the locator
-
handleEndMedia
Handles the end of a media rule.- Parameters:
media- the media query list
-
handleStartPage
Handles the start of a page rule.- Parameters:
name- the namepseudoPage- the pseudo pagelocator- the locator
-
handleEndPage
Handles the end of a page rule.- Parameters:
name- the namepseudoPage- the pseudo page
-
handleStartFontFace
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
Handles the start of a selector.- Parameters:
selectors- the selector listlocator- the locator
-
handleEndSelector
Handles the end of a selector.- Parameters:
selectors- the selector list
-
handleProperty
Handles a property declaration.- Parameters:
name- the namevalue- the valueimportant- important flaglocator- the locator
-
functionInternal
Process a function decl.- Parameters:
prev- the previous lexical unitfunct- the functionparams- the params- Returns:
- a lexical unit
-
rgbColorInternal
Processes an RGB or RGBA color definition.- Parameters:
prev- the previous lexical unitfunct- the function nameparam- the parameters lexical unit- Returns:
- a new lexical unit
-
hslColorInternal
Processes an HSL or HSLA color definition.- Parameters:
prev- the previous lexical unitfunct- the function nameparam- the parameters lexical unit- Returns:
- a new lexical unit
-
hwbColorInternal
Processes an HWB color definition.- Parameters:
prev- the previous lexical unitfunct- the function nameparam- the parameters lexical unit- Returns:
- a new lexical unit
-
labColorInternal
Processes a LAB color definition.- Parameters:
prev- the previous lexical unitfunct- the function nameparam- the parameters lexical unit- Returns:
- a new lexical unit
-
lchColorInternal
Processes an LCH color definition.- Parameters:
prev- the previous lexical unitfunct- the function nameparam- 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 unitt- the token- Returns:
- a new lexical unit
-
intValue
Parses the string into an integer.- Parameters:
op- the sign chars- the string to parse- Returns:
- the int value
-
doubleValue
Parses the string into a double.- Parameters:
op- the sign chars- the string to parse- Returns:
- the double value
-
getLastNumPos
Returns the pos of the last numeric char in the given string.- Parameters:
s- the string to parse- Returns:
- the pos
-
unescape
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 unescapeunescapeDoubleQuotes- if true unescape double quotes also- Returns:
- the unescaped string
-
normalizeAndValidatePagePseudoClass
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
-