Skip to content

Instantly share code, notes, and snippets.

@icasdri
Created August 1, 2016 04:42
Show Gist options
  • Select an option

  • Save icasdri/3bbba90ea9c57b8426f7483fcbba95da to your computer and use it in GitHub Desktop.

Select an option

Save icasdri/3bbba90ea9c57b8426f7483fcbba95da to your computer and use it in GitHub Desktop.
Java CSS Parser Test
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleSheet;
import java.io.IOException;
import java.io.StringReader;
public class CssTest {
public static void main(String[] args) throws IOException {
String css = "h1 { background: white; margin: 12px; }";
InputSource source = new InputSource(new StringReader(css));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);
CSSRuleList rules = sheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
final CSSRule rule = rules.item(i);
System.out.println(rule.getType());
System.out.println(rule.getCssText());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>css-test</groupId>
<artifactId>css-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.w3c.css</groupId>
<artifactId>sac</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cssparser</groupId>
<artifactId>cssparser</artifactId>
<version>0.9.20</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment