diff options
author | David A. Madore <david+git@madore.org> | 2010-04-11 08:28:05 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-04-11 08:28:05 +0200 |
commit | 130ceae3c6b30283a63f157ea5010a78e63142ab (patch) | |
tree | 54e11c2cc15e9fa662381a27449be1e06006eb78 /org/madore/damlengine | |
parent | 36bb7f0af64624119f24158d410c60f91167d759 (diff) | |
download | damlengine-130ceae3c6b30283a63f157ea5010a78e63142ab.tar.gz damlengine-130ceae3c6b30283a63f157ea5010a78e63142ab.tar.bz2 damlengine-130ceae3c6b30283a63f157ea5010a78e63142ab.zip |
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.
Diffstat (limited to 'org/madore/damlengine')
-rw-r--r-- | org/madore/damlengine/DamlEngine.java | 44 |
1 files changed, 25 insertions, 19 deletions
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)); } } |