private void importFunctions(XdmNode node) {
String href = node.getAttributeValue(_href);
String ns = node.getAttributeValue(_namespace);
String type = node.getAttributeValue(_type);
Processor processor = runtime.getProcessor();
String sed = processor.getUnderlyingConfiguration().getEditionCode();
if (!"EE".equals(sed)) {
throw new XProcException("Importing functions is only supported by Saxon EE.");
}
try {
URL url = runtime.getStaticBaseURI().resolve(href).toURL();
URLConnection connection = url.openConnection();
FunctionLibrary fl = null;
if (type.contains("xsl")) {
SAXSource stylesheet = new SAXSource(new InputSource(connection.getInputStream()));
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable exec = compiler.compile(stylesheet);
PreparedStylesheet ps = exec.getUnderlyingCompiledStylesheet();
fl = ps.getFunctionLibrary();
} else {
XQueryCompiler xqcomp = processor.newXQueryCompiler();
StaticQueryContext sqc = xqcomp.getUnderlyingStaticContext();
sqc.compileLibrary(connection.getInputStream(), "utf-8");
XQueryExpression xqe = sqc.compileQuery("import module namespace f='" + ns + "'; .");
QueryModule qm = xqe.getStaticContext();
fl = qm.getGlobalFunctionLibrary();
}
// We think this will work because we know from the test above that we're not in Saxon HE.
// It's a bit fragile, but I can't think of a better way.
Class<?> pc = Class.forName("com.saxonica.config.ProfessionalConfiguration");
Class<?> fc = Class.forName("net.sf.saxon.functions.FunctionLibrary");
Method setBinder = pc.getMethod("setExtensionBinder", String.class, fc);
setBinder.invoke(processor.getUnderlyingConfiguration(), "xmlcalabash" + importCount, fl);
importCount++;
} catch (Exception e) {
throw new XProcException(e);
}
}