Skip to content

Commit

Permalink
Setup and use our own UnparsedTextURIResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Aug 26, 2023
1 parent 465c11e commit a97ec1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/xmlcalabash/core/XProcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class XProcConfiguration {
public String stepName = null;
public String entityResolver = "org.xmlresolver.Resolver";
public String uriResolver = "org.xmlresolver.Resolver";
public String unparsedTextResolver = null;
public String errorListener = null;
public HashMap<QName,Class> implementations = new HashMap<QName,Class> ();
public HashMap<String,String> serializationOptions = new HashMap<String,String>();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/xmlcalabash/core/XProcRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.om.AttributeMap;
import net.sf.saxon.om.EmptyAttributeMap;
import net.sf.saxon.om.NamespaceUri;
Expand Down Expand Up @@ -226,6 +227,7 @@ public XProcRuntime(XProcConfiguration config) {
}

saxonConfig.setResourceResolver(uriResolver);
saxonConfig.setUnparsedTextURIResolver(uriResolver);
staticBaseURI = URIUtils.cwdAsURI();

try {
Expand All @@ -235,6 +237,9 @@ public XProcRuntime(XProcConfiguration config) {
if (config.entityResolver != null) {
uriResolver.setUnderlyingEntityResolver(Class.forName(config.entityResolver).asSubclass(EntityResolver.class).getDeclaredConstructor().newInstance());
}
if (config.unparsedTextResolver != null) {
uriResolver.setUnderlyingUnparsedTextURIResolver(Class.forName(config.unparsedTextResolver).asSubclass(UnparsedTextURIResolver.class).getDeclaredConstructor().newInstance());
}

if (config.errorListener != null) {
msgListener = Class.forName(config.errorListener).asSubclass(XProcMessageListener.class).getDeclaredConstructor().newInstance();
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/com/xmlcalabash/util/XProcURIResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.xmlresolver.CatalogResolver;
import org.xmlresolver.ResolvedResource;
import org.xmlresolver.Resolver;
import org.xmlresolver.sources.ResolverSAXSource;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
Expand All @@ -29,6 +30,7 @@
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -398,8 +400,10 @@ public StreamSource[] resolve(String moduleURI, String baseURI, String[] locatio

@Override
public Reader resolve(URI uri, String encoding, Configuration configuration) throws XPathException {
if (unparsedTextResolver == null) {
// If there's no resolver, let Saxon do it...
// If there's no resolver, let's try to use the catalog resolver
if (catalogResolver != null) {
unparsedTextResolver = new CatalogUnparsedTextURIResolver(catalogResolver);
} else {
unparsedTextResolver = new StandardUnparsedTextResolver();
}

Expand Down Expand Up @@ -454,4 +458,33 @@ public Source resolve(ResourceRequest request) throws XPathException {
}
}
}

private class CatalogUnparsedTextURIResolver implements UnparsedTextURIResolver {
private final Resolver resolver;

public CatalogUnparsedTextURIResolver(Resolver resolver) {
this.resolver = resolver;
}

@Override
public Reader resolve(URI uri, String encoding, Configuration configuration) throws XPathException {
InputStreamReader reader = null;

try {
Source source = resolver.resolveNamespace(uri.toString(), "https://www.iana.org/assignments/media-types/text/plain", null);

if (source instanceof ResolverSAXSource) {
InputSource isource = ((ResolverSAXSource) source).getInputSource();
reader = new InputStreamReader(isource.getByteStream());
} else if (source != null) {
URL url = new URL(source.getSystemId());
reader = new InputStreamReader(url.openStream());
}
} catch (IOException|TransformerException ex) {
reader = null;
}

return reader;
}
}
}

0 comments on commit a97ec1e

Please sign in to comment.