A simple CSS parser in Java.
An oh, it is also very strict. If it finds an unexpected character in your CSS it self-destructs, taking you with you - making it a great tool to validate strict CSS.
As String directly:
List<Rule> rules = CSSParser.parse("div { width: 100px; -mozilla-opacity: 345; }");
Or from a file (using Apache Commons IO):
String contents = IOUtils.toString(this.getClass().getResourceAsStream("stylesheet.css"));
List<Rule> rules = CSSParser.parse(contents);
To print it all out:
for (Rule rule : rules) {
System.out.println(rule);
}
Or go into details:
for (Rule rule : rules) {
// Get all the selectors (such as 'table', 'table td', 'a')
List<Selector> selectors = rule.getSelectors();
// Get all the property (such as 'width') and its value (such as '100px')
List<PropertyValue> propertyValues = rule.getPropertyValues();
}
Please view the JavaDoc for more informaton.
Download the latest version here:
https://github.com/corgrath/osbcp-css-parser/downloads/
http://dl.dropbox.com/u/8183146/persistent/projects/java_osbcp_css_parser/javadoc/index.html
mvn release:prepare release:perform -Prelease -DskipTests=true -Darguments="-DskipTests=true -Prelease"
Please create an issue here:
https://github.com/corgrath/osbcp-css-parser/issues
Version 1.2
Code is also checked with PMD and Checkstyle.
v1.5 2012-04-28 Christoffer Pettersson
- The parser can now handle nested comments. Thanks to Justin Marsan (HammHetfield) for reporting the bug.
v1.4 2012-03-11 Christoffer Pettersson
- The parser now gives an error if it founds another colon : while reading the value.
v1.3 2012-02-08 Christoffer Pettersson
- It is now possible to have duplicate property names in a rule
v1.2 2012-06-07 Christoffer Pettersson
- Added IncorrectFormatException.ErrorCode for more specific exceptions
- Added support for values with data URIs
v1.1 2012-02-06 Christoffer Pettersson
- While inside the INSIDE_PROPERTY_NAME and a colon : is read, a IncorrectFormatException is thrown
v1.0 2012-02-05 Christoffer Pettersson
- Initial release
OSBCP CSS Parser Copyright 2012 Christoffer Pettersson, christoffer[at]christoffer.me.
Licensed under the Apache License, Version 2.0