Example usage for org.w3c.dom.css CSSStyleDeclaration getPropertyCSSValue

List of usage examples for org.w3c.dom.css CSSStyleDeclaration getPropertyCSSValue

Introduction

In this page you can find the example usage for org.w3c.dom.css CSSStyleDeclaration getPropertyCSSValue.

Prototype

public CSSValue getPropertyCSSValue(String propertyName);

Source Link

Document

Used to retrieve the object representation of the value of a CSS property if it has been explicitly set within this declaration block.

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 {/*from   w w w.  j a v  a2  s.c o  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:com.edgenius.wiki.ext.textnut.NutParser.java

/**
 * Remove tags outside body./* ww w.  j  av  a 2 s.  co  m*/
 * Apply HTML CSS into tag style.
 * Remove default attribute in tag.
 * 
 * @param htmlContent
 * @return
 */
public String convertNutHTMLToPageHTML(String htmlContent) {

    //Parse whole HTML, and separator out CSS and tags list inside body.
    HtmlParser htmlParser = new HtmlParser();
    HtmlNodeListenerImpl listener = new HtmlNodeListenerImpl();
    htmlParser.scan(htmlContent, listener);

    StringBuffer css = new StringBuffer();
    HTMLNodeContainer body = new HTMLNodeContainer();

    //First, exact CSS style part, then remove all element outside body tag
    int require = 0; //1=css;2=body
    HTMLNode endNode = null;
    for (HTMLNode node : listener.getHtmlNode()) {

        if (!node.isTextNode() && "style".equals(node.getTagName()) && !node.isCloseTag()) {
            endNode = node.getPair();
            require = 1;
            continue;
        }
        if (!node.isTextNode() && "body".equals(node.getTagName()) && !node.isCloseTag()) {
            endNode = node.getPair();
            require = 2;
            continue;
        }
        if (endNode != null && require == 1) {
            if (endNode == node) {
                endNode = null;
            } else {
                if (node.isTextNode())
                    css.append(node.getText());
            }

            continue;
        }
        if (endNode != null && require == 2) {
            if (endNode == node) {
                break;
            } else {
                body.add(node);
            }
        }
    }

    // parse and create a stylesheet composition
    CSSOMParser parser = new CSSOMParser();
    //I don't know how to use CSSParser API to quick located style by selector, so blow map will be used for this purpose
    //its key is selector text, value is style list separated by ";" which is able to use in tag "class" attribute.
    Map<String, String> selectors = new HashMap<String, String>();
    try {
        CSSStyleSheet stylesheet = parser
                .parseStyleSheet(new InputSource(new StringReader(css.toString().trim())), null, null);
        CSSRuleList ruleList = stylesheet.getCssRules();
        for (int idx = 0; idx < ruleList.getLength(); idx++) {
            CSSRule rule = ruleList.item(idx);
            if (rule instanceof CSSStyleRule) {
                CSSStyleDeclaration style = ((CSSStyleRule) rule).getStyle();
                StringBuffer buf = new StringBuffer();
                int len = style.getLength();
                for (int idj = 0; idj < len; idj++) {
                    String name = style.item(idj);
                    String value = style.getPropertyCSSValue(name).getCssText();
                    if ("margin".equals(name) && value.equals("0px 0px 0px 0px")) {
                        continue;
                    } else if ("font".equals(name) && value.equals("18px Helvetica")) {
                        //This value must coordinate with TextNut default font setting.
                        continue;
                    }

                    buf.append(name).append(":").append(value).append(";");
                }

                //buf could be blank, even so, we still need put it into map so that class attribute can locate style by name
                selectors.put(((CSSStyleRule) rule).getSelectorText(), buf.toString());

            }
        }

        //debug use: only for display:
        //          for (int idx = 0; idx < ruleList.getLength(); idx++) {
        //              CSSRule rule = ruleList.item(idx);
        //              if (rule instanceof CSSStyleRule){ 
        //                  CSSStyleRule styleRule=(CSSStyleRule)rule;
        //                  CSSStyleDeclaration style = styleRule.getStyle();
        //                  System.out.println(styleRule.getSelectorText());
        //                  for (int idj = 0; idj < style.getLength(); idj++){
        //                       String name = style.item(idj);
        //                       System.out.println(name +"=" + style.getPropertyCSSValue(name).getCssText());
        //                  }
        //              }
        //            }

        //apply CSS to body tags
        HTMLNode node;
        for (Iterator<HTMLNode> iter = body.iterator(); iter.hasNext();) {
            node = iter.next();
            if (node.isTextNode())
                continue;

            if (node.getAttributes() != null && !StringUtils.isBlank(node.getAttributes().get("class"))) {
                //we only support "tagName.styleName" format selector
                String style = selectors.get(node.getTagName() + "." + node.getAttributes().get("class"));
                if (style != null) {
                    if (!"".equals(style)) {
                        //could be blank, if style all removed because they are default value.
                        node.setAttribute("style", style);
                    }
                    node.removeAttribute("class");
                }
            }
            //embedded files object - <object data="file:///index_1.html">index_1.html</object>
            if ("object".equals(node.getTagName()) && node.getAttributes() != null
                    && StringUtils.startsWith(node.getAttributes().get("data"), "file://")) {

            }
        }

        return body.toString().trim();
    } catch (IOException e) {
        log.error("Parse CSS failed", e);
    }
    return htmlContent;
}