X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fnext%2FIndentWriter.java;fp=src%2Fdolda%2Fjsvc%2Fnext%2FIndentWriter.java;h=93f9c0c0c04b526551bed7078ddc6808c316eff5;hb=938bdee151320b80b6f2b0dbb01c375a012e1aa4;hp=0000000000000000000000000000000000000000;hpb=5203590bbc5672cc6cc2df1b95179a97a2e42cda;p=jsvc.git 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(); + } +}