jStyleParser

jStyleParser is a CSS parser written in Java. It has its own application interface that is designed to allow an efficient CSS processing in Java and mapping the values to the Java data types. It parses CSS 2.1 style sheets into structures that can be efficiently assigned to DOM elements. It is intended be the primary CSS parser for the CSSBox library. While handling errors, it is user agent conforming according to the CSS specification.

License

License

Categories

Categories

Net
GroupId

GroupId

net.sf.cssbox
ArtifactId

ArtifactId

jstyleparser
Last Version

Last Version

4.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

jStyleParser
jStyleParser is a CSS parser written in Java. It has its own application interface that is designed to allow an efficient CSS processing in Java and mapping the values to the Java data types. It parses CSS 2.1 style sheets into structures that can be efficiently assigned to DOM elements. It is intended be the primary CSS parser for the CSSBox library. While handling errors, it is user agent conforming according to the CSS specification.
Project URL

Project URL

http://cssbox.sourceforge.net/jstyleparser
Source Code Management

Source Code Management

https://github.com/radkovo/jStyleParser

Download jstyleparser

How to add to project

<!-- https://jarcasting.com/artifacts/net.sf.cssbox/jstyleparser/ -->
<dependency>
    <groupId>net.sf.cssbox</groupId>
    <artifactId>jstyleparser</artifactId>
    <version>4.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.sf.cssbox/jstyleparser/
implementation 'net.sf.cssbox:jstyleparser:4.0.0'
// https://jarcasting.com/artifacts/net.sf.cssbox/jstyleparser/
implementation ("net.sf.cssbox:jstyleparser:4.0.0")
'net.sf.cssbox:jstyleparser:jar:4.0.0'
<dependency org="net.sf.cssbox" name="jstyleparser" rev="4.0.0">
  <artifact name="jstyleparser" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.sf.cssbox', module='jstyleparser', version='4.0.0')
)
libraryDependencies += "net.sf.cssbox" % "jstyleparser" % "4.0.0"
[net.sf.cssbox/jstyleparser "4.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.antlr : antlr4-runtime jar 4.5.3
org.slf4j : slf4j-api jar 1.7.25
org.unbescape : unbescape jar 1.1.6.RELEASE

test (4)

Group / Artifact Type Version
net.sourceforge.nekohtml : nekohtml jar 1.9.22
xerces : xercesImpl jar 2.12.0
xml-apis : xml-apis jar 1.4.01
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

jStyleParser

Build Status Maven Central

jStyleParser is a Java library for parsing CSS style sheets and assigning styles to the HTML or XML document elements according to the CSS 3 specifications. It allows parsing the individual CSS files as well as computing the efficient style of the DOM elements.

See the project page for more information: http://cssbox.sourceforge.net/jstyleparser

Installation

With Maven, use the following dependency:

<dependency>
    <groupId>net.sf.cssbox</groupId>
    <artifactId>jstyleparser</artifactId>
    <version>4.0.0</version>
</dependency>

Parsing CSS

The basic CSSFactory interface provides functions parsing CSS strings, files or URLs. The parsed style sheet is represented by the corresponding data structures that allow accessing all parts of the style sheet in a type-safe way.

String css = "div { background-color: red; width: 12px; }";

//parse the style sheet
StyleSheet sheet = CSSFactory.parseString(css, new URL("http://base.url"));

//access the rules and declarations
RuleSet rule = (RuleSet) sheet.get(0);       //get the first rule
CombinedSelector selector = rule.getSelectors()[0]; //read the 'div' selector
Declaration bgDecl = rule.get(0);            //read the 'background-color' declaration
String bgProperty = bgDecl.getProperty();    //read the property name
TermColor color = (TermColor) bgDecl.get(0); //read the color

//print the results
System.out.println(selector);   //prints 'div'
System.out.println(bgProperty); //prints 'background-color'
System.out.println(color);      //prints '#ff0000'

//or even print the entire style sheet (formatted)
System.out.println(sheet);

See the details in the documentation.

Computing style for DOM elements

jStyleParser allows to map the style rules to the individual elements in a DOM tree based on their selectors. This allows obtaining the exact style for any HTML element.

org.w3c.dom.Document doc = ... //source DOM

MediaSpec media = new MediaSpecAll(); //use styles for all media

//create the style map
StyleMap map = CSSFactory.assignDOM(doc, "UTF-8", new URL("https://base.url/"), media, true);

//get the style of a single element
Element div = doc.getElementById("searchelement"); //choose a DOM element
NodeData style = map.get(div); //get the style map for the element
//get the type of the assigned value
CSSProperty.Margin mm = style.getProperty("margin-top");
System.out.println("margin-top=" + mm);
//if a length is specified, obtain the exact value
if (mm == Margin.length) {
    TermLength mtop = style.getValue(TermLength.class, "margin-top");
    System.out.println("value=" + mtop);
}

Multi-value properties

Some properties (e.g. background) allow multiple values. In that case, the NodeData interface includes the getListSize() method for getting the number of values specified and the getProperty() and getValue() functions with an index argument:

//get the style of a single element
Element div = doc.getElementById("searchelement"); //choose a DOM element
NodeData style = map.get(div); //get the style map for the element

//get the number of background images specified for the element
int bgcnt = style.getListSize("background-image", true);

//read all images
for (int index = 0; index < bgcnt; index++>) {
    CSSProperty.BackgroundImage image = style.getProperty("background-image", index);
    if (image == CSSProperty.BackgroundImage.uri) { //if the image is specified by its url
        TermURI urlstring = style.getValue(TermURI.class, "background-image", index);
        //... do something with the image url
    }
}

See the details in the documentation.

License

All the source code of jStyleParser itself is licensed under the GNU Lesser General Public License (LGPL), version 3. A copy of the LGPL can be found in the LICENSE file.

Versions

Version
4.0.0
3.5
3.4
3.3
3.2
3.1
3.0
2.1
2.0
1.23
1.22
1.21
1.20
1.19
1.18
1.17
1.16
1.15