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 * @return 後続処理の指示 058 */ 059 @Override 060 public int doEndTag() { 061 debugPrint(); // 4.0.0 (2005/02/28) 062 ViewFormTag viewform = (ViewFormTag)findAncestorWithClass( this,ViewFormTag.class ); 063 if( viewform == null ) { 064 String errMsg = "<b>" + getTagName() + "タグは、ViewFormTagの内側(要素)に記述してください。</b>"; 065 throw new HybsSystemException( errMsg ); 066 } 067 068 viewform.setParam( param ); 069 070 return EVAL_PAGE ; 071 } 072 073 /** 074 * タグリブオブジェクトをリリースします。 075 * キャッシュされて再利用されるので、フィールドの初期設定を行います。 076 * 077 */ 078 @Override 079 protected void release2() { 080 super.release2(); // 3.5.6.0 (2004/06/18) 追加(抜けていた) 081 param = null; 082 } 083 084 /** 085 * パラメータのMapを初期設定します。 086 * 087 * パラメータのキーと値の初期値をセットしたMapを初期設定します。 088 * 処理のタイミングとして、すでにパラメータ変数は、設定されています。 089 * 一つも設定されていない場合は、param == null なので、引数の初期値マップを 090 * そのまま(コピーして)作成します。 091 * すでに、登録されている場合は、キーが存在しているため、キーの存在しない 092 * データのみ、初期値マップからコピーします。 093 * 094 * @og.rev 5.5.5.6 (2012/08/31) 新規追加 095 * 096 * @param map パラメータのMap 097 */ 098 protected void initParam( final Map<String,String> map ) { 099 if( param == null ) { 100 param = new HashMap<String,String>( map ); 101 } 102 else { 103 for ( String key : map.keySet() ) { 104 if( !param.containsKey( key ) ) { // キーが存在しなければ、初期化情報を登録する。 105 param.put( key,map.get( key ) ); 106 } 107 } 108 } 109 } 110 111 /** 112 * パラメータのキーと値をセットします。 113 * 114 * パラメータのキーと値をセットします。 115 * 116 * @param key キー 117 * @param value 値 118 */ 119 protected void putParam( final String key, final String value ) { 120 if( key != null ) { 121 if( param == null ) { param = new HashMap<String,String>(); } 122 param.put( key,value ); 123 } 124 } 125 126 /** 127 * シリアライズ用のカスタムシリアライズ書き込みメソッド 128 * 129 * @og.rev 4.0.0.0 (2006/09/31) 新規追加 130 * @serialData 一部のオブジェクトは、シリアライズされません。 131 * 132 * @param strm ObjectOutputStreamオブジェクト 133 * @throws IOException 入出力エラーが発生した場合 134 */ 135 private void writeObject( final ObjectOutputStream strm ) throws IOException { 136 strm.defaultWriteObject(); 137 } 138 139 /** 140 * シリアライズ用のカスタムシリアライズ読み込みメソッド 141 * 142 * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。 143 * 144 * @og.rev 4.0.0.0 (2006/09/31) 新規追加 145 * @serialData 一部のオブジェクトは、シリアライズされません。 146 * 147 * @param strm ObjectInputStreamオブジェクト 148 * @see #release2() 149 * @throws IOException シリアライズに関する入出力エラーが発生した場合 150 * @throws ClassNotFoundException クラスを見つけることができなかった場合 151 */ 152 private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException { 153 strm.defaultReadObject(); 154 } 155 156 /** 157 * このオブジェクトの文字列表現を返します。 158 * 基本的にデバッグ目的に使用します。 159 * 160 * @og.rev 5.2.1.0 (2010/10/01) Map の内容表示方法を変更 161 * 162 * @return このクラスの文字列表現 163 */ 164 @Override 165 public String toString() { 166 StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE ); 167 168 rtn.append( "[" ).append( this.getClass().getName() ).append( "]" ).append( HybsSystem.CR ); 169 if( param != null ) { 170 for ( Map.Entry<String, String> ent : param.entrySet() ) { 171 rtn.append( ent.getKey() ).append( "=" ).append( ent.getValue() ).append( HybsSystem.CR ); 172 } 173 } 174 175 return rtn.toString(); 176 } 177}