J2EE コンテキストでのリゾルバーの使用

Java™ 2 Platform, Enterprise Edition (J2EE) コンテキスト内に成果物をロードする場合は、 ローカルのデプロイメント成果物からリソースをロードするときに生じる特別な影響を考慮する必要があります。

このタスクについて

J2EE デプロイメント成果物 (EAR、WAR、ライブラリー JAR ファイルなど) から Class.getResource および Class.getResourceAsStream を通じて ローカル・リソースをロードすると、関連した XML 成果物のロード時に問題を引き起こす可能性があります。 これらの手段を使用して初期のローカル・リソースをロードするのは正常に行われますが、概して 初期リソースからロードされた成果物を安易にロードすると失敗します。

ここで、XPath fn:doc 関数を使用して、実行時にスタイルシートから文書を ロードする例を示します。 この場合、デフォルト動作では、静的コンテキストからの基本 URI を基に文書を解決します。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
		<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
		<xsl:variable name="names" select="doc('names.xml')"/>
	
		<xsl:template name="loadnames">
				<xsl:copy-of select="$names"/>
	</xsl:template>
	
</xsl:stylesheet>
これを J2EE 環境で以下の Java コードを使用して ロードした場合は、次のようになります。
// create the factory
XFactory factory = XFactory.newInstance();

// load the XSLT file from a local resource within a J2EE deployment artifact
StreamSource source = new StreamSource(XSLTDocFunction.class.getResourceAsStream("/samplexslts/doc.xsl"));

// Create an XSL transform executable
XSLTExecutable xslTransform = factory.prepareXSLT(source);
        
// Create the result
Result result = new StreamResult(new ByteArrayOutputStream());
        
// Create a dynamic context specifying the XSLT initial template
XDynamicContext dc = factory.newDynamicContext();
dc.setXSLTInitialTemplate(new QName("loadnames"));
        
// Execute the transformation
xslTransform.execute(dc, result);
以下のエラーを受け取ります。
IXJXE0364W: FATAL ERROR:  IXJXE0774E: [ERR 0693][ERR FODC0005] 
The URI string 'names.xml' does not map to an available document.

このエラーの理由は、初期 XML 成果物の ロード時に、基本 URI の静的コンテンツが設定されていなかったことにあります。 この場合、プロセッサーは、J2EE 環境では無意味な現行作業ディレクトリーを調べるために フォールバックします。

この状態を修正するには、3 つの方法が使用できます。 これらの方法の内、最初の 2 つを以下に示します。
  • 静的コンテキスト内に baseURI を設定します。
    静的コンテキストに baseURI を設定するように調整するには、以下の例のようにします。
    // create the factory
    XFactory factory = XFactory.newInstance();
            
    // set the baseURI in the static context
    URL dataURL = XSLTSchemaAware.class.getResource("/samplexslts/doc.xsl");
    XStaticContext staticContext = factory.newStaticContext();
    staticContext.setBaseURI(dataURL.toString());
    
    // load the XSLT file from a local resource within a J2EE deployment artifact        
    StreamSource source = new StreamSource(XSLTDocFunction.class.getResourceAsStream("/samplexslts/doc.xsl"));
    
    // Create an XSL transform executable
    XSLTExecutable xslTransform = factory.prepareXSLT(source, staticContext);
            
    // Create the result
    Result result = new StreamResult(new ByteArrayOutputStream());
    
    // Create a dynamic context specifying the XSLT initial template
    XDynamicContext dc = factory.newDynamicContext();
    dc.setXSLTInitialTemplate(new QName("loadnames"));
            
    // Execute the transformation
    xslTransform.execute(dc, result);
  • プロセッサーが baseURI を認識できるようにする方法でリソースをロードします。
    プロセッサーが baseURI を認識できるようにする方法でリソースをロードするよう調整するには、 以下の例のように、絶対 URL を StreamSource コンストラクターに渡します。
    // Create the factory
    XFactory factory = XFactory.newInstance();
            
    // Create the source from a URL
    URL dataURL = XSLTSchemaAware.class.getResource("/samplexslts/doc.xsl");
    StreamSource source = new StreamSource(dataURL.toString());
    
    // Create an XSL transform executable for the expression
    XSLTExecutable xslTransform = factory.prepareXSLT(source);
            
    // Create the result
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
            
    // Create a dynamic context specifying the XSLT initial template
    XDynamicContext dc = factory.newDynamicContext();
    dc.setXSLTInitialTemplate(new QName("loadnames"));
            
    // Execute the transformation
    xslTransform.execute(dc, result);
上述のこれら 2 つの手法は、すべての XML 成果物が単一の J2EE デプロイメント単位に ある場合は正常に機能しますが、XML 成果物が複数の J2EE デプロイメント単位に分かれている場合は、 これらすべての XML 成果物に単一の baseURI は存在しないため、失敗します。

複数のデプロイメント単位に 散在する成果物のようなケースをサポートするように XML 成果物のロードを完全に制御するには、 ロードの動作を完全に制御するリゾルバーを使用します。

手順

適切なリゾルバーを実装し、必要に応じてそれらのリゾルバーを静的または動的コンテキストに登録します。

XML 成果物のロードに関するこれらの推奨事項は、J2EE コンテキスト内で、XML 文書 (fn:doc()、document())、 スタイルシート (xsl:include、xsl:import)、解析対象外テキスト、XML スキーマ (XSLT インポート・スキーマ) などのリゾルバーによってサポートされている すべての XML 成果物に当てはまります。


トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_resolvers_j2ee
ファイル名:txml_resolvers_j2ee.html