From 938bdee151320b80b6f2b0dbb01c375a012e1aa4 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 13 Dec 2009 17:02:32 +0100 Subject: [PATCH] Added a basic indenting XML writer. It still needs a lot of work to be acceptable, of course. --- src/dolda/jsvc/next/IndentWriter.java | 60 +++++++++++++++++++++++++++++++++++ src/dolda/jsvc/next/XmlWriter.java | 13 +------- 2 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 src/dolda/jsvc/next/IndentWriter.java diff --git a/src/dolda/jsvc/next/IndentWriter.java b/src/dolda/jsvc/next/IndentWriter.java new file mode 100644 index 0000000..93f9c0c --- /dev/null +++ b/src/dolda/jsvc/next/IndentWriter.java @@ -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(); + } +} diff --git a/src/dolda/jsvc/next/XmlWriter.java b/src/dolda/jsvc/next/XmlWriter.java index 6e7c968..a1004e4 100644 --- a/src/dolda/jsvc/next/XmlWriter.java +++ b/src/dolda/jsvc/next/XmlWriter.java @@ -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(); - } } -- 2.11.0