Added a basic indenting XML writer.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 13 Dec 2009 16:02:32 +0000 (17:02 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 13 Dec 2009 16:02:32 +0000 (17:02 +0100)
It still needs a lot of work to be acceptable, of course.

src/dolda/jsvc/next/IndentWriter.java [new file with mode: 0644]
src/dolda/jsvc/next/XmlWriter.java

diff --git a/src/dolda/jsvc/next/IndentWriter.java b/src/dolda/jsvc/next/IndentWriter.java
new file mode 100644 (file)
index 0000000..93f9c0c
--- /dev/null
@@ -0,0 +1,60 @@
+package dolda.jsvc.next;
+
+import java.io.*;
+import org.w3c.dom.*;
+
+public class IndentWriter extends XmlWriter {
+    public int collimit = 80;
+    
+    public IndentWriter(Document doc) {
+       super(doc);
+    }
+    
+    private static boolean onlytext(Element el) {
+       for(Node n = el.getFirstChild(); n != null; n = n.getNextSibling()) {
+           if(!(n instanceof Text))
+               return(false);
+       }
+       return(true);
+    }
+    
+    protected boolean prebreak(ColumnWriter out, Element el) {
+       if(el.getFirstChild() == null)
+           return(false);
+       if(onlytext(el))
+           return(false);
+       return(true);
+    }
+    
+    protected int indent(ColumnWriter out, Element el) {
+       if(onlytext(el))
+           return(-1);
+       return(2);
+    }
+
+    protected boolean postbreak(ColumnWriter out, Element el) {
+       if(out.col > collimit)
+           return(true);
+       return(!onlytext(el));
+    }
+
+    protected void attribute(ColumnWriter out, String nm, String val, int indent) throws IOException {
+       if(out.col > indent) {
+           if(nm.length() + val.length() + 4 > collimit)
+               out.indent(indent);
+       }
+       super.attribute(out, nm, val, indent);
+    }
+    
+    public static void main(String[] args) throws Exception {
+       Html barda = Html.xhtml11("Barda");
+       barda.addcss("/slen.css", "Test");
+       barda.insert("body", barda.el("h1", barda.text("Mast")));
+       barda.finalise();
+       barda.validate();
+       XmlWriter w = new IndentWriter(barda.doc);
+       w.setnsname(Html.ns, null);
+       w.write(System.out);
+       System.out.flush();
+    }
+}
index 6e7c968..a1004e4 100644 (file)
@@ -142,7 +142,7 @@ public class XmlWriter {
        }
        
        if(postbreak(out, el))
-           out.indent(indent);
+           out.write('\n');
     }
     
     protected void text(ColumnWriter out, String s, int indent) throws IOException {
@@ -201,15 +201,4 @@ public class XmlWriter {
        write(w);
        w.flush();
     }
-    
-    public static void main(String[] args) throws Exception {
-       Html barda = Html.xhtml11("Barda");
-       barda.addcss("/slen.css", "Test");
-       barda.insert("body", barda.el("h1", barda.text("Mast")));
-       barda.finalise();
-       XmlWriter w = new XmlWriter(barda.doc);
-       w.setnsname(Html.ns, null);
-       w.write(System.out);
-       System.out.flush();
-    }
 }