モジュール・リゾルバーの使用
XModuleResolver インターフェースが実装できます。このインターフェースを XStaticContext に登録された実装として指定することで XQuery モジュールのデフォルトの解決動作をオーバーライドすることができます。 XQuery モジュールのインポートが実行される時は常に、モジュールは解決されます。
このタスクについて
デフォルトのモジュール解決動作は、モジュール・インポートに指定されるそれぞれのロケーション・ヒントごとに 1 つのモジュールを配置することです。 各ロケーション・ヒントに対するデフォルトの解決動作は、基本 URI が使用可能な場合には、静的コンテキストの基本 URI に基づいて相対 URI を解決すること、あるいは、基本 URI が使用不可の場合には、現行作業ディレクトリーに相対のファイル・パスとしてこれらの URI をインタープリットすることです。 絶対 URI はそのまま使用されます。ロケーション・ヒントにモジュールを配置できない場合、その名前空間にまったくモジュールをロードできない場合を除いて、プロセッサーはそれを無視します。モジュールをまったくロードできない場合は、プロセッサーはエラー・メッセージを出します。
手順
XStaticContext インターフェースの setModuleResolver メソッドを使用して、モジュール・リゾルバーを登録します。
getModule メソッドは java.util.List インターフェースのインスタンスを返します。これは、特定の名前空間およびロケーション・ヒントの集合に対して複数のモジュール文書がある可能性があるためです。
例
以下に、モジュール・リゾルバーを使用する基本的な例を示します。
XFactory factory = XFactory.newInstance();
// Create the static context
XStaticContext staticContext = factory.newStaticContext();
// Create the module resolver and register it with the static context.
staticContext.setModuleResolver(new AModuleResolver(replacementBase));
// Prepare the query.
XQueryExecutable executable = factory.prepareXQuery(new StreamSource(queryFile), staticContext);
// Execute the transformation.
Source source = new StreamSource(inputFile);
Result result = new StreamResult(System.out);
executable.execute(source, result);
以下に、モジュール・リゾルバー実装の基本的な例を示します。
class AModuleResolver implements XModuleResolver
{
String _replacementBase;
public AModuleResolver(String replacementBase)
{
_replacementBase=replacementBase;
}
// Resolve URI, returning the Source that URI represents.
// Implements the "rebase:" pseudo-scheme.
public List<? extends Source> getModule(String namespace, List<String> locations, String baseURI) {
String rebasePrefix="rebase:";
List<StreamSource> list = new ArrayList<StreamSource>();
for (int i = 0; i < locations.size(); i++) {
String href = locations.get(i);
String base = baseURI;
if(href.startsWith(rebasePrefix)) {
href=href.substring(rebasePrefix.length());
base=_replacementBase;
}
java.net.URI uri;
StreamSource source=null;
try {
// Get base URI object
uri = new java.net.URI(base);
// Resolved relative reference against base URI
URI resolvedURI = uri.resolve(href);
// Try to read...
source = new StreamSource(resolvedURI.toString());
} catch (java.net.URISyntaxException use) {
throw new RuntimeException(use);
}
list.add(source);
}
return list;
}
}