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.hayabusa.taglib;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020
021import java.util.Map;
022import java.util.HashMap;
023import java.io.ObjectOutputStream;
024import java.io.ObjectInputStream;
025import java.io.IOException;
026
027/**
028 * ViewFormTag にパラメーターを渡す為のスーパークラスです。
029 *
030 * ViewForm 関連の各クラスは、特殊・専用化の傾向が強くなりつつあり、
031 * 設定するパラメーターも増えています。これらのパラメータを、共通の
032 * ViewFormインターフェースに設定することは、得策とは考えられない為、
033 * パラメーターを一括して渡すようにします。
034 * ただし、key1=**** val2=**** 的な渡し方では、エラーチェックや自動ドキュメント化
035 * が難しいため、各ViewFormのサブクラスごとに、パラメータクラスを作成し、
036 * それらのスーパークラスとして、最終的には、同一方法で、パラメータオブジェクト
037 * として渡すことにします。
038 *
039 * @og.rev 3.5.4.8 (2004/02/23) 新規作成
040 * @og.group 画面表示
041 *
042 * @version  4.0
043 * @author   Kazuhiko Hasegawa
044 * @since    JDK5.0,
045 */
046public class ViewParamTag extends CommonTagSupport {
047        //* このプログラムのVERSION文字列を設定します。   {@value} */
048        private static final String VERSION = "5.5.5.6 (2012/08/31)" ;
049
050        private static final long serialVersionUID = 555620120831L ;
051
052        private transient Map<String,String> param = null;        // 3.5.6.2 (2004/07/05)
053
054        /**
055         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
056         * 
057         * @og.rev 5.9.10.2 (2016/07/08)  6.4.8.1 debug 属性を、パラメータにセットします。系
058         *
059         * @return      後続処理の指示
060         */
061        @Override
062        public int doEndTag() {
063                debugPrint();           // 4.0.0 (2005/02/28)
064                
065                putParam( "debug" , String.valueOf ( isDebug() ) );             // 5.9.10.2 (2016/07/08)
066                
067                ViewFormTag viewform = (ViewFormTag)findAncestorWithClass( this,ViewFormTag.class );
068                if( viewform == null ) {
069//                      String errMsg = "<b>このタグは、ViewFormTagの内側(要素)に記述してください。</b>";
070                        String errMsg = "<b>" + getTagName() + "タグは、ViewFormTagの内側(要素)に記述してください。</b>";
071                        throw new HybsSystemException( errMsg );
072                }
073
074                viewform.setParam( param );
075
076                return(EVAL_PAGE);
077        }
078
079        /**
080         * タグリブオブジェクトをリリースします。
081         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
082         *
083         */
084        @Override
085        protected void release2() {
086                super.release2();               // 3.5.6.0 (2004/06/18) 追加(抜けていた)
087                param = null;
088        }
089
090        /**
091         * パラメータのMapを初期設定します。
092         *
093         * パラメータのキーと値の初期値をセットしたMapを初期設定します。
094         * 処理のタイミングとして、すでにパラメータ変数は、設定されています。
095         * 一つも設定されていない場合は、param == null なので、引数の初期値マップを
096         * そのまま(コピーして)作成します。
097         * すでに、登録されている場合は、キーが存在しているため、キーの存在しない
098         * データのみ、初期値マップからコピーします。
099         *
100         * @og.rev 5.5.5.6 (2012/08/31) 新規追加
101         *
102         * @param   map         パラメータのMap
103         */
104        protected void initParam( final Map<String,String> map ) {
105                if( param == null ) {
106                        param = new HashMap<String,String>( map );
107                }
108                else {
109                        for ( String key : map.keySet() ) {
110                                if( !param.containsKey( key ) ) {       // キーが存在しなければ、初期化情報を登録する。
111                                        param.put( key,map.get( key ) );
112                                }
113                        }
114                }
115        }
116
117        /**
118         * パラメータのキーと値をセットします。
119         *
120         * パラメータのキーと値をセットします。
121         *
122         * @param   key         キー
123         * @param   value       値
124         */
125        protected void putParam( final String key, final String value ) {
126                if( key != null ) {
127                        if( param == null ) { param = new HashMap<String,String>(); }
128                        param.put( key,value );
129                }
130        }
131
132        /**
133         * シリアライズ用のカスタムシリアライズ書き込みメソッド
134         *
135         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
136         * @serialData 一部のオブジェクトは、シリアライズされません。
137         *
138         * @param       strm    ObjectOutputStreamオブジェクト
139         * @throws IOException  入出力エラーが発生した場合
140         */
141        private void writeObject( final ObjectOutputStream strm ) throws IOException {
142                strm.defaultWriteObject();
143        }
144
145        /**
146         * シリアライズ用のカスタムシリアライズ読み込みメソッド
147         *
148         * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。
149         *
150         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
151         * @serialData 一部のオブジェクトは、シリアライズされません。
152         *
153         * @param       strm    ObjectInputStreamオブジェクト
154         * @see #release2()
155         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
156         * @throws ClassNotFoundException       クラスを見つけることができなかった場合
157         */
158        private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
159                strm.defaultReadObject();
160        }
161
162        /**
163         * このオブジェクトの文字列表現を返します。
164         * 基本的にデバッグ目的に使用します。
165         *
166         * @og.rev 5.2.1.0 (2010/10/01) Map の内容表示方法を変更
167         *
168         * @return このクラスの文字列表現
169         */
170        @Override
171        public String toString() {
172                StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
173
174                rtn.append( "[" ).append( this.getClass().getName() ).append( "]" ).append( HybsSystem.CR );
175//              rtn.append( param     ).append( HybsSystem.CR );
176                if( param != null ) {
177                        for ( Map.Entry<String, String> ent : param.entrySet() ) {
178                                rtn.append( ent.getKey() ).append( "=" ).append( ent.getValue() ).append( HybsSystem.CR );
179                        }
180                }
181
182                return rtn.toString();
183        }
184}