Skip to content
Permalink
Browse files

Fix #266 by getting the right resolver into Saxon's config

  • Loading branch information...
ndw committed Mar 14, 2018
1 parent 7d0ba61 commit 2ed2aaf8932dd317b1b0d99085840f040e1965ad
@@ -107,6 +107,7 @@
public String mailUser = null;
public String mailPass = null;
public Hashtable<String,String> loaders = new Hashtable<String,String> ();
public HashSet<String> setSaxonProperties = new HashSet<String>();

public boolean extensionValues = false;
public boolean xpointerOnText = false;
@@ -225,10 +226,12 @@ private void init() {
ZipEntry catalog = jar.getEntry("catalog.xml");
if (catalog != null) {
catalogs.add("jar:file://" + s + "!/catalog.xml");
logger.debug("Using catalog: jar:file://" + s + "!/catalog.xml");
}
catalog = jar.getEntry("META-INF/catalog.xml");
if (catalog != null) {
catalogs.add("jar:file://" + s + "!/META-INF/catalog.xml");
logger.debug("Using catalog: jar:file://" + s + "!/META-INF/catalog.xml");
}
} catch (IOException e) {
// If it's not a jar file, maybe it's a directory with a catalog
@@ -240,6 +243,7 @@ private void init() {
File f = new File(catfn);
if (f.exists() && f.isFile()) {
catalogs.add(catfn);
logger.debug("Using catalog: " + catfn);
}
}
}
@@ -811,6 +815,7 @@ private void saxonConfigurationProperty(XdmNode node) {
}

try {
setSaxonProperties.add(key);
cfgProcessor.setConfigurationProperty(key, valueObj);
} catch (Exception e) {
throw new XProcException(e);
@@ -58,8 +58,10 @@
import com.xmlcalabash.util.URIUtils;
import com.xmlcalabash.util.XProcSystemPropertySet;
import com.xmlcalabash.util.XProcURIResolver;
import com.xmlcalabash.util.XProcURIResolverX;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
@@ -196,6 +198,19 @@ public XProcRuntime(XProcConfiguration config) {

Configuration saxonConfig = processor.getUnderlyingConfiguration();
uriResolver = new XProcURIResolver(this);

// Make sure that the Saxon processor uses *our* resolver for everything.
// Unless the user has already provided a class of their own, of course.
XProcURIResolverX saxonFakeStaticResolver = new XProcURIResolverX();
String saxonFakeClassName = saxonFakeStaticResolver.getClass().getName();
saxonFakeStaticResolver.setRealResolver(uriResolver);
if (!config.setSaxonProperties.contains(FeatureKeys.ENTITY_RESOLVER_CLASS)) {
saxonConfig.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, saxonFakeClassName);
}
if (!config.setSaxonProperties.contains(FeatureKeys.URI_RESOLVER_CLASS)) {
saxonConfig.setConfigurationProperty(FeatureKeys.URI_RESOLVER_CLASS, saxonFakeClassName);
}

saxonConfig.setURIResolver(uriResolver);
staticBaseURI = URIUtils.cwdAsURI();

@@ -0,0 +1,48 @@
package com.xmlcalabash.util;

import net.sf.saxon.Configuration;
import net.sf.saxon.lib.ModuleURIResolver;
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.trans.XPathException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.Reader;
import java.net.URI;

public class XProcURIResolverX implements URIResolver, EntityResolver, ModuleURIResolver, UnparsedTextURIResolver {
private static XProcURIResolver realResolver = null;

public XProcURIResolverX() {
// no one can do this
}

public void setRealResolver(XProcURIResolver resolver) {
realResolver = resolver;
}

public Source resolve(String href, String base) throws TransformerException {
return realResolver.resolve(href, base);
}

public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return realResolver.resolveEntity(publicId, systemId);
}

@Override
public StreamSource[] resolve(String moduleURI, String baseURI, String[] locations)
throws XPathException {
return realResolver.resolve(moduleURI, baseURI, locations);
}

@Override
public Reader resolve(URI uri, String encoding, Configuration configuration) throws XPathException {
return realResolver.resolve(uri, encoding, configuration);
}
}

0 comments on commit 2ed2aaf

Please sign in to comment.
You can’t perform that action at this time.