Looking for a CSS Parser in Java

CSS Parser in Java

Perhaps this might help you?

CSS parser parsing string content

A workaround that I found was to create a Reader using a StringReader with the contents and set the characterStream for the Input source. But there should be a better way to do this..

InputSource inputSource = new InputSource();
Reader characterStream = new StringReader(cssContent);
inputSource.setCharacterStream(characterStream);
CSSStyleSheet stylesheet = cssParserObj.parseStyleSheet(source, null,
null);
CSSRuleList ruleList = stylesheet.getCssRules();

How to parse/map JavaFX CSS file to retrieve its properties and values?

There are several ways you can tackle the conversion of a CSS declaration into a Color.

Style an auxiliar node

This is quite simple, but effective: The idea is that you could just style the background color of a node with the same css, and then set the colorPicker value with that color.

The only thing you need to take into account in this case is that the node is styled only when is added to a scene.

So you have to add the node to the scene. Adding a node with 0x0 size won't cause any issue, but maybe you don't want it to be there, so you can use an auxiliar scene.

public class CSSParsingApp extends Application {

@Override
public void start(Stage primaryStage) {
ColorPicker cpBackground = new ColorPicker(retrieveColor("value1"));
ColorPicker cpBase = new ColorPicker(retrieveColor("value2"));
ColorPicker cpDefaultButton = new ColorPicker(retrieveColor("value3"));

VBox root = new VBox(10, cpBackground, cpDefaultButton, cpBase);
root.setAlignment(Pos.CENTER);

Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

primaryStage.setScene(scene);
primaryStage.show();
}

private Color retrieveColor(String value) {
Pane pane = new Pane();
pane.getStyleClass().add(value);

Scene sceneAux = new Scene(pane);
sceneAux.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
pane.applyCss();
return (Color) pane.getBackground().getFills().get(0).getFill();
}

public static void main(String[] args) {
launch(args);
}

}

where style.css is:

.root {
-fx-background: #325c81;
-fx-default-button: #77a3ca;
-fx-base: #a7c4dd;
}

.value1 {
-fx-background-color: -fx-background;
}
.value2 {
-fx-background-color: -fx-default-button;
}
.value3 {
-fx-background-color: -fx-base;
}

Use StylableProperties

A similar, more elegant solution is found here. It uses StylableProperties to create a node, that you can style with a custom -named-color property, and then adds this helper node to the main scene.

Basically it is the same idea as the one above, maybe more clean, as you don't need to modify your css file.

Using CssToColorHelper, your code will be like this:

public class CSSParsingApp extends Application {

private CssToColorHelper helper = new CssToColorHelper();

@Override
public void start(Stage primaryStage) {
ColorPicker cpBackground = new ColorPicker();
ColorPicker cpBase = new ColorPicker();
ColorPicker cpDefaultButton = new ColorPicker();

VBox root = new VBox(10, cpBackground, cpDefaultButton, cpBase, helper);
root.setAlignment(Pos.CENTER);

Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

cpBackground.setValue(getNamedColor("-fx-background"));
cpDefaultButton.setValue(getNamedColor("-fx-default-button"));
cpBase.setValue(getNamedColor("-fx-base"));

primaryStage.setScene(scene);
primaryStage.show();
}

private Color getNamedColor(String name) {
helper.setStyle("-named-color: " + name + ";");
helper.applyCss();

return helper.getNamedColor();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

where style.css is your css file:

.root {
-fx-background: #325c81;
-fx-default-button: #77a3ca;
-fx-base: #a7c4dd;
}

Use JavaFX CSSParser

If you are looking for a CSSParser, why don't you just use the one included in JavaFX, the one you actually use to apply styling to your app?

It is under com.sun.javafx.css.parser.CSSParser, and if the answer is you don't want to use private API, the good news is that it will be public API in JavaFX 9.

With it you can parse the css file and retrieve any parsed value easily.

 public class CSSParsingApp extends Application {

@Override
public void start(Stage primaryStage) {
ColorPicker cpBackground = new ColorPicker();
ColorPicker cpBase = new ColorPicker();
ColorPicker cpDefaultButton = new ColorPicker();

VBox root = new VBox(10, cpBackground, cpDefaultButton, cpBase);
root.setAlignment(Pos.CENTER);

Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

cpBackground.setValue(parseColor("-fx-background"));
cpDefaultButton.setValue(parseColor("-fx-default-button"));
cpBase.setValue(parseColor("-fx-base"));

primaryStage.setScene(scene);
primaryStage.show();
}

private Color parseColor(String property) {
CSSParser parser = new CSSParser();
try {
Stylesheet css = parser.parse(getClass().getResource("style.css").toURI().toURL());
final Rule rootRule = css.getRules().get(0); // .root
return (Color) rootRule.getDeclarations().stream()
.filter(d -> d.getProperty().equals(property))
.findFirst()
.map(d -> ColorConverter.getInstance().convert(d.getParsedValue(), null))
.get();
} catch (URISyntaxException | IOException ex) { }
return Color.WHITE;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

where style.css is your css file:

.root {
-fx-background: #325c81;
-fx-default-button: #77a3ca;
-fx-base: #a7c4dd;
}

Getting style information (css) of a node after parsing an html page in Java

jStyleParser provides exactly this functionality. It parses all the referenced style sheets and maps them to the DOM tree nodes. More options are offered for a similar question here.

Looking for a CSS parser in Ruby

You have a few options, such as...

  • Ruby CSS Parser.
  • TamTam
  • CSSPool


Related Topics



Leave a reply



Submit