実行時における結果リゾルバーの使用
実行時に結果リゾルバーを指定すると、実行可能ファイル 内で指定された出力 URI のリダイレクト方法をプロセッサーに指示することができます。
手順
結果リゾルバーを活動状態にするには、このリゾルバーを動的コンテキストに登録
してから、execute() を呼び出します。
結果リゾルバーは、 プロセッサーの出力側で実行される以外は、基本的にソース・リゾルバーと同じ機能を実行します。 結果リゾルバーは、スタイルシートの xsl:result-document ディレクティブのように、 実行可能ファイルに指定された出力 URI をインターセプトし、リダイレクトできるようにします。
デフォルトの解決動作では、URI 参照が相対である場合に、 基本出力 URI を使用して結果文書を解決します。 絶対 URI はそのまま使用されます。
以下に、
結果リゾルバーの基本的な例を示します。
class AResultResolver implements XResultResolver
{
String _replacementBase;
public AResultResolver(String replacementBase)
{
_replacementBase=replacementBase;
}
// Resolve URIs by loading the resource as an XSLT stylesheet
// and evaluating it - return the result as the Source to use
public Result getResult(String href, String base) {
String rebasePrefix="rebase://";
if(href.startsWith(rebasePrefix))
{
href=href.substring(rebasePrefix.length());
base=_replacementBase;
}
java.net.URI baseURI;
Result result=null;
try {
// Get base URI object
baseURI = new java.net.URI(base);
// Resolved relative reference against base URI
URI resolvedURI = baseURI.resolve(href);
// Try to read...
result = new StreamResult(resolvedURI.toString());
} catch (java.net.URISyntaxException use) {
throw new RuntimeException(use);
}
return result;
}
}
以下に、リゾルバーの登録と使用の基本的な例を示します。
XFactory factory = XFactory.newInstance();
XStaticContext staticContext = factory.newStaticContext();
// Prepare the stylesheet
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile), staticContext);
XDynamicContext dynamicContext = factory.newDynamicContext();
// Register the result resolver with the dynamic context
XResultResolver resultResolver=new AResultResolver(replacementBase);
dynamicContext.setResultResolver(resultResolver);
// Execute the XPath expression
XSequenceCursor cursor = executable.execute(new StreamSource(inputFile), dynamicContext);