Example usage for org.w3c.dom.css CSSStyleRule getSelectorText

List of usage examples for org.w3c.dom.css CSSStyleRule getSelectorText

Introduction

In this page you can find the example usage for org.w3c.dom.css CSSStyleRule getSelectorText.

Prototype

public String getSelectorText();

Source Link

Document

The textual representation of the selector for the rule set.

Usage

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 {/*w  w w  .  j  ava 2  s .  c  om*/
        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:net.sf.nmedit.jtheme.store.StorageContext.java

public CSSStyleRule getStyleRule(String name) {
    CSSStyleRule rule = styleRuleMap.get(name);
    if (rule != null)
        return FakeRule.isFake(rule) ? null : rule;

    CSSStyleSheet css = getStyleSheet();
    CSSRuleList list = css.getCssRules();

    rule = null;/*from  w  ww.  j av a 2 s  .c o  m*/

    for (int i = 0; i < list.getLength(); i++) {
        CSSRule arule = list.item(i);

        if (arule.getType() == CSSRule.STYLE_RULE) {
            CSSStyleRule srule = (CSSStyleRule) arule;

            if (name.equals(srule.getSelectorText())) {
                // we found the rule
                rule = srule;
                break;
            }
        }
    }

    if (rule == null) {
        styleRuleMap.put(name, FakeRule.instance());
        return null;
    }

    styleRuleMap.put(name, rule);
    return rule;

}

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   ww w.  j ava  2 s  .  c om*/
            } 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;
}