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;
020import org.opengion.hayabusa.db.DBTableModel;
021import org.opengion.hayabusa.resource.GUIInfo;
022import org.opengion.fukurou.util.XHTMLTag;
023import org.opengion.fukurou.util.StringUtil;
024
025import static org.opengion.fukurou.util.StringUtil.nval ;
026
027import java.io.ObjectOutputStream;
028import java.io.ObjectInputStream;
029import java.io.IOException;
030
031/**
032 * フレームを作成するHTML拡張タグで、引数の受け渡しが可能です。
033 *
034 * @og.formSample
035 * ●形式:<og:frame src="…" name="…" />
036 * ●body:なし
037 *
038 * ●Tag定義:
039 *   <og:frame
040 *       src              ○【HTML】フレームに表示するソースファイルを指定します(必須)。
041 *       name             ○【HTML】フレーム名を指定します(例:QUERY,RESULT,CONTENTS など)(必須)。
042 *       changeGamen        【TAG】ソース指定の画面を変更します
043 *       keys               【TAG】引数を指定します
044 *       dbTable            【TAG】前ページで選択したデータ列の情報を次のページに渡すかどうか[true/false]を指定します(初期値:false)
045 *       longdesc           【HTML】フレームに関する詳しい説明のあるURL(lobgdesc)を指定します
046 *       marginwidth        【HTML】フレームの左右余白サイズ(marginwidth)を指定します
047 *       marginheight       【HTML】フレームの上下余白サイズ(marginheight)を指定します
048 *       noresize           【HTML】フレームサイズを変更できないよう(noresize)に指定します
049 *       scrolling          【HTML】スクロールバー(scrolling)の表示/非表示[auto/yes/no]を指定します(初期値:auto)
050 *       frameborder        【HTML】フレームの境界線(frameborder)の[0:非表示/1:表示]を指定します(初期値:1)
051 *       id                 【HTML】要素に対して固有の名前(id)をつける場合に設定します
052 *       clazz              【HTML】要素に対して class 属性を設定します
053 *       title              【HTML】要素に対する補足的情報(title)を設定します
054 *       style              【HTML】この要素に対して適用させるスタイルシート(style)を設定します
055 *       caseKey            【TAG】このタグ自体を利用するかどうかの条件キーを指定します(初期値:null) 5.7.7.2 (2014/06/20)
056 *       caseVal            【TAG】このタグ自体を利用するかどうかの条件値を指定します(初期値:null) 5.7.7.2 (2014/06/20)
057 *       caseNN             【TAG】指定の値が、null/ゼロ文字列 でない場合(Not Null=NN)は、このタグは使用されます(初期値:true) 5.7.7.2 (2014/06/20)
058 *       caseNull           【TAG】指定の値が、null/ゼロ文字列 の場合は、このタグは使用されます(初期値:true) 5.7.7.2 (2014/06/20)
059 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
060 *   />
061 *
062 * ●使用例:
063 *    ・一般的な例:フレーム分割する構文は、HTML準拠。リクエスト変数は各フレームまで転送されます。
064 *    <frameset>
065 *        <og:frame marginheight="2" marginwidth="2" src="query.jsp"   name="QUERY"  />
066 *        <og:frame marginheight="2" marginwidth="2" src="forward.jsp" name="RESULT" />
067 *    </frameset>
068 *
069 *    ・DBTableModel の値(例ではPN)を、取り出して、リクエスト変数として利用します。
070 *      現状では、commonForward タグ の useTableData="true" dbkeys="{@dbkeys}" 属性を利用します。
071 *    <frameset>
072 *        <og:frame marginheight="2" marginwidth="2" src="query.jsp"   name="QUERY"  keys="PN" dbTable="true" />
073 *        <og:frame marginheight="2" marginwidth="2" src="forward.jsp" name="RESULT" keys="PN" dbTable="true" />
074 *    </frameset>
075 *
076 *    ・changeGamen 属性を利用して、ソース指定の画面を切り替えます。
077 *      たとえば、jsp/index.jsp では、GAMENID属性がURLに存在する場合、直接その画面を
078 *      表示させることができます。
079 *    <frameset cols="160,*,0" frameborder="1" framespacing="1">
080 *        <og:frame marginheight="2" marginwidth="2" src="menu/menu.jsp"    name="MENU" />
081 *        <og:frame marginheight="2" marginwidth="2" src="GE0000/index.jsp" name="CONTENTS"
082 *                                                    changeGamen="{@GAMENID}" />
083 *    </frameset>
084 *
085 * @og.group 画面部品
086 *
087 * @version  4.0
088 * @author       Kazuhiko Hasegawa
089 * @since    JDK5.0,
090 */
091public class FrameTag extends HTMLTagSupport {
092        //* このプログラムのVERSION文字列を設定します。   {@value} */
093        private static final String VERSION = "4.2.1.1 (2008/04/30)" ;
094
095        private static final long serialVersionUID = 421120080430L ;
096
097        private String                  tableId         = HybsSystem.TBL_MDL_KEY;
098        private String                  changeGmn       = null;
099        private String                  keys            = null;
100        private transient DBTableModel  table           = null;
101        private boolean                 dbTable         = false;
102
103        // 3.5.2.0 (2003/10/20) システムパラメータ の FRAME_UNDER_BAR_REQUEST_KEY_USED を使用。
104        private final boolean UNDER_BAR_KEY_USED = HybsSystem.sysBool( "FRAME_UNDER_BAR_REQUEST_KEY_USED" );
105
106        /**
107         * タグリブオブジェクトをリリースします。
108         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
109         *
110         * @og.rev 2.0.0.4 (2002/09/27) カスタムタグの release() メソッドを、追加
111         * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。
112         *
113         */
114        @Override
115        protected void release2() {
116                super.release2();
117                tableId         = HybsSystem.TBL_MDL_KEY;
118                changeGmn       = null;         // 4.0.0 (2005/02/28)
119                keys            = null;
120                table           = null;
121                dbTable         = false;
122        }
123
124        /**
125         * リンクを作成します。
126         *
127         * @og.rev 3.5.4.0 (2003/11/25) comand="RENEW" 時には、dbTable 属性は、強制的に false とします。
128         *
129         * @return      リンクタグ文字列
130         */
131        @Override
132        protected String makeTag() {
133                if( changeGmn != null ) { set( "src",changeGmn ); }
134
135                String cmd = getRequest().getParameter( "command" );
136                if( "RENEW".equals( cmd ) ) { dbTable = false; }
137
138                setIdName();
139                String urlEnc = getUrlEncode();
140                return XHTMLTag.frame( getAttributes(),urlEnc );
141        }
142
143        /**
144         * id 属性 / name 属性 セット
145         *
146         * フレーム名は id 属性で登録する(XHTML) 互換性のため、
147         * id 属性と name 属性には同じ値をセットしておく。
148         *
149         */
150        private void setIdName() {
151                String idno     = get( "id" );
152                String name = get( "name" );
153                if( idno == null || idno.length() == 0 ) {
154                        if( name != null && name.length() > 0 ) {
155                                set( "id", name );
156                        }
157                        else {
158                                String errMsg = "id 属性か name 属性のどちらかは登録してください。";
159                                throw new HybsSystemException( errMsg );
160                        }
161                }
162                else {
163                        set( "name", idno );
164                }
165        }
166
167        /**
168         * keys 属性 を元に、request情報より values の値を取り込む。
169         *
170         * keys属性は キー情報がカンマ区切りになっている為,ばらして
171         * values属性の配列に一つづつ設定していきます。
172         *
173         * @og.rev 2.0.0.2 (2002/09/24) 検索結果の値を取り込めていなかったバグを修正。
174         * @og.rev 2.1.1.1 (2002/11/15) 選択行情報を取り込めていなかったバグを修正。
175         * @og.rev 3.4.0.3 (2003/09/10) DBTableModelへのリクエスト情報をURLに連結しないように変更。
176         * @og.rev 4.0.0.0 (2005/01/31) getParameterRows() を使用するように変更
177         *
178         * @return      URLエンコードされた文字列
179         */
180        private String getUrlEncode() {
181                int[] rowNo = getParameterRows();               // 4.0.0 (2005/01/31)
182                int selcount = rowNo.length;    // 4.0.0 (2005/01/31)
183
184                String[] key = (String[])StringUtil.enume2Array( getParameterNames(), new String[0] );
185                String[] dbkey = null;
186
187                int dbcount = 0;
188
189                int recount = 0;
190                for( int i=0; i<key.length; i++ ) {
191                        if( isNormalRequestKey( key[i] ) ) {    // 3.4.0.3 (2003/09/10)
192                                recount++;
193                        }
194                }
195
196                if( keys != null && dbTable && selcount > 0 ) {
197                        dbkey = StringUtil.csv2Array( keys );
198                        dbcount = dbkey.length;
199                }
200
201                String[] val  = new String[ recount + dbcount + selcount ];
202                String[] keyt = new String[ recount + dbcount + selcount ];
203
204                int j = 0;
205                for( int i=0; i<key.length; i++ ) {
206                        if( isNormalRequestKey( key[i] ) ) {    // 3.4.0.3 (2003/09/10)
207                                keyt[j] = key[i];
208                                val[j]  = getRequestValue( key[i] );
209                                j++;
210                        }
211                }
212
213                if( dbTable && dbcount > 0 ) {
214                        table = (DBTableModel)getSessionAttribute( tableId );
215                        if( table != null ) {
216                                for( int i=0; i<dbcount; i++ ) {
217                                        keyt[recount + i] = dbkey[i];
218                                        val[recount +i]  = table.getValue(rowNo[0],table.getColumnNo( dbkey[i] ));
219                                }
220                        }
221                }
222
223                // 4.0.0 (2005/01/31) selected文字配列をrowNo数字配列に変更
224                for( int i=0; i<selcount; i++ ) {
225                        keyt[recount + dbcount + i] = HybsSystem.ROW_SEL_KEY;
226                        val[recount + dbcount + i]      = String.valueOf( rowNo[i] );
227                }
228
229                return XHTMLTag.urlEncode( keyt,val );
230        }
231
232        /**
233         * 【HTML】フレームに表示するソースファイルを指定します。
234         *
235         * @og.tag フレームに表示するソースファイルを指定します。
236         *
237         * @param       src ソースファイル
238         */
239        public void setSrc( final String src ) {
240                set( "src",getRequestParameter( src ) );
241        }
242
243        /**
244         * 【HTML】フレーム名を指定します(例:QUERY,RESULT,CONTENTS など)。
245         *
246         * @og.tag フレーム名を指定します。
247         *
248         * @param       name フレーム名
249         */
250        public void setName( final String name ) {
251                set( "name",getRequestParameter( name ) );
252        }
253
254        /**
255         * 【HTML】フレームに関する詳しい説明のあるURL(lobgdesc)を指定します。
256         *
257         * @og.tag lobgdescを指定します。
258         *
259         * @param       longdesc 詳しい説明のあるURL
260         */
261        public void setLongdesc( final String longdesc ) {
262                set( "longdesc",getRequestParameter( longdesc ) );
263        }
264
265        /**
266         * 【HTML】フレームの左右余白サイズ(marginwidth)を指定します。
267         *
268         * @og.tag フレームの左右余白サイズを指定します。
269         *
270         * @param       marginwidth 左右余白サイズ
271         */
272        public void setMarginwidth( final String marginwidth ) {
273                set( "marginwidth",getRequestParameter( marginwidth ) );
274        }
275
276        /**
277         * 【HTML】フレームの上下余白サイズ(marginheight)を指定します。
278         *
279         * @og.tag フレームの上下余白サイズを指定します。
280         *
281         * @param       marginheight 上下余白サイズ
282         */
283        public void setMarginheight( final String marginheight ) {
284                set( "marginheight",getRequestParameter( marginheight ) );
285        }
286
287        /**
288         * 【HTML】フレームサイズを変更できないよう(noresize)に指定します。
289         *
290         * @og.tag フレームサイズを変更できないように指定します。
291         *
292         * @param       noresize フレームサイズを変更させない場合は、"noresize" を指定します。
293         */
294        public void setNoresize( final String noresize ) {
295                String ns = getRequestParameter( noresize );
296                if( ns != null ) { set( "noresize", "noresize" ); }
297        }
298
299        /**
300         * 【HTML】スクロールバー(scrolling)の表示/非表示[auto/yes/no]を指定します(初期値:auto)。
301         *
302         * @og.tag
303         * auto:必要に応じてスクロールバーを表示(初期値)
304         * yes:常にスクロールバーを表示
305         * no:常にスクロールバーを表示しない
306         *
307         * @param       scrolling  スクロールバーの表示/非表示[auto:自動/yes:常時表示/no:非表示]
308         */
309        public void setScrolling( final String scrolling ) {
310                set( "scrolling",getRequestParameter( scrolling ) );
311        }
312
313        /**
314         * 【HTML】フレームの境界線(frameborder)の[0:非表示/1:表示]を指定します(初期値:1)。
315         *
316         * @og.tag
317         * 0:枠を表示しない
318         * 1:枠を表示する。
319         * 初期値は、1:枠を表示する です。
320         *
321         * @param       frameborder     フレームの境界線[0:枠非表示/1:枠表示]
322         */
323        public void setFrameborder( final String frameborder ) {
324                set( "frameborder",getRequestParameter( frameborder ) );
325        }
326
327        /**
328         * 【TAG】ソース指定の画面を変更します。
329         *
330         * @og.tag
331         * src 指定がデフォルト設定で、changeGamen属性が設定されている
332         * 場合には、この値が優先されます。
333         * changeGamen は、画面IDのみ指定してください。src には、このID+"/index.jsp" が
334         * 追加されます。つまり、changeGamen="{&#064;GAMENID}" という指定をしておけば、
335         * FavoriteLinkTag 等で引数に GAMENID が指定された場合のみ、この属性が有効になり、
336         * src="実画面ID/index.jsp" が指定されたことと同じ結果になります。
337         *
338         * @og.rev 3.1.2.0 (2003/04/07) 画面IDと実画面ディレクトリとの関連見直し(DIRの代りにGAMENIDを推奨)
339         * @og.rev 4.2.1.1 (2008/04/30) 画面切り替えをするのは、アドレスが設定されいる場合に限る
340         * @param       src     置換えソース
341         */
342        public void setChangeGamen( final String src ) {
343                String sc = nval( getRequestParameter( src ),changeGmn );
344                if( sc != null ) {
345                        GUIInfo guiInfo = getGUIInfo( sc );
346                        if( guiInfo != null && guiInfo.getAddress() != null && guiInfo.getAddress().length() > 0 ) { // 見つからない場合は、アクセス不可
347                                changeGmn = guiInfo.getRealAddress( "index.jsp" );
348                        }
349                }
350        }
351
352        /**
353         * 【TAG】引数を指定します。
354         *
355         * @og.tag
356         * URL の引数にセットするキーを カンマ区切りでセットします。
357         *
358         * @param       key 引数
359         */
360        public void setKeys( final String key ) {
361                keys = getRequestParameter( key ) ;
362        }
363
364        /**
365         * 【TAG】前ページで選択したデータ列の情報を次のページに渡すかどうか[true/false]を指定します(初期値:false)。
366         *
367         * @og.tag
368         * ただし、1行分のデータのみです。(複数選択時は、最初の1件目)
369         * true:渡す
370         * false:渡さない。
371         * 初期値は、false:渡さない です。
372         *
373         * @param       db 選択データを次のページに渡すかどうか[true:渡す/false:渡さない]
374         */
375        public void setDbTable( final String db ) {
376                dbTable = nval( getRequestParameter( db ),dbTable );
377        }
378
379        /**
380         * 標準的な リクエスト情報かどうかを判断します。
381         *
382         * これは、引数のキーが、HybsSystem.ROW_SEL_KEY か、
383         * DBTableModel で送信されたキー( キー__番号)形式の場合は
384         * false を返します。
385         * 通常のリクエストキーとして扱いたくない場合の判定に使用します。
386         *
387         * @og.rev 3.4.0.3 (2003/09/10) 新規追加
388         * @og.rev 3.5.2.0 (2003/10/20) システムパラメータ の FRAME_UNDER_BAR_REQUEST_KEY_USED を使用。
389         * @og.rev 3.5.5.0 (2004/03/12) 名前と行番号の区切り記号("__")を、HybsSystem.JOINT_STRING  に変更。
390         *
391         * @param key 判定するキー
392         *
393         * @return 標準的な リクエスト情報かどうか [true:標準的/false:それ以外]
394         */
395        private boolean isNormalRequestKey( final String key ) {
396                return  key != null &&
397                                ! key.equals( HybsSystem.ROW_SEL_KEY ) &&
398                                ( key.indexOf( HybsSystem.JOINT_STRING ) < 0 || UNDER_BAR_KEY_USED );
399        }
400
401        /**
402         * シリアライズ用のカスタムシリアライズ書き込みメソッド
403         *
404         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
405         * @serialData 一部のオブジェクトは、シリアライズされません。
406         *
407         * @param       strm    ObjectOutputStreamオブジェクト
408         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
409         */
410        private void writeObject( final ObjectOutputStream strm ) throws IOException {
411                strm.defaultWriteObject();
412        }
413
414        /**
415         * シリアライズ用のカスタムシリアライズ読み込みメソッド
416         *
417         * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。
418         *
419         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
420         * @serialData 一部のオブジェクトは、シリアライズされません。
421         *
422         * @param       strm    ObjectInputStreamオブジェクト
423         * @see #release2()
424         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
425         * @throws ClassNotFoundException       クラスを見つけることができなかった場合
426         */
427        private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
428                strm.defaultReadObject();
429        }
430
431        /**
432         * このオブジェクトの文字列表現を返します。
433         * 基本的にデバッグ目的に使用します。
434         *
435         * @return このクラスの文字列表現
436         */
437        @Override
438        public String toString() {
439                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
440                                .println( "VERSION"             ,VERSION        )
441                                .println( "tableId"             ,tableId        )
442                                .println( "changeGmn"   ,changeGmn      )
443                                .println( "keys"                ,keys           )
444                                .println( "dbTable"     ,dbTable        )
445                                .println( "Other..."    ,getAttributes().getAttribute() )
446                                .fixForm().toString() ;
447        }
448}