スキーマ・リゾルバーの登録
XSchemaResolver インターフェースは実装できます。 また、このインターフェースを XFactory に登録された実装として指定することで、 デフォルトのスキーマ解決動作をオーバーライドすることができます。 このデフォルトのスキーマ解決動作には、registerSchema メソッドを使用して XFactory に登録されたスキーマの インポートの解決、および xsl:import-schema 宣言を使用して XSLT スタイルシートにインポートされた スキーマの解決があります。
このタスクについて
スキーマ内のインポートを解決するデフォルト動作としては、 そのスキーマの基本 URI を使用して、インポートされたスキーマのロケーションを解決します。 XSLT スキーマのインポートのデフォルト動作としては、xsl:import-schema 宣言の 基本 URI を使用して、その宣言に指定されたロケーションを解決します。
手順
XFactory クラスで setSchemaResolver メソッドを使用して、
スキーマ・リゾルバーを登録します。
getSchema メソッドが java.util.List インターフェースの インスタンスを返します。これは、特定の名前空間のスキーマ・コンポーネント定義を 複数の異なるスキーマ文書間で分割できるためです。 getSchema メソッドを使用すると、指定されたすべてのロケーション・ヒントに 関連付けられた特定のターゲット名前空間のスキーマ文書すべてを返すことができます。
例
以下に、スキーマ・リゾルバーを使用する基本的な例を示します。
XFactory factory = XFactory.newInstance();
// Set validating to true.
factory.setValidating(true);
// Create the schema resolver and register it with the factory.
factory.setSchemaResolver(new ASchemaResolver(replacementBase));
// Prepare the stylesheet.
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile));
// Execute the transformation.
Source source = new StreamSource(inputFile);
Result result = new StreamResult(System.out);
executable.execute(source, result);
以下は、XSchemaResolver 実装の基本的な例です。
class ASchemaResolver implements XSchemaResolver
{
String _replacementBase;
public ASchemaResolver(String replacementBase)
{
_replacementBase=replacementBase;
}
// Resolve URI, returning the Source that URI represents.
// Implements the "rebase:" pseudo-scheme.
public List<? extends Source> getSchema(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;
}
}