summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-11 06:38:42 +0200
committerDavid A. Madore <david+git@madore.org>2010-04-11 06:38:42 +0200
commit36bb7f0af64624119f24158d410c60f91167d759 (patch)
tree474a5e38ffe5d539fa23377495d151b221af0223
downloaddamlengine-36bb7f0af64624119f24158d410c60f91167d759.tar.gz
damlengine-36bb7f0af64624119f24158d410c60f91167d759.tar.bz2
damlengine-36bb7f0af64624119f24158d410c60f91167d759.zip
Start a Java version of a daml engine. So far, just the identity transform.
Even for something so completely trivial, this was a pain to write, because some stupid XML catalog was distributed with a DOCTYPE referencing a broken URL which could not be replaced by a local file because the catalog engine was bootstrapping; nor did Xerces provide a way, in org.apache.xerces.util.XMLCatalogResolver, to provide a bootstrap resolver: so I had to partially rewrite XMLCatalogResolver.
-rw-r--r--org/madore/damlengine/DamlEngine.java53
-rw-r--r--org/madore/damlengine/Resolver.java151
2 files changed, 204 insertions, 0 deletions
diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java
new file mode 100644
index 0000000..8571319
--- /dev/null
+++ b/org/madore/damlengine/DamlEngine.java
@@ -0,0 +1,53 @@
+package org.madore.damlengine;
+
+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.apache.xerces.dom.DOMImplementationSourceImpl;
+
+public final class DamlEngine {
+
+ private static Document doc;
+
+ private DamlEngine() { } // Forbid instantiation
+
+ public static void processDocument() {
+ // ...
+ }
+
+ public static void main(String[] args) {
+
+ 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();
+
+ 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);
+ processDocument();
+ final LSOutput out = implLS.createLSOutput();
+ out.setByteStream(System.out);
+ writer.write(doc, out);
+ System.out.println("");
+ }
+
+ }
+
+}
diff --git a/org/madore/damlengine/Resolver.java b/org/madore/damlengine/Resolver.java
new file mode 100644
index 0000000..80f8afb
--- /dev/null
+++ b/org/madore/damlengine/Resolver.java
@@ -0,0 +1,151 @@
+package org.madore.damlengine;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.MalformedURLException;
+import javax.xml.parsers.SAXParserFactory;
+import org.w3c.dom.ls.LSResourceResolver;
+import org.w3c.dom.ls.LSInput;
+import org.xml.sax.SAXException;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ext.EntityResolver2;
+import org.xml.sax.InputSource;
+import org.apache.xml.resolver.CatalogManager;
+import org.apache.xml.resolver.Catalog;
+import org.apache.xml.resolver.readers.SAXCatalogReader;
+import org.apache.xml.resolver.readers.OASISXMLCatalogReader;
+import org.apache.xerces.dom.DOMInputImpl;
+import org.apache.xerces.jaxp.SAXParserFactoryImpl;
+
+public final class Resolver
+ implements LSResourceResolver, EntityResolver, EntityResolver2 {
+
+ private static final String catalogFiles[] = { "/etc/xml/catalog" };
+
+ private CatalogManager catalogManager = null;
+ private Catalog catalog = null;
+
+ public static final class BootstrapResolver
+ extends org.apache.xml.resolver.helpers.BootstrapResolver {
+ public InputSource resolveEntity(String publicId, String systemId) {
+ String resolved = null;
+ if ( publicId.equals("-//GlobalTransCorp//DTD XML Catalogs "
+ +"V1.0-Based Extension V1.0//EN") ) {
+ resolved
+ = "file:///usr/share/xml/schema/xml-core/tr9401.dtd";
+ }
+ if ( resolved != null ) {
+ InputSource source = new InputSource(resolved);
+ source.setPublicId(publicId);
+ return source;
+ }
+ return super.resolveEntity(publicId, systemId);
+ }
+ }
+
+ public Resolver() {
+ catalogManager = new CatalogManager();
+ // catalogManager.setVerbosity(42);
+ catalogManager.setPreferPublic(true);
+ catalogManager.setRelativeCatalogs(false);
+ catalogManager.setBootstrapResolver(new BootstrapResolver());
+ catalog = new Catalog(catalogManager);
+ SAXParserFactory spf = new SAXParserFactoryImpl();
+ spf.setNamespaceAware(true);
+ spf.setValidating(false);
+ SAXCatalogReader saxReader = new SAXCatalogReader(spf);
+ saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
+ "catalog",
+ "org.apache.xml.resolver.readers."
+ +"OASISXMLCatalogReader");
+ catalog.addReader("application/xml", saxReader);
+ for ( String f : catalogFiles ) {
+ try {
+ catalog.parseCatalog(f);
+ } catch (MalformedURLException e) {
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ public String resolvePublic(String publicId, String systemId) {
+ try {
+ return catalog.resolvePublic(publicId, systemId);
+ } catch (MalformedURLException e) {
+ return null;
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ public String resolveSystem(String systemId) {
+ try {
+ return catalog.resolveSystem(systemId);
+ } catch (MalformedURLException e) {
+ return null;
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ protected String resolve(String namespaceURI,
+ String publicId,
+ String systemId,
+ String baseURI) {
+ String resolved;
+ if ( publicId != null )
+ resolved = resolvePublic(publicId, systemId);
+ else
+ resolved = resolveSystem(systemId);
+ if ( resolved != null )
+ return resolved;
+ if ( baseURI != null ) {
+ try {
+ URI u = new URI(baseURI);
+ URI v = u.resolve(systemId);
+ return v.toString();
+ } catch ( URISyntaxException e ) {
+ }
+ }
+ return systemId;
+ }
+
+ // Interface LSResourceResolver
+
+ public LSInput resolveResource(String type,
+ String namespaceURI,
+ String publicId,
+ String systemId,
+ String baseURI) {
+ String resolved = resolve(namespaceURI, publicId, systemId, baseURI);
+ return new DOMInputImpl(publicId, resolved, baseURI);
+ }
+
+ // Interface EntityResolver
+
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException {
+ String resolved = resolve(null, publicId, systemId, null);
+ InputSource source = new InputSource(resolved);
+ source.setPublicId(publicId);
+ return source;
+ }
+
+ // Interface EntityResolver2
+
+ public InputSource resolveEntity(String name, String publicId,
+ String baseURI, String systemId)
+ throws SAXException {
+ String resolved = resolve(null, publicId, systemId, baseURI);
+ InputSource source = new InputSource(resolved);
+ source.setPublicId(publicId);
+ return source;
+ }
+
+ public InputSource getExternalSubset(String name,
+ String baseURI) {
+ return null;
+ }
+
+}