From 7d7adeeca0ca6e093952c45a24f0f0110f619d24 Mon Sep 17 00:00:00 2001 From: jendib Date: Wed, 13 Apr 2016 01:29:03 +0200 Subject: [PATCH] #79: CSS generator --- .../main/java/com/sismics/util/css/Rule.java | 39 +++++++++++++ .../java/com/sismics/util/css/Selector.java | 56 +++++++++++++++++++ .../test/java/com/sismics/util/TestCss.java | 20 +++++++ .../docs/rest/resource/ThemeResource.java | 5 +- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 docs-core/src/main/java/com/sismics/util/css/Rule.java create mode 100644 docs-core/src/main/java/com/sismics/util/css/Selector.java create mode 100644 docs-core/src/test/java/com/sismics/util/TestCss.java diff --git a/docs-core/src/main/java/com/sismics/util/css/Rule.java b/docs-core/src/main/java/com/sismics/util/css/Rule.java new file mode 100644 index 00000000..930814f0 --- /dev/null +++ b/docs-core/src/main/java/com/sismics/util/css/Rule.java @@ -0,0 +1,39 @@ +package com.sismics.util.css; + +/** + * A CSS rule. + * + * @author bgamard + */ +public class Rule { + /** + * Rule separator. + */ + private static String SEPARATOR = ": "; + + /** + * CSS rule property name. + */ + private String property; + + /** + * CSS rule value. + */ + private String value; + + /** + * Create a new CSS rule. + * + * @param property Property name + * @param value Value + */ + public Rule(String property, String value) { + this.property = property; + this.value = value; + } + + @Override + public String toString() { + return property + SEPARATOR + value; + } +} diff --git a/docs-core/src/main/java/com/sismics/util/css/Selector.java b/docs-core/src/main/java/com/sismics/util/css/Selector.java new file mode 100644 index 00000000..931878a0 --- /dev/null +++ b/docs-core/src/main/java/com/sismics/util/css/Selector.java @@ -0,0 +1,56 @@ +package com.sismics.util.css; + +import java.util.ArrayList; +import java.util.List; + +/** + * A CSS selector. + * + * @author bgamard + */ +public class Selector { + /** + * Selector name. + */ + private String name; + + /** + * Rules in this selector. + */ + private List ruleList; + + /** + * Create a new CSS selector. + * + * @param name Selector name + */ + public Selector(String name) { + this.name = name; + ruleList = new ArrayList<>(); + } + + /** + * Add a CSS rule. + * + * @param property Property name + * @param value Value + * @return CSS selector + */ + public Selector rule(String property, String value) { + ruleList.add(new Rule(property, value)); + return this; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(name); + sb.append(" {\n"); + for (Rule rule : ruleList) { + sb.append(" "); + sb.append(rule); + sb.append(";\n"); + } + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/docs-core/src/test/java/com/sismics/util/TestCss.java b/docs-core/src/test/java/com/sismics/util/TestCss.java new file mode 100644 index 00000000..50206c13 --- /dev/null +++ b/docs-core/src/test/java/com/sismics/util/TestCss.java @@ -0,0 +1,20 @@ +package com.sismics.util; + +import org.junit.Test; + +import com.sismics.util.css.Selector; + +/** + * Test of CSS utilities. + * + * @author bgamard + */ +public class TestCss { + @Test + public void testBuildCss() { + Selector selector = new Selector(".test") + .rule("background-color", "yellow") + .rule("font-family", "Comic Sans"); + System.out.println(selector); + } +} diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java index a8f72deb..ec1f32a0 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java @@ -5,6 +5,8 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import com.sismics.util.css.Selector; + /** * Theme REST resources. * @@ -22,8 +24,7 @@ public class ThemeResource extends BaseResource { @Produces("text/css") public Response stylesheet() { StringBuilder sb = new StringBuilder(); - sb.append("body {\n"); - sb.append("}"); + sb.append(new Selector("body")); return Response.ok().entity(sb.toString()).build(); } }