실행 시간에 결과 분석기 사용
실행 시간에 결과 분석기를 지정하면 실행 파일에 지정된 출력 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);