Added basic HTML generation and response handling.
[jrw.git] / src / jrw / resp / Skeleton.java
diff --git a/src/jrw/resp/Skeleton.java b/src/jrw/resp/Skeleton.java
new file mode 100644 (file)
index 0000000..75655bc
--- /dev/null
@@ -0,0 +1,47 @@
+package jrw.resp;
+
+import jrw.*;
+import jrw.sp.*;
+import java.util.*;
+import java.util.function.*;
+import static jrw.sp.cons.*;
+import static jrw.sp.xhtml.cons.*;
+
+public class Skeleton {
+    public static Environment.Variable<Supplier<? extends Skeleton>> defskel = new Environment.Variable<>(() -> Skeleton::new);
+    public List<String> styles = new ArrayList<>();
+    public Element body = xhtml.cons.body();
+    public String title;
+
+    public Skeleton(String title, Object... contents) {
+       this.title = title;
+       populate(body, contents);
+    }
+
+    public Skeleton() {
+       this("");
+    }
+
+    public Skeleton title(String title) {this.title = title; return(this);}
+    public Skeleton style(String... styles) {this.styles.addAll(Arrays.asList(styles)); return(this);}
+    public Skeleton body(Object... data) {populate(body, data); return(this);}
+
+    public Element head(Request req) {
+       Element head = xhtml.cons.head(xhtml.cons.title(title));
+       for(String style : styles)
+           populate(head, link($("rel", "stylesheet"), $("type", "text/css"), $("href", style)));
+       return(head);
+    }
+
+    public Element body(Request req) {
+       return(body);
+    }
+
+    public Element message(Request req) {
+       return(html(head(req), body(req)));
+    }
+
+    public Element error(Request req) {
+       return(message(req));
+    }
+}