Velocity

Velocityについて

コミュニティ

ドキュメント

ツール

比較

日本語訳について

Case Study: XMLC vs. Velocity − 事例研究: XMLC vs. Velocity

A while ago, a question was asked on a Jakarta Tomcat mailing list about XMLC and Velocity. Bojan Smojver <bojan@binarix.com> gave this thoughtful reply, and gave us permission to present it here on the Velocity site.

少し前に、Jakarta Tomcat メーリングリストで、XMLC と Velocity についての質問が ありました。それに対する、Bojan Smojver < bojan@binarix.com> の興味深い回答を、彼の許可の上でこの Velocity サイトで公開します。


I found this in the tutorial for XMLC:

------------------
XMLC is a Java-based compiler that takes a document written in the
Hypertext Markup Language (HTML) or the eXtensible Markup Language (XML)
and creates Java classes that will faithfully recreate the document. The
resulting Java classes can be used to insert dynamic content into the
document framework at run time. XMLC, therefore, is a wonderful way to
create dynamic HTML or XML documents from Java.
------------------

This sounds awfully like JSP to me, which is reason #1 why someone would
use Velocity.

Although I use Velocity for my web work, Velocity is a generic template
engine, it doesn't really care about the rest of the content. And that's
great!

I use XSLT (sorry Jon, promise to give Anakia another look :-) to
prepare my documents from XML into XHTML (this is not done at run time,
not like Cocoon) and Velocity doesn't get upset much by that (except for
the fact that you can't have '&&' (VTL and) in XSLT without applying a
few tricks) but I've overcome that through Ant's nice replacement
techniques...

Anyway, the first bad point to XMLC goes for the pains of code
generation, which Velocity avoids so neatly.

The second is the actual process of designing the complete solution
which is pictured here:
http://xmlc.enhydra.org/project/aboutProject/index.html

Designer designs the page and then engineer puts in the logic? This
sounds very bad to me. There should be a 'box full of Lego's' designers
can choose their functionality from, not the other way around. If
engineers have to be involved in simple projects, it comes back to
employing engineers to do everything in the first place. And why
wouldn't you use even JSP or ECS in such a case? You're are mucking
around with Java again... The XMLC cycle is just too long and it doesn't
make sense at all - what happens when a designer (by mistake or
intentionally) screws up the id's in the page? All of the engineers code
becomes unusable. Now that's a nice separation between presentation and
functionality!

Second bad point.

An example from my 'production line', which illustrates how Velocity
handles the above. I have a few classes that handle all inquiry forms:
one class handles the fields from the form and stores those into a
database, the other one picks the fields and mails them to designated
e-mail address. These classes are beans and are loaded from the only
servlet I ever use (licensed GPL, but could contain any number of bugs
since I wrote it), which handles ALL Velocity pages: 331 lines long
including comments. Beans have scope and all, just like with JSP's.

Once the first ever form is created and debugged, an imaginary web
designer on my 'production line' would copy and existing Velocity page
from a previous project, change the fields (their names, their number...
whatever) in it and the other content. Once they post the page on the
site, that's it. It just works. They didn't even have to talk to an
engineer to get things done. Now that's MUCH better then XMLC!

I think the biggest problem XMLC, JSP and servlets are facing is more of
a philosophical nature: the document is data, not code - and Velocity
treats it exactly like that. Why would you convert your HTML into a Java
class when you want to send it to the browser as text?

My 2 cents. Velocity rocks!

Bojan


XMLC のチュートリアルでこんな文章を見つけました。

------------------
XMLC は、Hypertext Markup Language(HTML)または eXtensible Markup
Language (XML) で書かれた文書から、忠実にドキュメントを再現する Java
クラスを生成する Java ベースのコンパイラです。出力される Java クラス
は、自動的に動的コンテンツを文書フレームワークに挿入するのに使えます。
したがって、XMLC は Java からダイナミックな HTML や XML文書をつくる
素晴らしい方法です。
------------------

これはJSPに大変似ているように思われますが、このことが Velocity を使う
一番の理由でしょう。

私は Web での作業のために Velocity を使っていますが、Velocity は汎用的
なテンプレートエンジンであり、文書中のVTL以外の部分がどんなものであろう
とほとんど気にしません。そして、そこが素晴らしいのです。

私は文書を XML から XHTML に前もって変換 (Cocoonと違って実行時ではあり
ませんが) するのに XSLT を使っています (ごめんなさい。Anakia も検討して
みます :-))。しかし、そのせいで Velocity の方でおかしくなることはあまり
ありませんでした (XSLT では "&&" (VTL の and)をちょっと細工をしないと使
えないこと以外は)。問題に関しても、Ant のすてきな置換テクニックを使って
解決出来ました。

とにかく、XMLC の欠点として第一に挙げられるのは、コード生成での苦労なの
ですが、Velocity ではとてもうまく回避されています。

第二の欠点は、ソリューション全体を設計する実際の手順です
(下記のページで説明されています)。
http://xmlc.enhydra.org/project/aboutProject/index.html

デザイナがページを設計してから、エンジニアがロジックを記述する?私から
見れば全然ダメっぽいです。デザイナが「レゴブロックでいっぱいの箱」から
自分の機能を選べるべきであり、その逆ではないのです。エンジニアが単純な
プロジェクトに関わらなければならない場合、最初のうちは、エンジニアが何
もかもやる羽目になります。そういう場合、JSP や ECS を使ってしまうのも
無理ありません。そうして、また Java まみれになってしまうのです……。XMLC
サイクルは長すぎるだけでまったく意味がありません ── デザイナが (意図
的であろうがなかろうが) ページの id をおかしくしたらどうなるでしょうか?
エンジニアのコードは全て使えなくなります。プレゼンテーションと機能が、
うまく分離されていますよね(笑)。

これが第二の欠点です。

私の「製造ライン」の例で、Velocity なら上記の問題をうまく解決できることを
示したいと思います。私はすべての問合せフォームを処理するいくつかのクラス
を作りました。1つのクラスはフォームのフィールドを処理し、データベースに
格納します。もう1つのクラスはフィールドのデータを拾って、指定されたメール
アドレスにメールします。これらのクラスは Bean になっていて、ここで使う
唯一の Servlet からロードされます (GPL ライセンスですが、私が作ったもの
なので、バグがたくさんあるかもしれません)。この Servlet は全ての Velocity
ページを処理するもので、コメントも含めて 331行あります。上記の Bean には
スコープなど、JSP と同じものが全てあります。

最初に1個だけフォームを作成・デバッグするだけで、あとは、この「製造ライン」
上の (架空の) Web デザイナが、以前に行ったプロジェクトから既存の Velocity 
ページをコピーし、フィールド (名前、個数……どんなものでもOKです) を変更
して、他のコンテンツにしてくれます。いったん、サイトにページを置けばそれで
おしまいです。これだけで動作します。作業を行うのに、エンジニアと話す必要が
全然ないのです。XMLC よりも断然いいでしょ?

XMLC、JSP、Servlet が直面している最大の問題は、むしろ哲学的な性質のもの
だと思います。つまり、文書はデータであって、コードではないのです。そして
Velocity はまさにそのように処理するのです。テキストとしてブラウザに送り
たい時に、HTML を Java クラスに変換しなくてもよいのです。

やはり、Velocity は素晴らしいです。

Bojan



このドキュメントは、 熊坂祐二 、 高橋達男 、 野瀬直樹 が訳しました。
コメントがある場合は、 report@jajakarta.org までお願いします。
オリジナル英文 Copyright © 1999-2003, The Apache Software Foundation