List of usage examples for org.w3c.dom.css CSSStyleRule getStyle
public CSSStyleDeclaration getStyle();
From source file:com.pagecrumb.proxy.util.filter.CssProxyTransformParser.java
@Override public void parse(String document) { log.info("Parsing CSS: " + document); this.document = document; InputSource source = new InputSource(new StringReader(this.document)); try {/*from w w w .j a v a 2s . co m*/ CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null); CSSRuleList ruleList = stylesheet.getCssRules(); log.info("Number of rules: " + ruleList.getLength()); // lets examine the stylesheet contents for (int i = 0; i < ruleList.getLength(); i++) { CSSRule rule = ruleList.item(i); if (rule instanceof CSSStyleRule) { CSSStyleRule styleRule = (CSSStyleRule) rule; log.info("selector: " + styleRule.getSelectorText()); CSSStyleDeclaration styleDeclaration = styleRule.getStyle(); //assertEquals(1, styleDeclaration.getLength()); for (int j = 0; j < styleDeclaration.getLength(); j++) { String property = styleDeclaration.item(j); log.info("property: " + property); log.info("value: " + styleDeclaration.getPropertyCSSValue(property).getCssText()); } } } } catch (IOException e) { e.printStackTrace(); } }
From source file:org.castafiore.ecm.Main.java
public static String Parse(File css, String name) throws Exception { String result = ""; InputStream stream = new FileInputStream(css); InputSource source = new InputSource(new InputStreamReader(stream)); CSSOMParser parser = new CSSOMParser(); CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null); CSSRuleList ruleList = stylesheet.getCssRules(); for (int i = 0; i < ruleList.getLength(); i++) { CSSRule rule = ruleList.item(i); if (rule instanceof CSSStyleRule) { CSSStyleRule styleRule = (CSSStyleRule) rule; String selector = styleRule.getSelectorText().replace("*", "").replace("html", "").trim(); StringBuilder b = new StringBuilder(); if (selector.startsWith("body")) { selector = "." + name; b.append(selector);//from w ww . j a v a 2 s . co m } else { String[] asSelector = StringUtils.split(selector, ","); for (String s : asSelector) { if (b.length() > 0) { b.append(" , "); } // if(s.trim().startsWith("a:")){ // // } s = s.replace("#", ".").trim(); s = "." + name + " " + s; b.append(s); } } System.out.println(b.toString() + "{"); result = result + b.toString() + "{\n"; CSSStyleDeclaration styleDeclaration = styleRule.getStyle(); for (int j = 0; j < styleDeclaration.getLength(); j++) { String property = styleDeclaration.item(j); String value = styleDeclaration.getPropertyValue(property); System.out.println("\t" + property + ":" + value); result = result + "\t" + property + ":" + value + ";\n"; } result = result + "}\n"; System.out.println("}"); } } if (stream != null) stream.close(); return result; }