2

Is there anything like Jsoup but instead of parsing HTML, I need to parse CSS, is there a parser similar to that that will create a Document model of a CSS? That perhaps can be used to iterate to nodes?

What I need to do is to find and replace "url" in the CSS file. And with html this is quite easy with Jsoup. However I am not sure if this will be possible with CSS.

If there are no such parser exist what are the options?

quarks
  • 33,478
  • 73
  • 290
  • 513

4 Answers4

4

Perhaps this might help you?

Taymon
  • 24,950
  • 9
  • 62
  • 84
  • I'm getting java.lang.NoClassDefFoundError: org/w3c/css/sac/DocumentHandler when using this CSS parser, do I need to include additional jar apart from the cssparser-0.9.6.jar? – quarks Apr 02 '12 at 17:58
  • You might need [SAC](http://www.w3.org/Style/CSS/SAC/). – Taymon Apr 02 '12 at 21:55
3

I just rolled out my own CSS Stream Parser for Java, available on github. What sets this parser apart includes:

  • It is a stream parser, so the parser handler will receive notification of all new content immediately after each item has been parsed
  • Full support for all currently-documented At-Rules
  • Custom classes TokenSequence and Token simplify processes for handling selectors, etc.
  • Easy to use and to understand
  • Useful for validation or for more advanced applications
  • Scalable: designed to be able to handle changes to CSS definitions.

For your problem, just use the basic parsing technique:

try
{
    //First get the input stream. For example, from a file
    InputStream is = new FileInputStream(new File("/path/to/my/CSS/main.css"));

    //Then parse        
    CSSParser parser = new CSSParser(is, new DefaultCSSHandler());
    parser.parse();
}
catch (Throwable t)
{
    t.printStackTrace();
}

but override some methods in DefaultCSSHandler to look for the identifier you need.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • cool, are you using this for one of your projects? – quarks Jan 07 '14 at 20:40
  • How does it compare to: http://cssparser.sourceforge.net/ – quarks Jan 07 '14 at 20:41
  • @xybrek yes, I am currently using this parser to develop *CSS* support for [droidQuery](http://bit.ly/droidquery). I stumbled upon the linked CSS parser before, and found it confusing. It also uses a lot of libraries that if I remember are not easy to find. My Parser __does not use SAC__, it is standalone, so once you download it, it is easy to run. Furthermore, I made sure to document well and, because it is intended to be used for more than a validator, it is should be easy to understand how to handle parsed data in a similar way as xml parsing. – Phil Jan 07 '14 at 20:57
  • I see, I just pulled jCSS-parser and did some changes to add POM on it. – quarks Jan 07 '14 at 21:05
2

A CSS library for reading and writing CSS2 and CSS3 files in Java is ph-css from https://github.com/phax/ph-css It is based on a JavaCC grammar and supports both CSS2 as well as CSS3 and additionally lets you parse HTML style attributes.

It supports the most common hacks "*", "_" and "$" which are not spec compliant
It supports CSS math - the calc() expression
It supports the @page rule
It supports the CSS3 media queries
It supports @viewport rules
It supports @keyframes rules
It supports @supports rules - quite new
It supports the @namespace rules
You can get source location information for the different elements (line + column number for start and end - both for the tag as well as for the complete construct)
Philip Helger
  • 1,814
  • 18
  • 28
0

this will help you

http://cssparser.sourceforge.net/

also

http://sourceforge.net/projects/cssparser/

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77