diff options
-rw-r--r-- | org/madore/damlengine/DamlEngine.java | 51 | ||||
-rw-r--r-- | org/madore/damlengine/Unparser.java | 88 |
2 files changed, 92 insertions, 47 deletions
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("<!--(comment)-->"); - } else if ( node.getNodeType() == Node.CDATA_SECTION_NODE ) { - System.out.print("<![CDATA[(cdata)]]>"); - } else { - System.out.print("<!--(some other kind of node)-->"); - } - node = node.getNextSibling(); - } else { - node = node0; - node0 = node.getParentNode(); - if ( node.getNodeType() == Node.ELEMENT_NODE ) { - Element elt = (Element)node; - System.out.print("</"+elt.getTagName()+">"); - } - 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("<!--(comment)-->"); + skip(); + } else if ( cursor.getNodeType() == Node.CDATA_SECTION_NODE ) { + out.write("<![CDATA[(cdata)]]>"); + skip(); + } else { + out.write("<!--(some other kind of node)-->"); + skip(); + } + break; + case POPPING: + if ( cursor.getNodeType() == Node.ELEMENT_NODE ) { + Element elt = (Element)cursor; + out.write("</"+elt.getTagName()+">"); + } + skip(); + break; + } + } + + public void unparse() + throws IOException { + while ( cursor != null ) { + unparseOne(); + } + } + +} |