Permalink
Browse files

Resolve issue #32, support UnparsedTextURIResolver in the p:xslt step.

  • Loading branch information...
ndw committed Jan 30, 2013
1 parent c0b8c40 commit 6cd72d8295d6225810fda06a5d6d190fc8b26cb9
@@ -21,7 +21,17 @@
package com.xmlcalabash.core;
import com.xmlcalabash.config.XProcConfigurer;
-import com.xmlcalabash.functions.*;
+import com.xmlcalabash.functions.BaseURI;
+import com.xmlcalabash.functions.Cwd;
+import com.xmlcalabash.functions.IterationPosition;
+import com.xmlcalabash.functions.IterationSize;
+import com.xmlcalabash.functions.ResolveURI;
+import com.xmlcalabash.functions.StepAvailable;
+import com.xmlcalabash.functions.SystemProperty;
+import com.xmlcalabash.functions.ValueAvailable;
+import com.xmlcalabash.functions.VersionAvailable;
+import com.xmlcalabash.functions.XPathVersionAvailable;
+import com.xmlcalabash.functions.XProcExtensionFunctionDefinition;
import com.xmlcalabash.io.ReadableData;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.runtime.XLibrary;
@@ -19,7 +19,6 @@
package com.xmlcalabash.library;
-import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Hashtable;
@@ -43,12 +42,9 @@
import com.xmlcalabash.util.CollectionResolver;
import com.xmlcalabash.util.S9apiUtils;
import net.sf.saxon.Configuration;
-import net.sf.saxon.Controller;
-import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.CollectionURIResolver;
import net.sf.saxon.lib.OutputURIResolver;
-import net.sf.saxon.lib.TraceListener;
-import net.sf.saxon.om.Item;
+import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.XdmDestination;
@@ -63,7 +59,6 @@
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.event.Receiver;
import com.xmlcalabash.runtime.XAtomicStep;
-import net.sf.saxon.trace.InstructionInfo;
import org.xml.sax.InputSource;
/**
@@ -192,6 +187,7 @@ public void run() throws SaxonApiException {
OutputURIResolver uriResolver = config.getOutputURIResolver();
CollectionURIResolver collectionResolver = config.getCollectionURIResolver();
+ UnparsedTextURIResolver unparsedTextURIResolver = runtime.getResolver();
config.setOutputURIResolver(new OutputResolver());
config.setCollectionURIResolver(new CollectionResolver(runtime, defaultCollection, collectionResolver));
@@ -230,6 +226,7 @@ public void run() throws SaxonApiException {
}
transformer.setSchemaValidationMode(ValidationMode.DEFAULT);
+ transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver);
transformer.transform();
config.setOutputURIResolver(uriResolver);
@@ -1,5 +1,7 @@
package com.xmlcalabash.util;
+import net.sf.saxon.lib.UnparsedTextURIResolver;
+import net.sf.saxon.trans.XPathException;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;
@@ -14,21 +16,24 @@
import javax.xml.parsers.ParserConfigurationException;
import net.sf.saxon.s9api.DocumentBuilder;
-import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.Configuration;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcRuntime;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.Hashtable;
import java.util.logging.Logger;
-import java.io.IOException;
/**
* Created by IntelliJ IDEA.
@@ -37,9 +42,10 @@
* Time: 4:04:27 PM
* To change this template use File | Settings | File Templates.
*/
-public class XProcURIResolver implements URIResolver, EntityResolver {
+public class XProcURIResolver implements URIResolver, EntityResolver, UnparsedTextURIResolver {
private URIResolver uriResolver = null;
private EntityResolver entityResolver = null;
+ private UnparsedTextURIResolver unparsedTextResolver = null;
private XProcRuntime runtime = null;
private Hashtable<String,XdmNode> cache = new Hashtable<String,XdmNode> ();
private Logger logger = Logger.getLogger(this.getClass().getName());
@@ -50,11 +56,15 @@ public XProcURIResolver(XProcRuntime runtime) {
}
public void setUnderlyingURIResolver(URIResolver resolver) {
- this.uriResolver = resolver;
+ uriResolver = resolver;
}
public void setUnderlyingEntityResolver(EntityResolver resolver) {
- this.entityResolver = resolver;
+ entityResolver = resolver;
+ }
+
+ public void setUnderlyingUnparsedTextURIResolver(UnparsedTextURIResolver resolver) {
+ unparsedTextResolver = resolver;
}
public void cache(XdmNode doc, URI baseURI) {
@@ -246,4 +256,21 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
return null;
}
}
+
+ @Override
+ public Reader resolve(URI uri, String s, Configuration configuration) throws XPathException {
+ if (unparsedTextResolver != null) {
+ return unparsedTextResolver.resolve(uri, s, configuration);
+ }
+
+ // Ack. Apparently I have to do this if there isn't a resolver...
+ try {
+ URL url = uri.toURL();
+ URLConnection conn = url.openConnection();
+ InputStream stream = conn.getInputStream();
+ return new InputStreamReader(stream);
+ } catch (Exception e) {
+ throw new XProcException(e);
+ }
+ }
}

0 comments on commit 6cd72d8

Please sign in to comment.