Class CssParser

java.lang.Object
org.jhotdraw8.css.parser.CssParser

public class CssParser extends Object
The CssParser processes a stream of characters into a Stylesheet object.

The CSS Syntax Module Level 3 defines a grammar which is equivalent to the following EBNF ISO/IEC 14977 productions:

 stylesheet_core = { S | CDO | CDC | qualified_rule | at_rule } ;

 rule_list    = { S | qualified_rule | at_rule} ;

 at_rule      = AT_KEYWORD , { component_value } , ( curly_block | ';' ) ;

 qualified_rule
              = { component_value } , curly_block ;

 declaration_list_core
              = { S } , ( [ declaration_core ] , [ ';' , declaration_list_core ]
                        | at_rule , declaration_list_core ,
                        ) ;

 declaration_core  = IDENT , { S } ,  ":", { component_value } , [ !important ] ;

 !important   = '!' , { S } , "important" , { S } ;

 component_value
              = ( preserved_token | curly_block | round_block | square_block
                | function_block ) ;

 curly_block  = '{' , { component_value } , '}' ;
 round_block  = '(' , { component_value } , ')' ;
 square_block = '[' , { component_value } , ']' ;
 function_block
              = ROUND_BLOCK , { component_value } , ')' ;

 
This parser parses the following syntax:
 stylesheet   = { S | CDO | CDC | qualified_rule | style_rule } ;

 operator     = ( '/' | ',' ) , { S } ;

 combinator   = ( '+' | '>' | '~' ) , { S } ;

 unary_operator
              = ( '-' | '+' ) ;

 property     = IDENT , { S } ;

 style_rule   = [ selector_group ] , "{" , declaration_list , "}" ;

 selector_group
              = selector , { "," , { S }, selector } ;

 selector     = simple_selector ,
                { ( combinator , selector
                  | { S }, [ [ combinator ] , selector ]
                  )
                } ;

 simple_selector
              = universal_selector | type_selector | id_selector
                | class_selector | pseudoclass_selector | attribute_selector ;
 universal_selector   = '*' ;
 type_selector        = ns_aware_ident ;
 id_selector          = HASH ;
 class_selector       = "." , IDENT ;
 pseudoclass_selector = ":" , IDENT ;
 attribute_selector   = "[" , ns_aware_ident
                            , [ ( "=" | "~=" | "|=" ) , ( IDENT | STRING ) ],
                        "]" ;
 ns_aware_ident      = IDENT
                      | '*' , '|', IDENT
                      | IDENT , '|', IDENT
                      ;

 declaration_list
              = { S } , [ declaration ] , [ ';' , declaration_list ] ;

 declaration  = IDENT , { S } ,  ":", { terms } ;

 terms        = { { S } , ( term | bracketedTerms ) } ;

 term         = any token - ( "]" | "}" | ";" | S ) ;

 bracketedTerms = "{", { { S } , term } , { S } , "}"
                | "[", { { S } , term } , { S } , "]";


 function     = ROUND_BLOCK , { S } , expr , ')' , { S } ;
 expr         = term , { [ operator ] , term } ;
 
The parser interprets the following at rules:
 namespace_rule = "@namespace" , [ namespace_prefix ] , ( STRING | URI ) ;
 namespace_prefix = IDENT ;
 

References:

CSS Syntax Module Level 3, Paragraph 5. Parsing
w3.org
CSS Namespaces Module Level 3, Paragraph 2 Declaring namespaces: the @namespace rulex
w3.org
W3C CSS2.2, Appendix G.1 Grammar of CSS 2.2
w3.org

FIXME The parser does not support the !important declaration.

Author:
Werner Randelshofer
  • Field Details

  • Constructor Details

    • CssParser

      public CssParser()
  • Method Details

    • getParseExceptions

      public List<ParseException> getParseExceptions()
    • parseDeclarationList

      public List<Declaration> parseDeclarationList(String css) throws IOException
      Parses a declaration list.
      Parameters:
      css - A stylesheet
      Returns:
      the declaration list
      Throws:
      IOException - if parsing fails
    • parseDeclarationList

      public List<Declaration> parseDeclarationList(Reader css) throws IOException
      Parses a declaration list.
      Parameters:
      css - A stylesheet
      Returns:
      the declaration list
      Throws:
      IOException - if parsing fails
    • parseSelectorGroup

      public SelectorGroup parseSelectorGroup(CssTokenizer tt) throws IOException, ParseException
      Throws:
      IOException
      ParseException
    • parseStylesheet

      public Stylesheet parseStylesheet(URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException
      Parses a given stylesheet from the specified URI.
      Parameters:
      stylesheetUri - the URI of the stylesheet (must be known)
      stylesheetHome - base URI (if it exists)
      Returns:
      the parsed stylesheet
      Throws:
      IOException - on failure
    • parseStylesheet

      public Stylesheet parseStylesheet(String css, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException
      Parses a given stylesheet from the specified String and document home.
      Parameters:
      css - the uri of the stylesheet file
      stylesheetUri - the URI of the stylesheet (if known)
      stylesheetHome - base URI (if it exists)
      Returns:
      the parsed stylesheet
      Throws:
      IOException - on failure
    • parseSelector

      public Selector parseSelector(String css) throws ParseException
      Parses a given selector from the specified String and document home.
      Parameters:
      css - a literal selector String
      Returns:
      the parsed selector
      Throws:
      ParseException - on failure
    • parseStylesheet

      public Stylesheet parseStylesheet(Reader css, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException
      Parses a given stylesheet from the specified String and document home.
      Parameters:
      css - the uri of the stylesheet file
      stylesheetUri - the URI of the stylesheet (if known)
      stylesheetHome - base URI (if it exists)
      Returns:
      the parsed stylesheet
      Throws:
      IOException - on failure
    • parseStylesheet

      public Stylesheet parseStylesheet(CssTokenizer tt, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException
      Parses a given stylesheet from the specified String and document home.
      Parameters:
      tt - the tokenier
      stylesheetUri - the URI of the stylesheet (if known)
      stylesheetHome - base URI (if it exists)
      Returns:
      the parsed stylesheet
      Throws:
      IOException - on failure
    • getStylesheetUri

      public @Nullable URI getStylesheetUri()
    • setStylesheetUri

      public void setStylesheetUri(@Nullable URI stylesheetUri)
    • getStylesheetHome

      public @Nullable URI getStylesheetHome()
    • setStylesheetHome

      public void setStylesheetHome(@Nullable URI stylesheetHome)
    • getUriResolver

      public UriResolver getUriResolver()
    • setUriResolver

      public void setUriResolver(UriResolver uriResolver)
    • isStrict

      public boolean isStrict()
      Return strict parsing mode.
      Returns:
      true if the parser is in strict mode.
    • setStrict

      public void setStrict(boolean strict)
      Sets strict parsing mode.
      Parameters:
      strict - true to parse in strict mode, the default value is false.