<?xml version="1.0" encoding="UTF-8"?>
<essay xml:lang="en" version="5.0" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:gal="http://norman.walsh.name/rdf/gallery#" xmlns:foaf="http://xmlns.com/foaf/0.1/">
<info>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
<title>Resolver APIs</title><biblioid class="uri">http://norman.walsh.name/2007/02/09/resolvers</biblioid>
<volumenum>10</volumenum>
<issuenum>10</issuenum>
<pubdate>2007-02-09T15:52:52-05:00</pubdate>
<date>$Date: 2007-04-25 07:53:59 -0400 (Wed, 25 Apr 2007) $</date>
<author>
      <personname>
<firstname>Norman</firstname>
	<surname>Walsh</surname>
</personname>
    </author>
<copyright>
      <year>2007</year>
      <holder>Norman Walsh</holder>
    </copyright>
<abstract>
<para>How can I resolve thee? Let me count the ways.</para>
</abstract>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#GlassFish"/>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#Java"/>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#XML"/>
<dc:subject rdf:resource="http://norman.walsh.name/knows/taxonomy#XMLCatalogs"/>
</info>

<para xml:id="p3">Working on my <link xlink:href="/2007/02/06/xmlresolver">XMLResolver</link>
project has made me look again at the range of entity and URI resolution APIs
in the Java platform. How many are there?
I got to six without breaking a sweat.</para>

<para xml:id="p1">You can resolve with <wikipedia page="Simple_API_for_XML">SAX</wikipedia>,
<interfacename xlink:href="http://java.sun.com/javase/6/docs/api/org/xml/sax/EntityResolver.html">org.xml.sax.EntityResolver</interfacename>:</para>

<programlisting>InputSource resolveEntity(String publicId, String systemId);</programlisting>

<para xml:id="p4">Here we can't look at the entity name nor can we look at the
system identifier before it's been made absolute. There's also no way
for this interface to indicate failure to resolve, so we can't
effectively chain resolvers.</para>

<para xml:id="p2">Also with SAX,
<interfacename xlink:href="http://java.sun.com/javase/6/docs/api/org/xml/sax/ext/EntityResolver2.html">org.xml.sax.ext.EntityResolver2</interfacename>:</para>

<programlisting>InputSource getExternalSubset(String name, String baseURI);

InputSource resolveEntity(String name,
                          String publicId,
                          String baseURI,
                          String systemId);</programlisting>

<para xml:id="p5">Getting an external subset for a document that doesn't have one is kind of
cool, though I've never actually tried it.</para>

<para xml:id="p6">This version of
<methodname>resolveEntity</methodname> fixes the bugs in the earlier
API: we can examine the system identifier before and after making it
absolute and we can return <code>null</code> to indicate that we
weren't able to resolve it.</para>

<para xml:id="p7">With <wikipedia>StAX</wikipedia>,
<interfacename xlink:href="http://java.sun.com/javase/6/docs/api/javax/xml/stream/XMLResolver.html">javax.xml.stream.XMLResolver</interfacename>:</para>

<programlisting>Object resolveEntity(String publicID,
                     String systemID,
                     String baseURI,
                     String namespace)</programlisting>

<para xml:id="p8">Oh, joy! It returns <classname>Object</classname>! And notice how its signature
conflicts with the <interfacename>EntityResolver2</interfacename> interface so you
can't even implement both APIs in a single class.</para>

<para xml:id="p9">I can't imagine how a namespace URI comes into play for
resolving an entity that includes <emphasis>either</emphasis>
a system <emphasis>or</emphasis> public identifier. And vice versa.
</para>

<para xml:id="p10">And where's the name of the entity? That I could actually <emphasis>use</emphasis>!
</para>

<para xml:id="p11">We're just warming up,
<wikipedia page="Document_Object_Model">DOM</wikipedia>
gets into the act too, with
<interfacename xlink:href="http://java.sun.com/javase/6/docs/api/org/w3c/dom/ls/LSResourceResolver.html">org.w3c.dom.ls.LSResourceResolver</interfacename>:</para>

<programlisting>LSInput resolveResource(String type,
                        String namespaceURI,
                        String publicId,
                        String systemId,
                        String baseURI);</programlisting>

<para xml:id="p12">Another bizarre conflation of
<link xlink:href="http://www.w3.org/TR/REC-xml/#NT-ExternalID">external identifiers</link>
and
<link xlink:href="http://www.w3.org/TR/REC-xml-names/#dt-NSName">namespace
names</link>.</para>

<para xml:id="p13">The string valued “<code>type</code>” makes this a sort of chameleon method.
If you specify a type of “<uri>http://www.w3.org/TR/REC-xml</uri>” then it's
supposed to resolve “XML resources (i.e. entities)” (sic). But without the
entity name, apparently.</para>

<para xml:id="p14">If you specify a type of “<uri>http://www.w3.org/2001/XMLSchema</uri>”
then it's supposed to resolve “XML Schema [resources]”. But when does
<emphasis>anything</emphasis> have both a namespace <emphasis>and</emphasis>
an external identifier?</para>

<para xml:id="p15">(If you specify any other type, you're on your own. Fair enough,
I guess.)</para>

<para xml:id="p16">The Transformer API has one too,
<interfacename xlink:href="http://java.sun.com/javase/6/docs/api/javax/xml/transform/URIResolver.html">javax.xml.transform.URIResolver</interfacename>:</para>

<programlisting>Source resolve(String href, String base);</programlisting>

<para xml:id="p17">This is just for URIs, so it's at least a little less complicated.</para>

<para xml:id="p18">Finally, contributing to the problem myself, I've proposed one more in
<interfacename xlink:href="https://xmlresolver.dev.java.net/nonav/javadoc/org/xmlresolver/NamespaceResolver.html">org.xmlresolver.NamespaceResolver</interfacename>:</para>

<programlisting>Source resolveNamespace(String uri, String nature, String purpose);</programlisting>

<para xml:id="p19">This one resolves namespaces using
<wikipedia>RDDL</wikipedia> natures and purposes. (You
<link xlink:href="http://www.w3.org/TR/REC-xml-names/#iri-use">aren't supposed</link>
to use relative URIs as namespace names, so my decision not to support them in
this API was intentional.)</para>

<para xml:id="p20">On the whole: Bleh!</para>

<para xml:id="p21">I wonder how many of these are actually used with any
regularity? I wonder if there's any opportunity for unification
and simplification? (And I wonder how many more there are out there.)</para>

</essay>

