/** * Import an XQuery library module from the given document. The namespace and preferred prefix of * the module are extracted from the module itself. The MIME type of the document is set to * "application/xquery" as a side-effect. * * @param module the non-XML document that holds the library module's source * @return this service, to chain calls * @throws DatabaseException if the module is an XML document, or the module declaration cannot be * found at the top of the document */ public QueryService importModule(Document module) { if (module instanceof XMLDocument) throw new DatabaseException("module cannot be an XML document: " + module); Matcher matcher = MODULE_DECLARATION_DQUOTE.matcher(module.contentsAsString()); if (!matcher.find()) { matcher = MODULE_DECLARATION_SQUOTE.matcher(module.contentsAsString()); if (!matcher.find()) throw new DatabaseException("couldn't find a module declaration at the top of " + module); } module.metadata().setMimeType("application/xquery"); String moduleNamespace = matcher.group(1); // TODO: should do URILiteral processing here to replace entity and character references and // normalize // whitespace, but since it seems that eXist doesn't do it either (bug?) there's no reason to // rush. Document prevModule = moduleMap.get(moduleNamespace); if (prevModule != null && !prevModule.equals(module)) throw new DatabaseException( "module " + moduleNamespace + " already bound to " + prevModule + ", can't rebind to " + module); moduleMap.put(moduleNamespace, module); return this; }
private String presub(String query, Object[] params) { if (params == null) return query; StringBuffer buf = new StringBuffer(); Matcher matcher = PRE_SUB_PATTERN.matcher(query); while (matcher.find()) { matcher.appendReplacement( buf, ((String) params[Integer.parseInt(matcher.group(1)) - 1]) .replace("\\", "\\\\") .replace("$", "\\$")); } matcher.appendTail(buf); return buf.toString(); }