0

I'm trying to use the jStyleParser library ( part of the CSSBox project) to analyze the css of a generic web page and bind all the css information to the DOM of that page. My goal is, given a web page, to have a DOM tree such that, for each node, I can get all the css informations realted to tha node.

This is (part of) my code:

System.out.println("Analizing "+Mylocalfile.getAbsolutePath()); 
StyleSheet ss = CSSFactory.parse(Mylocalfile.getAbsolutePath(), "UTF-8");
if (ss.isEmpty()) System.out.println("StyleSheet is void");    

The parse method seem to be unable to parse the file: the StyleSheet ss is infact void.

Do you know why?

radkovo
  • 868
  • 6
  • 10

1 Answers1

1

Your code is correct as long as MyLocalFile points to a CSS style sheet (*.css). In that case, you should obtain the parsed style sheet in ss but it is not bound to any DOM. If you still obtain an empty style sheet, there might be a bug in the CSS code or even in the parser. In that case, I recommend to report this to the jStyleParser forum at SourceForge.

If you want to assign the style definitions to a DOM (MyLocalFile points to an HTML file), you should use the assignDOM method as described in the jStyleParser manual. In that case, you should parse the HTML document with a DOM parser and use the assignDOM method to automatically retrieve the referenced style sheets and compute the element styles. You may find an example in src/test/DOMAssign.java in the jStyleParser source package.

radkovo
  • 868
  • 6
  • 10
  • Thank you Radkovo, few ours ago I wrote on jStyleParser's forum about the ambiguos documentation: just after sever testing I understood that Mylocalfile should be an Css file and not an htmlf file, and that was my duty to extract all the ccs urls from the tags inside the html code. – Antonio Giovanni Schiavone Aug 10 '12 at 17:03
  • Now I'm facing new issuses: inside the assignDOM method is not clear what the "media" parameter rapresents. And after that, if I have an org.w3c.dom Node (part of the assigned DOM), how i can get its style definitions? – Antonio Giovanni Schiavone Aug 10 '12 at 17:12