Built on top of the DOM4J package, gives DOM4J built-in support for CSS style sheets. You can use the XHTMLDocumentFactory as you would use a DOMDocumentFactory, and the produced documents (and elements) will be style-aware.

To use the library with dom4j (using dom4j-like documents, elements and factory) you need the css4j-dom4j module in addition to the core module. The -dom4j module optionally depends on the -agent module, you do not need it unless you plan to use the DOM4J agent.

Example with DOM4J

This is the easiest way to use this package with DOM4J, using that library's SAXReader:

Reader re = ... [reader for XHTML document]
InputSource source = new InputSource(re);
SAXReader reader = new SAXReader(XHTMLDocumentFactory.getInstance());
reader.setEntityResolver(new DefaultEntityResolver());
XHTMLDocument document = (XHTMLDocument) reader.read(source);

And once you got the element you want the computed style for (see, for example, the DOM4J Quick Start Guide), just get it with a procedure analogous to the ViewCSS interface:

CSSComputedProperties style = ((CSSStylableElement) element).getComputedStyle(null);
String display = style.getPropertyValue("display");

It is also possible to parse an HTML5 document into a css4j-dom4j tree with the validator.nu HTML5 parser:

XHTMLDocumentFactory factory = XHTMLDocumentFactory.getInstance();
// Next line is optional: default is TRUE, and is probably what you want
// factory.setLenientSystemValues(false);
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(factory);
// We do not set the EntityResolver, the HtmlDocumentBuilder does not need it
Reader re = ... [reader for HTML document]
InputSource source = new InputSource(re);
XHTMLDocument document = (XHTMLDocument) builder.parse(source);

Or use a SAX parser to parse an XML document into a css4j-dom4j tree, with XMLDocumentBuilder instead of dom4j's SAXReader:

XHTMLDocumentFactory factory = XHTMLDocumentFactory.getInstance();
// Next line is optional: default is TRUE, and is probably what you want
// factory.setLenientSystemValues(false);
XMLDocumentBuilder builder = new XMLDocumentBuilder(factory);
builder.setEntityResolver(new DefaultEntityResolver());
Reader re = ... [reader for XML document]
InputSource source = new InputSource(re);
XHTMLDocument document = (XHTMLDocument) builder.parse(source);
re.close();

The code above uses the default JAXP SAX parser. You could use a different SAXParserFactory:

SAXParserFactory parserFactory = ...
XMLDocumentBuilder builder = new XMLDocumentBuilder(factory, parserFactory);

Setting the User Agent sheet

To set the user agent sheet, first obtain an instance of the factory that you are using:

XHTMLDocumentFactory docFactory = XHTMLDocumentFactory.getInstance();

If css4j's default HTML5 UA sheet is appropriate for you, just do:

docFactory.getStyleSheetFactory().setDefaultHTMLUserAgentSheet();

But if you want to set your own UA sheet, first obtain a reference to it:

BaseCSSStyleSheet sheet = docFactory.getStyleSheetFactory().getUserAgentStyleSheet(CSSDocument.ComplianceMode.STRICT);

This is assuming the STRICT mode, i.e. that you use a DOCTYPE, otherwise use QUIRKS.

If the UA sheet already contains rules (it is empty by default), clean it:

sheet.getCssRules().clear();

And now load the new sheet:

sheet.parseStyleSheet(reader, CSSStyleSheet.COMMENTS_IGNORE);