Class AbstractCSSParser
- java.lang.Object
-
- org.htmlunit.cssparser.parser.AbstractCSSParser
-
- Direct Known Subclasses:
CSS3Parser
public abstract class AbstractCSSParser extends java.lang.Object
Base implementation.
-
-
Field Summary
Fields Modifier and Type Field Description private DocumentHandler
documentHandler_
private CSSErrorHandler
errorHandler_
private static java.lang.String
NUM_CHARS
private static java.util.HashMap<java.lang.String,java.lang.String>
parserMessages_
private InputSource
source_
-
Constructor Summary
Constructors Constructor Description AbstractCSSParser()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
addEscapes(java.lang.String str)
Escapes some chars in the given string.protected Locator
createLocator(Token t)
Returns a new locator for the given token.protected CSSParseException
createSkipWarning(java.lang.String messageKey, CSSParseException e)
protected double
doubleValue(char op, java.lang.String s)
Parses the sting into an double.protected abstract LexicalUnit
expr()
Process an expression.protected LexicalUnit
functionInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit params)
Process a function decl.private static CharStream
getCharStream(InputSource source)
protected DocumentHandler
getDocumentHandler()
protected CSSErrorHandler
getErrorHandler()
protected InputSource
getInputSource()
protected int
getLastNumPos(java.lang.String s)
Returns the pos of the last numeric char in the given string.protected java.lang.String
getParserMessage(java.lang.String key)
abstract java.lang.String
getParserVersion()
protected void
handleCharset(java.lang.String characterEncoding, Locator locator)
charset handler.protected void
handleEndDocument()
end document handler.protected void
handleEndFontFace()
end font face handler.protected void
handleEndMedia(MediaQueryList media)
end media handler.protected void
handleEndPage(java.lang.String name, java.lang.String pseudoPage)
end page handler.protected void
handleEndSelector(SelectorList selectors)
selector end handler.protected void
handleIgnorableAtRule(java.lang.String s, Locator locator)
ignorable at rule handler.protected void
handleImportStyle(java.lang.String uri, MediaQueryList media, java.lang.String defaultNamespaceURI, Locator locator)
import style handler.protected void
handleProperty(java.lang.String name, LexicalUnit value, boolean important, Locator locator)
property handler.protected void
handleStartDocument()
start document handler.protected void
handleStartFontFace(Locator locator)
start font face handler.protected void
handleStartMedia(MediaQueryList media, Locator locator)
start media handler.protected void
handleStartPage(java.lang.String name, java.lang.String pseudoPage, Locator locator)
start page handler.protected void
handleStartSelector(SelectorList selectors, Locator locator)
selector start handler.protected LexicalUnit
hexColorInternal(LexicalUnit prev, Token t)
Processes a hexadecimal color definition.private static int
hexval(char c)
protected LexicalUnit
hslColorInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit param)
protected int
intValue(char op, java.lang.String s)
Parses the sting into an integer.protected abstract void
mediaList(MediaQueryList ml)
Process a media list.protected java.lang.String
normalizeAndValidatePagePseudoClass(Token t)
MediaQueryList
parseMedia(InputSource source)
Parse the given input source and return the media list.boolean
parsePriority(InputSource source)
Parse a CSS priority value (e.g.LexicalUnit
parsePropertyValue(InputSource source)
Parse a CSS property value.void
parseRule(InputSource source)
Parse a CSS rule.SelectorList
parseSelectors(InputSource source)
Parse a comma separated list of selectors.protected abstract SelectorList
parseSelectorsInternal()
Process a selector list.void
parseStyleDeclaration(InputSource source)
Parse a CSS style declaration (without '{' and '}').void
parseStyleSheet(InputSource source)
Parse a CSS document.protected abstract boolean
prio()
Process a prio.protected abstract void
ReInit(CharStream charStream)
Re intit the stream.protected LexicalUnit
rgbColorInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit param)
void
setDocumentHandler(DocumentHandler handler)
Allow an application to register a document event handler.void
setErrorHandler(CSSErrorHandler handler)
Allow an application to register an error event handler.protected abstract void
styleDeclaration()
Process a style sheet declaration.protected abstract void
styleSheet()
Process a style sheet.protected abstract void
styleSheetRuleSingle()
Process a style sheet rule.protected CSSParseException
toCSSParseException(java.lang.String messageKey, java.lang.Object[] msgParams, Locator locator)
protected CSSParseException
toCSSParseException(java.lang.String key, ParseException e)
protected CSSParseException
toCSSParseException(TokenMgrError e)
protected CSSParseException
toCSSParseException(org.w3c.dom.DOMException e)
java.lang.String
unescape(java.lang.String s, boolean unescapeDoubleQuotes)
Unescapes escaped characters in the specified string, according to the CSS specification.
-
-
-
Field Detail
-
documentHandler_
private DocumentHandler documentHandler_
-
errorHandler_
private CSSErrorHandler errorHandler_
-
source_
private InputSource source_
-
parserMessages_
private static final java.util.HashMap<java.lang.String,java.lang.String> parserMessages_
-
NUM_CHARS
private static final java.lang.String NUM_CHARS
- See Also:
- Constant Field Values
-
-
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 keye
- 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 keymsgParams
- the paramslocator
- the locator- Returns:
- a new CSSParseException
-
createSkipWarning
protected CSSParseException createSkipWarning(java.lang.String messageKey, CSSParseException e)
- Parameters:
messageKey
- the message keye
- 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
-
styleSheet
protected abstract void styleSheet() throws CSSParseException, ParseException
Process a style sheet.- Throws:
CSSParseException
- in case of errorParseException
- 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(java.lang.String s, Locator locator)
ignorable at rule handler.- Parameters:
s
- the rulelocator
- the locator
-
handleCharset
protected void handleCharset(java.lang.String characterEncoding, Locator locator)
charset handler.- Parameters:
characterEncoding
- the encodinglocator
- the locator
-
handleImportStyle
protected void handleImportStyle(java.lang.String uri, MediaQueryList media, java.lang.String defaultNamespaceURI, Locator locator)
import style handler.- Parameters:
uri
- the urimedia
- the media query listdefaultNamespaceURI
- the namespace urilocator
- the locator
-
handleStartMedia
protected void handleStartMedia(MediaQueryList media, Locator locator)
start media handler.- Parameters:
media
- the media query listlocator
- 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 namepseudoPage
- the pseudo pagelocator
- the locator
-
handleEndPage
protected void handleEndPage(java.lang.String name, java.lang.String pseudoPage)
end page handler.- Parameters:
name
- the namepseudoPage
- 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 listlocator
- 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 namevalue
- the valueimportant
- important flaglocator
- the locator
-
functionInternal
protected LexicalUnit functionInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit params)
Process a function decl.- Parameters:
prev
- the previous lexical unitfunct
- the functionparams
- the params- Returns:
- a lexical unit
-
rgbColorInternal
protected LexicalUnit rgbColorInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit param)
-
hslColorInternal
protected LexicalUnit hslColorInternal(LexicalUnit prev, java.lang.String funct, LexicalUnit param)
-
hexColorInternal
protected LexicalUnit hexColorInternal(LexicalUnit prev, Token t)
Processes a hexadecimal color definition.- Parameters:
prev
- the previous lexical unitt
- 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 chars
- 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 chars
- 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 unescapeunescapeDoubleQuotes
- 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)
-
-