1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.functions.ExecutableFunctionLibrary; 4 import net.sf.saxon.instruct.Executable; 5 import net.sf.saxon.om.AttributeCollection; 6 import net.sf.saxon.om.NamePool; 7 import net.sf.saxon.query.StaticQueryContext; 8 import net.sf.saxon.query.XQueryFunction; 9 import net.sf.saxon.query.ModuleURIResolver; 10 import net.sf.saxon.query.QueryReader; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.trans.StaticError; 13 14 import javax.xml.transform.stream.StreamSource ; 15 import java.util.Iterator ; 16 17 18 24 25 public class SaxonImportQuery extends StyleElement { 26 27 private String href; 28 private String moduleURI; 29 30 36 37 public void importModule() throws XPathException { 38 prepareAttributes(); 39 loadLibraryModule(); 40 } 41 42 public void prepareAttributes() throws XPathException { 43 44 if (href!=null || moduleURI!=null) { 46 return; 47 } 48 49 AttributeCollection atts = getAttributeList(); 50 51 for (int a=0; a<atts.getLength(); a++) { 52 int nc = atts.getNameCode(a); 53 String f = getNamePool().getClarkName(nc); 54 if (f==StandardNames.HREF) { 55 href = atts.getValue(a).trim(); 56 } else if (f==StandardNames.NAMESPACE) { 57 moduleURI = atts.getValue(a).trim(); 58 } else { 59 checkUnknownAttribute(nc); 60 moduleURI=""; } 62 } 63 64 if (href==null && moduleURI==null) { 65 compileError("At least one of href or namespace must be specified"); 66 moduleURI=""; } 68 } 69 70 public void validate() throws XPathException { 71 checkEmpty(); 72 checkTopLevel(null); 73 } 74 75 private void loadLibraryModule() throws XPathException { 76 77 if (href==null && moduleURI==null) { 78 return; 80 } 81 82 try { 83 XSLStylesheet top = getPrincipalStylesheet(); 84 getExecutable().setFunctionLibrary(new ExecutableFunctionLibrary(getConfiguration())); 85 StaticQueryContext importedModule = loadModule(); 87 88 90 short ns = importedModule.getModuleNamespaceCode(); 91 NamePool pool = getTargetNamePool(); 92 Iterator it = importedModule.getFunctionDefinitions(); 93 while (it.hasNext()) { 94 XQueryFunction def = (XQueryFunction)it.next(); 95 if (pool.getURICode(def.getFunctionFingerprint()) == ns) { 97 top.declareXQueryFunction(def); 98 } 99 } 101 } catch (XPathException err) { 102 compileError(err); 103 } 104 } 105 106 109 110 private StaticQueryContext loadModule() throws XPathException { 111 113 ModuleURIResolver resolver = getConfiguration().getModuleURIResolver(); 114 if (resolver == null) { 115 resolver = getConfiguration().getStandardModuleURIResolver(); 116 } 117 118 String [] hints = {href}; 119 StreamSource [] sources; 120 try { 121 sources = resolver.resolve(moduleURI, getBaseURI(), hints); 122 if (sources == null) { 123 resolver = getConfiguration().getStandardModuleURIResolver(); 124 sources = resolver.resolve(moduleURI, getBaseURI(), hints); 125 } 126 } catch (XPathException e) { 127 throw StaticError.makeStaticError(e); 128 } 129 130 if (sources.length != 1) { 131 StaticError err = new StaticError("Query module resolver must return a single module"); 132 throw err; 133 } 134 135 136 StreamSource ss = sources[0]; 137 String baseURI = ss.getSystemId(); 138 if (baseURI == null) { 139 ss.setSystemId(hints[0]); 140 } 141 String queryText = QueryReader.readSourceQuery(ss); 142 StaticQueryContext sqc = StaticQueryContext.makeStaticQueryContext( 143 baseURI, getExecutable(), null, queryText, moduleURI); 144 getExecutable().fixupQueryModules(sqc); 145 return sqc; 146 147 148 } 149 150 151 public Expression compile(Executable exec) throws XPathException { 152 exec.setReasonUnableToCompile("Cannot compile a stylesheet that imports an XQuery library module"); 153 return null; 154 } 155 } 156 157 | Popular Tags |