Skip to content

Commit

Permalink
Fix #266 by getting the right resolver into Saxon's config
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Mar 14, 2018
1 parent 7c0cf8c commit 3622693
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/xmlcalabash/core/XProcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class XProcConfiguration {
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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -812,6 +816,7 @@ private void saxonConfigurationProperty(XdmNode node) {
}

try {
setSaxonProperties.add(key);
cfgProcessor.setConfigurationProperty(key, valueObj);
} catch (Exception e) {
throw new XProcException(e);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/xmlcalabash/core/XProcRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/xmlcalabash/util/XProcURIResolverX.java
Original file line number Diff line number Diff line change
@@ -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 3622693

Please sign in to comment.