001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.taglet;
017
018import com.sun.tools.doclets.Taglet;
019import com.sun.javadoc.Tag;
020import java.util.Map;
021import org.opengion.fukurou.util.StringUtil;
022
023/**
024 * ソースコメントから、Javadoc を作成する場合のカスタムタグ情報を作成する
025 * Taglet インターフェースの実装クラスを作成します。
026 * og.formSample タグ(形式サンプル)を処理します。
027 *
028 * @version  4.0
029 * @author   Kazuhiko Hasegawa
030 * @since    JDK5.0,
031 */
032public class TagletTag extends AbstractTaglet {
033
034        private static final String NAME   = "og.tag";
035        private static final String HEADER = "説明:";
036
037        /**
038         * 実行時にドックレットがタグレットを読み込んで使用するには、
039         * そのタグレットが、次のシグニチャでマップ を引数として受け取る、
040         * レジスタ と呼ばれる static メソッドをもっている必要があります。
041         * このメソッドは、タグレット名をキーとして、カスタムタグレットの
042         * インスタンスをマップに追加します。 タグレットをオーバーライドする場合、
043         * 名前の競合を避けるため、新しいタグレットのインスタンスをマップに
044         * 追加する前に、オーバーライドされる側のタグレットをマップから
045         * 削除する必要があります。
046         *
047         * @param tagletMap タグレットマップ
048         */
049        public static void register( final Map<String,Taglet> tagletMap ) {
050           TagletTag tagTag = new TagletTag();
051           Taglet tag = tagletMap.get(NAME);
052           if(tag != null) {
053                   tagletMap.remove(NAME);
054           }
055           tagletMap.put(NAME, tagTag);
056        }
057
058        /**
059         * このカスタムタグの名前を返します。
060         *
061         * @return カスタムタグの名前
062         */
063        public String getName() {
064                return NAME;
065        }
066
067        /**
068         * このカスタムタグのタグ表現を受け取り、
069         * 文字列としての表現を返し、生成されたページに出力します。
070         *
071         * @param tagTag このカスタムタグのタグ表現
072         *
073         * @return このタグの文字列としての表現
074         */
075        public String toString( final Tag tagTag ) {
076                return "<span bgcolor=\"yellow\">"
077                           + StringUtil.htmlFilter( tagTag.text() )
078                           + "</span>";
079        }
080
081        /**
082         * このカスタムタグのタグ表現の配列を受け取り、
083         * 文字列としての表現を返し、生成されたページに出力します。
084         * このタグレットがインラインタグを表す場合、
085         * このメソッドは null を返します。
086         *
087         * @og.rev 5.1.9.0 (2010/08/01) table表記をpre に置き換えます。
088         *
089         * @param tagTags       このカスタムタグを表すタグの配列
090         *
091         * @return このタグの文字列としての表現
092         */
093        public String toString( final Tag[] tagTags ) {
094                if(tagTags.length == 0) {
095                        return null;
096                }
097                StringBuilder result = new StringBuilder();
098                result.append( "<dt><b>" ).append( HEADER ).append( "</b></dt>" );
099                result.append( "<dd><pre> " );
100                for(int i = 0; i < tagTags.length; i++) {
101                        result.append( link(tagTags[i].text()) );
102                }
103                result.append( "</pre></dd>\n" );
104                return result.toString();
105        }
106}