summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/Unparser.java
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-12 15:30:36 +0200
committerDavid A. Madore <david+git@madore.org>2010-04-12 15:30:36 +0200
commit00dc8d9bbb54cf9a0cc47e6326a8444df92a14f1 (patch)
tree03b1db09439a795d2f975c9f638cdc0604bd97e2 /org/madore/damlengine/Unparser.java
parent7766f09c66945626ee883ad1a6710c2fe18dfef3 (diff)
downloaddamlengine-00dc8d9bbb54cf9a0cc47e6326a8444df92a14f1.tar.gz
damlengine-00dc8d9bbb54cf9a0cc47e6326a8444df92a14f1.tar.bz2
damlengine-00dc8d9bbb54cf9a0cc47e6326a8444df92a14f1.zip
Move ad hoc serializer to an "Unparser" class (ugly and unfinished).
Diffstat (limited to 'org/madore/damlengine/Unparser.java')
-rw-r--r--org/madore/damlengine/Unparser.java88
1 files changed, 88 insertions, 0 deletions
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();
+ }
+ }
+
+}