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.develop;
017
018import java.util.Map;
019import java.util.List;
020
021import org.opengion.fukurou.system.HybsConst ;          // 6.1.0.0 (2014/12/26)
022import org.opengion.fukurou.util.FileInfo;                      // 6.2.0.0 (2015/02/27)
023import org.opengion.fukurou.system.ThrowUtil;                                                   // 6.4.2.0 (2016/01/29)
024import org.opengion.fukurou.xml.JspParserFilter;
025import org.opengion.fukurou.xml.OGDocument;
026import org.opengion.fukurou.xml.OGElement;
027import org.opengion.fukurou.xml.OGNode;
028
029/**
030 * コンストラクタに引数で与えられたマスタデータ情報を元に、特定のJSPタグ情報を生成する基底クラス。
031 * マスタデータ情報はGF92のNMSYORIカラムの種別毎にJspConvertEntityオブジェクトに事前に準備する必要がある。
032 *
033 * 例)
034 * JspConvertEntity e = new JspConvertEntity("RESULT");
035 * e.setTableName("GF92");
036 * e.setColumnName("NMSYORI");
037 *
038 *
039 * 継承先のクラスのexecuteメソッドでは、引数のマスタデータ情報からJSPタグの文字列を生成する処理を実装します。
040 *
041 * @author Takeshi.Takada
042 *
043 */
044public abstract class AbstractJspCreate implements JspParserFilter {
045        /** タブを定数化しておきます。 */
046        public static final String T1 = "\t" ;                          // 5.6.4.4 (2013/05/31) タブを定数化しておきます。
047        public static final String T2 = "\t\t" ;                        // 5.6.4.4 (2013/05/31) タブを定数化しておきます。
048        public static final String T3 = "\t\t\t" ;                      // 5.6.4.4 (2013/05/31) タブを定数化しておきます。
049
050        /** システムの改行コードを設定します。*/
051        protected static final String CR                 = HybsConst.CR;                        // 6.1.0.0 (2014/12/26) refactoring
052        /** StringBilderなどの初期値を設定します。   {@value} */
053        protected static final int BUFFER_MIDDLE = HybsConst.BUFFER_MIDDLE;     // 6.1.0.0 (2014/12/26) refactoring
054
055        // 5.6.1.2 (2013/02/22) 初期値を設定。NAME は、複数のファイル名をCSV形式で与えられるように想定を変更。
056        private final String KEY        ;                               // 6.3.9.1 (2015/11/27)
057        private final String NAMES      ;                               // 6.3.9.1 (2015/11/27)
058
059        /**
060         * タグ名とファイル名を指定するコンストラクター
061         *
062         * タグ名は、og:XXXX , :XXX , XXX いづれの形式でも可能です。部分一致で判断するので、
063         * og固定にしないのであれば、:XXX 形式で指定するのが良いと思います。
064         * ファイル名(names)は、拡張子なしのファイル名(index,query,result,・・・)などになります。
065         * 先に指定しているファイル名と部分一致(大文字小文字は区別)するかどうかで判定します。
066         *
067         * @og.rev 6.3.9.1 (2015/11/27) コンストラクタを用意して、KEY,NAME をセットするように変更します。
068         *
069         * @param       key             タグ名の指定(og:XXXX , :XXX , XXX いづれの形式でも可)
070         * @param       names   処理対象のファイル名(複数のファイル名をCSV形式で指定可能)
071         */
072        protected AbstractJspCreate( final String key , final String names ) {
073                KEY   = key;
074                NAMES = names;
075        }
076
077        /**
078         * ファイル名から、処理対象かどうかを判断します。
079         * ファイル名は、拡張子なしのファイル名(index,query,result,・・・)などになります。
080         * 先に指定しているファイル名と部分一致するかどうかで判定します。
081         *
082         * @og.rev 5.6.1.2 (2013/02/22) NAME は、複数のファイル名をCSV形式で与えられるように変更。
083         *
084         * @param       name    処理対象のファイル名
085         *
086         * @return      処理対象なら true/ そうでなければ、false
087         */
088        protected boolean isExecute( final String name ) {
089                return NAMES.contains( FileInfo.getNAME( name ) );
090        }
091
092        /**
093         * 初期化メソッド
094         *
095         * 内部で使用する JspConvertEntity の List のマップを受け取り、初期化を行います。
096         *
097         * @param       master  JspConvertEntityのリストのマップ
098         */
099        protected abstract void init( final Map<String,List<JspConvertEntity>> master );
100
101        /**
102         * JSPに出力するタグの内容を作成します。
103         * 引数より作成前のタグの属性内容を確認するする事が出来ます。
104         *
105         * @og.rev 5.2.1.0 (2010/10/01) メソッドの引数を、OGAttributes から OGElement に変更します。
106         *
107         * @param       ele     エレメントオブジェクト
108         * @param       nameSpace       このドキュメントのnameSpace( og とか mis とか )
109         *
110         * @return      変換された文字列
111         * @throws Throwable 変換時のエラー
112         */
113        protected abstract String execute( final OGElement ele , final String nameSpace )  throws Throwable ;
114
115        /**
116         * ドキュメントオブジェクト を変換します。
117         *
118         * 引数に null が設定された場合も、正常に処理を行います。(return null とする)
119         * 後続処理を行いたくない場合にも、null を返します。
120         *
121         * @og.rev 5.2.1.0 (2010/10/01) メソッドの引数を、OGAttributes から OGElement に変更します。
122         * @og.rev 6.4.2.0 (2016/01/29) ex.printStackTrace() を、ThrowUtil#ogStackTrace(Throwable) に置き換え。
123         *
124         * @param       doc     処理を行う ドキュメントオブジェクト
125         *
126         * @return      処理した結果の ドキュメントオブジェクト
127         * @see org.opengion.fukurou.xml.JspParserFilter#filter( OGDocument )
128         */
129        @Override
130        public OGDocument filter( final OGDocument doc ) {
131                if( doc == null ) { return doc; }
132                final OGDocument rtndoc = doc;
133
134                final String name = doc.getFilename();
135                if( isExecute( name ) && KEY != null ) {
136                        try {
137                                final String nameSpace = doc.getNameSpace();
138                                final String key = (KEY.indexOf( ':' ) == 0 ) ? nameSpace + KEY : KEY ;
139
140                                final List<OGElement> list = doc.getElementList( key );
141                                for( final OGElement ele : list ) {
142                                        final OGNode newNode = new OGNode( execute( ele,nameSpace ) );          // 5.2.1.0 (2010/10/01)
143                                        rtndoc.changeNode( ele,newNode );
144                                }
145                        } catch( final Throwable th ) {
146                                System.err.println( ThrowUtil.ogStackTrace( th ) );                             // 6.4.2.0 (2016/01/29)
147                        }
148                }
149                return rtndoc;
150        }
151
152        /**
153         * 引数のリストを連結文字列で連結した文字列を作成して返します。
154         *
155         * @param       list    処理を行うドキュメントリスト
156         * @param       sep             連結する区切り文字
157         *
158         * @return      連結された文字列
159         * @og.rtnNotNull
160         */
161        protected String chainChar( final List<String> list , final String sep ){
162                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
163
164                for( final String st : list ) {
165                        if( buf.length() > 0 ) {
166                                buf.append( sep ) ;
167                        }
168                        buf.append( st ) ;
169                }
170                return buf.toString();
171        }
172
173        /**
174         * 指定のListオブジェクトが null でなく、かつ、空でないか、判定する。
175         *
176         * @og.rev 5.2.1.0 (2010/10/01) 新規追加
177         *
178         * @param       list    、判定するListオブジェクト
179         *
180         * @return      nullでなく、かつ、空でない場合、true
181         */
182        protected boolean isNotEmpty( final List<JspConvertEntity> list ) {
183                return list != null && !list.isEmpty() ;
184        }
185}