Show Table of Contents
第8章 Groovy スクリプト
Groovy スクリプト (http://groovy.codehaus.org) のサポートは設定名前空間 (http://www.milyn.org/xsd/smooks/groovy-1.1.xsd) より利用できます。 この名前空間は DOM ベーススクリプトと SAX ベーススクリプトのサポートを提供します。
例8.1 設定例
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">
<g:groovy executeOnElement="xxx">
<g:script>
<!--
//Rename the target fragment element from "xxx" to "yyy"...
DomUtils.renameElement(element, "yyy", true, true);
-->
</g:script>
</g:groovy>
</smooks-resource-list>
Groovy スクリプトを使用する秘訣は次の通りです。
- 適切に名前が付けられた imports 要素を使用してインポートを追加します。 自動的にインポートされるクラスは次の通りです。
org.milyn.xml.DomUtilsorg.milyn.javabean.context.BeanContext。Smooks 1.3 および以降のバージョンでのみ含まれます。org.milyn.javabean.repository.BeanRepositoryorg.w3c.dom.*groovy.xml.dom.DOMCategorygroovy.xml.dom.DOMUtilgroovy.xml.DOMBuilder
- element と適切に名前付けされた変数よりスクリプトは visited 要素を使用できます (要素名にアルファベットと数値のみが使用されている場合、 要素名と同じ変数名でも使用できます)。
- 前に実行 (Execute Before) / 後で実行 (Execute After): デフォルトでは、 スクリプトは visitAfter イベントで実行されます。 executeBefore 属性を
trueに設定し、 visitBefore 上で実行するよう指示します。 - コメント / CDATA スクリプトラッピング: スクリプトに特別な XML 文字が含まれている場合、 スクリプトを
XML CommentまたはCDATAセクションでラッピングできます。
8.1. DOM と SAX の混合を Groovy で使用する
Groovy は DOM とSAX モデルの混合をサポートします。
そのため、 Groovy の DOM ユーティリティを使用してターゲットのメッセージ断片を処理することができます。 SAX フィルターが使用される場合でも DOM 「要素」は Groovy スクリプトによって受け取られます。 これにより、 SAX フィルターを使用する Groovy スクリプトが大変簡単になり、 ストリームされた形式で巨大メッセージを処理する機能も維持されます。
注意事項:
- デフォルトモードでのみ使用できます (executeBefore が
falseである場合)。 executeBefore がtrueに設定されると、 この機能は使用できないため、 Groovy スクリプトは SAXElement のみにアクセスできます。 - DOM 断片を
Smooks.filterSource StreamResultに書き込むには writeFragment が呼び出されなければなりません。 - DOM 構築機能を使用するとパフォーマンスのオーバーヘッドが発生します。 巨大メッセージの処理は行えますが、 処理時間が若干長くなることがあります。 「有用性」とパフォーマンスがトレードオフになります。
8.1.1. DOM と SAX の混合例
下例のような XML メッセージを使用します。
<shopping>
<category type="groceries">
<item>Chocolate</item>
<item>Coffee</item>
</category>
<category type="supplies">
<item>Paper</item>
<item quantity="4">Pens</item>
</category>
<category type="present">
<item when="Aug 10">Kathryn's Birthday</item>
</category>
</shopping>
ショッピングリストの「supplies」カテゴリに 2 つのペンを追加して変更したいとします。 これには、 簡単な Groovy スクリプトを書いて、 メッセージの <category> 要素へ向けます。
カテゴリタイプが「supplies」で項目が「pens」であるカテゴリの <item> 要素上でスクリプトが反復し、 数量の値が 2 増えます。
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">
<core:filterSettings type="SAX" />
<g:groovy executeOnElement="category">
<g:script>
<!--
use(DOMCategory) {
// Modify "supplies": we need an extra 2 pens...
if (category.'@type' == 'supplies') {
category.item.each { item ->
if (item.text() == 'Pens') {
item['@quantity'] = item.'@quantity'.toInteger() + 2;
}
}
}
}
// When using the SAX filter, we need to explicitly write the fragment
// to the result stream...
writeFragment(category);
-->
</g:script>
</g:groovy>
</smooks-resource-list>

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.