Added basic HTML generation and response handling.
[jrw.git] / src / jrw / sp / cons.java
diff --git a/src/jrw/sp/cons.java b/src/jrw/sp/cons.java
new file mode 100644 (file)
index 0000000..8c54288
--- /dev/null
@@ -0,0 +1,53 @@
+package jrw.sp;
+
+import java.util.*;
+
+public class cons {
+    public static class Attribute {
+       public final Name name;
+       public final String value;
+
+       public Attribute(Name name, String value) {
+           this.name = name;
+           this.value = value;
+       }
+    }
+
+    public static Attribute $(Name name, String value) {
+       return(new Attribute(name, value));
+    }
+
+    public static Attribute $(String name, String value) {
+       return($(new Name(name), value));
+    }
+
+    public static Attribute $(Namespace ns, String local, String value) {
+       return($(new Name(ns, local), value));
+    }
+
+    private static void populate0(Element el, Iterable<?> contents) {
+       for(Object ob : contents) {
+           if(ob == null) {
+           } else if(ob instanceof Node) {
+               el.add((Node)ob);
+           } else if(ob instanceof Attribute) {
+               el.set(((Attribute)ob).name, ((Attribute)ob).value);
+           } else if(ob instanceof Populous) {
+               ((Populous)ob).populate(el);
+           } else if(ob instanceof Object[]) {
+               populate0(el, Arrays.asList((Object[])ob));
+           } else if(ob instanceof Iterable) {
+               populate0(el, (Iterable<?>)ob);
+           } else if(ob instanceof String) {
+               el.add(new Text((String)ob));
+           } else {
+               el.add(new Text(ob.toString()));
+           }
+       }
+    }
+
+    public static Element populate(Element el, Object... contents) {
+       populate0(el, Arrays.asList(contents));
+       return(el);
+    }
+}