#79: CSS generator

This commit is contained in:
jendib 2016-04-13 01:29:03 +02:00
parent 8ad9c529b6
commit 7d7adeeca0
4 changed files with 118 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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<Rule> 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();
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}