0

I have written a program which scans css files using jar cssparser-0.9.5.jar and i performed some operation on it

public static Map<String, CSSStyleRule> parseCSS(String FileName) throws IOException {
        Map<String, CSSStyleRule> rules = new LinkedHashMap<String, CSSStyleRule>();
        InputSource inputSource = new InputSource(
                new FileReader(FileName));
        CSSStyleSheet styleSheet = new CSSOMParser().parseStyleSheet(
                inputSource, null, null);

        CSSRuleList ruleList = styleSheet.getCssRules();
        for (int i = 0; i < ruleList.getLength(); i++) {
            CSSRule rule = ruleList.item(i);
            if (rule.getType() == CSSRule.STYLE_RULE) {
                CSSStyleRule styleRule = (CSSStyleRule) rule;
                rules.put(styleRule.getSelectorText(), styleRule);
            }
        }

        return rules;
    }

this code works fine for all classes except for class which contain properties which start with '-' like

.overlay
{

    filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');

}

after parsing it give error for double ':' present in class .overlay's properties

so is there any idea to solve this problem?

2

1 Answer 1

3

The code you posted is a few levels higher than where the actual problem is. The problem is in the lexical scanner. Its definition of what an identifier (IDENT) is seems to be wrong, as it can also contain hyphens and start with hyphens.

As the CSS3 syntax specification says:

In CSS3, identifiers (including element names, classes, and IDs in selectors (see [SELECT[or is this still true])) can contain only the characters [A-Za-z0-9] and ISO 10646 characters 161 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit or a hyphen followed by a digit.

See the full specification here.

Sign up to request clarification or add additional context in comments.

8 Comments

ok thanks for your help but how to deal with this should i need to change my whole code?
Changing this shouldn't affect a lot of your code. An adjustment has to be made in the lexer, which scans the input and breaks it into tokens for the parser. CSS properties (i.e. background-color) will be recognized by the lexer as identifiers. You have to adjust a bit of code in the lexer to make sure that identifiers can also start with a hyphen (-).
actualy i am using cssparser0.9.5 jar
It might be using a parser for a version of CSS older than 2.1, since for those versions, CSS properties couldn't start with a hyphen. When you create the parser, you can specify which underlying parser it should use. Try this: new CSSOMParser(new com.steadystate.css.parser.SACParserCSS21()) and see if it works then, otherwise use the latest version of CSS Parser, which is 0.9.7.
hey it solves my 99% problem now just one problem for filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.