From 00dc8d9bbb54cf9a0cc47e6326a8444df92a14f1 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 12 Apr 2010 15:30:36 +0200 Subject: Move ad hoc serializer to an "Unparser" class (ugly and unfinished). --- org/madore/damlengine/DamlEngine.java | 51 ++------------------ org/madore/damlengine/Unparser.java | 88 +++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 47 deletions(-) create mode 100644 org/madore/damlengine/Unparser.java (limited to 'org/madore/damlengine') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index ecbdefd..e5454da 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -1,5 +1,6 @@ package org.madore.damlengine; +import java.io.OutputStreamWriter; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.*; @@ -33,53 +34,9 @@ public final class DamlEngine { for (String fname : args) { doc = db.parse(fname); processDocument(); - Node node0 = null; - Node node = doc; - while ( node != null || node0 != null ) { - if ( node != null && node.getNodeType() == Node.ELEMENT_NODE ) { - Element elt = (Element)node; - System.out.print("<"+elt.getTagName()); - if ( elt.hasAttributes() ) { - NamedNodeMap attrs = elt.getAttributes(); - Node n2; - for ( int i=0 ; (n2=attrs.item(i)) != null ; i++ ) { - Attr attr = (Attr)n2; - System.out.print(" "+attr.getName() - +"=\"(value)\""); - } - } - if ( ! elt.hasChildNodes() ) { - System.out.print(" />"); - node = node.getNextSibling(); - } else { - System.out.print(">"); - node0 = node; - node = node0.getFirstChild(); - } - } else if ( node != null && node.getNodeType() == Node.DOCUMENT_NODE ) { - node0 = node; - node = node0.getFirstChild(); - } else if ( node != null ) { - if ( node.getNodeType() == Node.TEXT_NODE ) { - System.out.print("(text)"); - } else if ( node.getNodeType() == Node.COMMENT_NODE ) { - System.out.print(""); - } else if ( node.getNodeType() == Node.CDATA_SECTION_NODE ) { - System.out.print(""); - } else { - System.out.print(""); - } - node = node.getNextSibling(); - } else { - node = node0; - node0 = node.getParentNode(); - if ( node.getNodeType() == Node.ELEMENT_NODE ) { - Element elt = (Element)node; - System.out.print(""); - } - node = node.getNextSibling(); - } - } + Unparser unparser + = new Unparser(doc, new OutputStreamWriter(System.out)); + unparser.unparse(); } } diff --git a/org/madore/damlengine/Unparser.java b/org/madore/damlengine/Unparser.java new file mode 100644 index 0000000..a903523 --- /dev/null +++ b/org/madore/damlengine/Unparser.java @@ -0,0 +1,88 @@ +package org.madore.damlengine; + +import java.io.Writer; +import java.io.IOException; +import org.w3c.dom.*; + +public final class Unparser { + + private Node cursor; + private enum Dir { PUSHING, POPPING }; + private Dir dir; + private Writer out; + + public Unparser(Document doc, Writer out) { + cursor = doc; + dir = Dir.PUSHING; + this.out = out; + } + + protected void skip() { + Node nsib = cursor.getNextSibling(); + if ( nsib == null ) { + cursor = cursor.getParentNode(); + dir = Dir.POPPING; + } else { + cursor = nsib; + dir = Dir.PUSHING; + } + } + + protected void unparseOne() + throws IOException { + switch ( dir ) { + case PUSHING: + if ( cursor.getNodeType() == Node.ELEMENT_NODE ) { + Element elt = (Element)cursor; + out.write("<"+elt.getTagName()); + if ( elt.hasAttributes() ) { + NamedNodeMap attrs = elt.getAttributes(); + Node n2; + for ( int i=0 ; (n2=attrs.item(i)) != null ; i++ ) { + Attr attr = (Attr)n2; + out.write(" "+attr.getName() + +"=\"(value)\""); + } + } + if ( ! elt.hasChildNodes() ) { + out.write(" />"); + skip(); + } else { + out.write(">"); + cursor = elt.getFirstChild(); + } + } else if ( cursor.getNodeType() == Node.DOCUMENT_NODE ) { + cursor = cursor.getFirstChild(); + skip(); + } else if ( cursor.getNodeType() == Node.TEXT_NODE ) { + out.write("(text)"); + skip(); + } else if ( cursor.getNodeType() == Node.COMMENT_NODE ) { + out.write(""); + skip(); + } else if ( cursor.getNodeType() == Node.CDATA_SECTION_NODE ) { + out.write(""); + skip(); + } else { + out.write(""); + skip(); + } + break; + case POPPING: + if ( cursor.getNodeType() == Node.ELEMENT_NODE ) { + Element elt = (Element)cursor; + out.write(""); + } + skip(); + break; + } + } + + public void unparse() + throws IOException { + while ( cursor != null ) { + unparseOne(); + } + } + +} -- cgit v1.2.3