Menu

Need some examples about CSS Parser

Help
QuangHoang
2009-11-06
2012-07-18
  • QuangHoang

    QuangHoang - 2009-11-06

    I'm try to use this library but I don't know how to start, can sombody help me
    ?

     
  • Anonymous

    Anonymous - 2009-11-15

    Hi

    I too looked in vain for a basic usage example. Finding none, I poked around
    the code. I wrote the following JUnit test. It reads and parses a simple css
    file and then walks through the CSSStyleSheet object that the parse operation
    has created. So, this exercises the CSS DOM api. I have not yet tried to
    exercise the SAC api.

    Hope it helps you.

    JSandoe

    package com.steadystate.css;

    import com.steadystate.css.parser.CSSOMParser;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import junit.framework.Test;

    import junit.framework.TestCase;

    import junit.framework.TestSuite;

    import org.w3c.css.sac.InputSource;

    import org.w3c.dom.css.CSSStyleSheet;

    import org.w3c.dom.css.CSSRuleList;

    import org.w3c.dom.css.CSSRule;

    import org.w3c.dom.css.CSSStyleRule;

    import org.w3c.dom.css.CSSStyleDeclaration;

    import org.apache.log4j.Logger;

    /**

    • Unit test for simple App.
      */
      public class BasicTest

    extends TestCase

    {

    private static final Logger logger = Logger.getLogger(BasicTest.class);

    public void testParse() throws Exception

    {

    // get css file. In this case it is accessed as a resource.

    InputStream stream = this.getClass().getResourceAsStream("/simple.css");

    assertNotNull(stream);

    // wrap as an InputSource

    InputSource source = new InputSource(new InputStreamReader(stream));

    // instantiate a parser

    CSSOMParser parser = new CSSOMParser();

    // parse and create a stylesheet

    CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);

    // now we can open up the dom and look at what is inside. Naturally, this is
    first a list of rules.

    CSSRuleList ruleList = stylesheet.getCssRules();

    logger.info("Number of rules: " + ruleList.getLength());

    // in our case there is just one rule.

    assertEquals(1, 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;

    logger.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);

    logger.info("property: " + property);

    logger.info("value: " +
    styleDeclaration.getPropertyCSSValue(property).getCssText());

    }

    }

    }

    }

    }

    Here is the (very simple) css file (named simple.css) I used for my test case:

    p { color:red; }

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.