From c5d9cae77b1cf62735550bfe9a1161b72421f5e2 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 11 Apr 2010 11:10:27 +0200 Subject: Implement a toy serializer of my own (just the structure). This should go into a separate class, of course. --- org/madore/damlengine/DamlEngine.java | 63 ++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 15 deletions(-) (limited to 'org/madore') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index 80b1f2d..ecbdefd 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -2,15 +2,10 @@ package org.madore.damlengine; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; +import org.w3c.dom.*; import org.xml.sax.EntityResolver; -import org.apache.xerces.dom.DOMImplementationSourceImpl; import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl; -import org.apache.xml.serialize.XHTMLSerializer; -import org.apache.xml.serialize.OutputFormat; -@SuppressWarnings("deprecation") public final class DamlEngine { private static Document doc; @@ -25,11 +20,6 @@ public final class DamlEngine { throws Exception { final Resolver resolver = new Resolver(); - final DOMImplementationSourceImpl source - = new DOMImplementationSourceImpl(); - @SuppressWarnings("unused") - final DOMImplementation impl - = source.getDOMImplementation("XML 3.0 Core 3.0 LS 3.0"); final DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl(); dbf.setNamespaceAware(true); dbf.setValidating(false); @@ -43,10 +33,53 @@ public final class DamlEngine { for (String fname : args) { doc = db.parse(fname); processDocument(); - OutputFormat format = new OutputFormat(doc); - XHTMLSerializer serializer = new XHTMLSerializer(format); - serializer.setOutputByteStream(System.out); - serializer.serialize(doc); + 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(); + } + } } } -- cgit v1.2.3