From 130ceae3c6b30283a63f157ea5010a78e63142ab Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 11 Apr 2010 08:28:05 +0200 Subject: Alternate version using javax interfaces rather than the W3C DOM Load&Save. I was rather hoping this would pretty-print XHTML as required (e.g., put a space before the final slash on empty tags), but alas, such is not the case. --- org/madore/damlengine/DamlEngine.java | 44 ++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 19 deletions(-) (limited to 'org/madore/damlengine/DamlEngine.java') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index 8571319..696f509 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -1,13 +1,17 @@ package org.madore.damlengine; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.w3c.dom.DOMImplementation; -import org.w3c.dom.ls.DOMImplementationLS; -import org.w3c.dom.ls.LSParser; -import org.w3c.dom.ls.LSSerializer; -import org.w3c.dom.ls.LSInput; -import org.w3c.dom.ls.LSOutput; import org.w3c.dom.Document; +import org.xml.sax.EntityResolver; import org.apache.xerces.dom.DOMImplementationSourceImpl; +import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl; public final class DamlEngine { @@ -19,33 +23,35 @@ public final class DamlEngine { // ... } - public static void main(String[] args) { + public static void main(String[] args) + throws Exception { final Resolver resolver = new Resolver(); final DOMImplementationSourceImpl source = new DOMImplementationSourceImpl(); final DOMImplementation impl = source.getDOMImplementation("XML 3.0 Core 3.0 LS 3.0"); - final DOMImplementationLS implLS = (DOMImplementationLS)impl; - final LSParser parser - = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, - null); - parser.getDomConfig().setParameter("resource-resolver", resolver); - final LSSerializer writer = implLS.createLSSerializer(); + final DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl(); + dbf.setNamespaceAware(true); + dbf.setValidating(false); + final DocumentBuilder db = dbf.newDocumentBuilder(); + db.setEntityResolver((EntityResolver)resolver); + final TransformerFactory tf = TransformerFactory.newInstance(); + final Transformer t = tf.newTransformer(); + t.setOutputProperty(OutputKeys.METHOD, "xml"); + t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, + "-//W3C//DTD XHTML 1.0 Strict//EN"); + t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); if ( args.length == 0 ) { System.err.println("expecting filename as argument"); } for (String fname : args) { - final LSInput in = implLS.createLSInput(); - in.setSystemId(fname); - doc = parser.parse(in); + doc = db.parse(fname); processDocument(); - final LSOutput out = implLS.createLSOutput(); - out.setByteStream(System.out); - writer.write(doc, out); - System.out.println(""); + t.transform(new DOMSource(doc), new StreamResult(System.out)); } } -- cgit v1.2.3