| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.lib; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.functions.ResolveURI; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
|
| 14 |
import javax.xml.transform.stream.StreamSource; |
| 15 |
import java.io.BufferedInputStream; |
| 16 |
import java.io.IOException; |
| 17 |
import java.io.InputStream; |
| 18 |
import java.io.InputStreamReader; |
| 19 |
import java.net.URI; |
| 20 |
import java.net.URISyntaxException; |
| 21 |
import java.net.URL; |
| 22 |
import java.net.URLConnection; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class StandardModuleURIResolver implements ModuleURIResolver { |
| 38 |
|
| 39 |
Configuration config; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public StandardModuleURIResolver() { |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public StandardModuleURIResolver(Configuration config) { |
| 59 |
this.config = config; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public StreamSource[] resolve(String moduleURI, String baseURI, String[] locations) throws XPathException { |
| 79 |
if (locations.length == 0) { |
| 80 |
XPathException err = new XPathException("Cannot locate module for namespace " + moduleURI); |
| 81 |
err.setErrorCode("XQST0059"); |
| 82 |
err.setIsStaticError(true); |
| 83 |
throw err; |
| 84 |
} else { |
| 85 |
|
| 86 |
StreamSource[] sources = new StreamSource[locations.length]; |
| 87 |
for (int m = 0; m < locations.length; m++) { |
| 88 |
String href = locations[m]; |
| 89 |
URI absoluteURI; |
| 90 |
try { |
| 91 |
absoluteURI = ResolveURI.makeAbsolute(href, baseURI); |
| 92 |
} catch (URISyntaxException err) { |
| 93 |
XPathException se = new XPathException("Cannot resolve relative URI " + href, err); |
| 94 |
se.setErrorCode("XQST0059"); |
| 95 |
se.setIsStaticError(true); |
| 96 |
throw se; |
| 97 |
} |
| 98 |
sources[m] = getQuerySource(absoluteURI); |
| 99 |
} |
| 100 |
return sources; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
protected StreamSource getQuerySource(URI absoluteURI) |
| 120 |
throws XPathException { |
| 121 |
|
| 122 |
String encoding = null; |
| 123 |
try { |
| 124 |
InputStream is; |
| 125 |
if ("classpath".equals(absoluteURI.getScheme())) { |
| 126 |
String path = absoluteURI.getPath(); |
| 127 |
is = config.getDynamicLoader().getResourceAsStream(path); |
| 128 |
if (is == null) { |
| 129 |
XPathException se = new XPathException("Cannot locate module " + path + " on class path"); |
| 130 |
se.setErrorCode("XQST0059"); |
| 131 |
se.setIsStaticError(true); |
| 132 |
throw se; |
| 133 |
} |
| 134 |
} else { |
| 135 |
URL absoluteURL = absoluteURI.toURL(); |
| 136 |
URLConnection connection = absoluteURL.openConnection(); |
| 137 |
connection.connect(); |
| 138 |
is = connection.getInputStream(); |
| 139 |
|
| 140 |
|
| 141 |
String contentType; |
| 142 |
|
| 143 |
|
| 144 |
if (!"file".equals(connection.getURL().getProtocol())) { |
| 145 |
|
| 146 |
|
| 147 |
contentType = connection.getContentType(); |
| 148 |
|
| 149 |
if (contentType != null) { |
| 150 |
int pos = contentType.indexOf("charset"); |
| 151 |
if (pos >= 0) { |
| 152 |
pos = contentType.indexOf('=', pos + 7); |
| 153 |
if (pos >= 0) { |
| 154 |
contentType = contentType.substring(pos + 1); |
| 155 |
} |
| 156 |
if ((pos = contentType.indexOf(';')) > 0) { |
| 157 |
contentType = contentType.substring(0, pos); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
if ((pos = contentType.indexOf('(')) > 0) { |
| 162 |
contentType = contentType.substring(0, pos); |
| 163 |
} |
| 164 |
|
| 165 |
if ((pos = contentType.indexOf('"')) > 0) { |
| 166 |
contentType = contentType.substring(pos + 1, |
| 167 |
contentType.indexOf('"', pos + 2)); |
| 168 |
} |
| 169 |
encoding = contentType.trim(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if (!is.markSupported()) { |
| 176 |
is = new BufferedInputStream(is); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
StreamSource ss = new StreamSource(); |
| 181 |
if (encoding == null) { |
| 182 |
ss.setInputStream(is); |
| 183 |
} else { |
| 184 |
ss.setReader(new InputStreamReader(is, encoding)); |
| 185 |
} |
| 186 |
ss.setSystemId(absoluteURI.toString()); |
| 187 |
return ss; |
| 188 |
} catch (IOException err) { |
| 189 |
XPathException se = new XPathException(err); |
| 190 |
se.setErrorCode("XQST0059"); |
| 191 |
se.setIsStaticError(true); |
| 192 |
throw se; |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|