if (mod != null) {
return mod;
}
if (locationURI == null) {
throw new StaticError(
"import module must either specify a known namespace or a location");
}
// Resolve relative URI
URL absoluteURL;
if (baseURI==null) { // no base URI available
try {
// the href might be an absolute URL
absoluteURL = new URL(locationURI);
} catch (MalformedURLException err) {
// it isn't
throw new StaticError("Cannot resolve URI (no base URI available)", err);
}
} else {
try {
absoluteURL = new URL(new URL(baseURI), locationURI);
} catch (MalformedURLException err) {
throw new StaticError("Cannot resolve relative URI", err);
}
}
InputStream is;
try {
is = absoluteURL.openStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer sb = new StringBuffer(2048);
char[] buffer = new char[2048];
int actual;
while (true) {
actual = reader.read(buffer, 0, 2048);
if (actual<0) break;
sb.append(buffer, 0, actual);
}
reader.close();
is.close();
StaticQueryContext module = new StaticQueryContext(config);
module.setBaseURI(absoluteURL.toString());
module.setExecutable(executable);
QueryParser qp = new QueryParser();
qp.parseLibraryModule(sb.toString(), module);
if (module.getModuleNamespace() == null) {
throw new StaticError(
"Imported module must be a library module");
}
if (!module.getModuleNamespace().equals(namespaceURI)) {
throw new StaticError(
"Imported module's namespace does not match requested namespace");
}
executable.addQueryLibraryModule(module);
return module;
} catch (java.io.IOException ioErr) {
throw new StaticError(ioErr);
}
}