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     */
016    package org.opengion.hayabusa.develop;
017    
018    import java.util.Map;
019    import java.util.List;
020    
021    import org.opengion.fukurou.xml.JspParserFilter;
022    import org.opengion.fukurou.xml.OGDocument;
023    import org.opengion.fukurou.xml.OGElement;
024    import org.opengion.fukurou.xml.OGNode;
025    
026    import org.opengion.fukurou.util.FileUtil;
027    
028    /**
029     * コンストラクタに引数で与えられたã?スタãƒ??タæƒ??をå?に、特定ã?JSPタグæƒ??を生成する基底クラスã€?
030     * マスタãƒ??タæƒ??はGF92のNMSYORIカラãƒ??種別毎にJspConvertEntityオブジェクトに事前に準備するå¿?¦�があるã€?
031     *
032     * ä¾?
033     * JspConvertEntity e = new JspConvertEntity("RESULT");
034     * e.setTableName("GF92");
035     * e.setColumnName("NMSYORI");
036     *
037     *
038     * 継承先ã?クラスのexecuteメソãƒ?ƒ‰では、引数のマスタãƒ??タæƒ??からJSPタグのæ–?­—å?を生成するå?ç�?‚’実è£?�—ますã?
039     *
040     * @author Takeshi.Takada
041     *
042     */
043    public abstract class AbstractJspCreate implements JspParserFilter {
044    
045            /** 出力å?のシスãƒ?ƒ に応じた改行コーãƒ?*/
046            public static final String CR = System.getProperty("line.separator");
047    
048            /** タブを定数化しておきますã? */
049            public static final String T1 = "\t" ;                          // 5.6.4.4 (2013/05/31) タブを定数化しておきますã?
050            public static final String T2 = "\t\t" ;                        // 5.6.4.4 (2013/05/31) タブを定数化しておきますã?
051            public static final String T3 = "\t\t\t" ;                      // 5.6.4.4 (2013/05/31) タブを定数化しておきますã?
052    
053            // 5.6.1.2 (2013/02/22) 初期値を設定ã?NAME は、è¤?•°のファイル名をカンマ区åˆ?‚Šæ–?­—で与えられるよã�?�«想定を変更ã€?
054            protected String KEY  = null;
055            protected String NAME = "";
056    
057            /**
058             * ファイル名からã?処ç�?¯¾象かどã�?�‹を判断しますã?
059             * ファイル名ã?、拡張子なしã?ファイルå�?index,query,result,・・・)などになりますã?
060             * 先にæŒ?®šしてã�?‚‹ファイル名と部åˆ?¸??するかどã�?�‹で判定しますã?
061             *
062             * @og.rev 5.6.1.2 (2013/02/22) NAME は、è¤?•°のファイル名をカンマ区åˆ?‚Šæ–?­—で与えられるよã�?�«変更ã€?
063             *
064             * @param       name    処ç�?¯¾象のファイルå�?
065             *
066             * @return      処ç�?¯¾象なã‚?true/ そうでなければ、false
067             */
068            protected boolean isExecute( final String name ) {
069                    return NAME.contains( FileUtil.getBaseName( name ) ) ;
070            }
071    
072            /**
073             * 初期化メソãƒ?ƒ‰
074             *
075             * å†?ƒ¨で使用する JspConvertEntity の List のマップを受け取り、å?期化を行いますã?
076             *
077             * @param       master  JspConvertEntityのリストã?マッãƒ?
078             */
079            abstract protected void init( final Map<String,List<JspConvertEntity>> master );
080    
081            /**
082             * JSPに出力するタグのå†?®¹を作æ?しますã?
083             * 引数より作æ?前ã?タグの属æ?å†?®¹を確認するする事が出来ますã?
084             *
085             * @og.rev 5.2.1.0 (2010/10/01) メソãƒ?ƒ‰の引数をã?OGAttributes から OGElement に変更しますã?
086             *
087             * @param       ele     エレメントオブジェクãƒ?
088             * @param       nameSpace       こã?ドキュメントã?nameSpace( og とã�?mis とã�?)
089             *
090             * @return      変換された文字å?
091             * @throws Throwable 変換時ã?エラー
092             */
093            abstract protected String execute( final OGElement ele , final String nameSpace )  throws Throwable ;
094    
095            /**
096             * ドキュメントオブジェクãƒ?を変換しますã?
097             *
098             * 引数に null が設定された場合も、正常に処ç�?‚’行いますã?(return null とする)
099             * 後続å?ç�?‚’行いたくなã�??合にもã?null を返しますã?
100             *
101             * @og.rev 5.2.1.0 (2010/10/01) メソãƒ?ƒ‰の引数をã?OGAttributes から OGElement に変更しますã?
102             *
103             * @param       doc     処ç�?‚’行う ドキュメントオブジェクãƒ?
104             *
105             * @return      処ç�?�—た結果の ドキュメントオブジェクãƒ?
106             * @see org.opengion.fukurou.xml.JspParserFilter#filter( OGDocument )
107             */
108            @Override
109            public OGDocument filter( final OGDocument doc ) {
110                    if( doc == null ) { return doc; }
111                    OGDocument rtndoc = doc;
112    
113                    String name = doc.getFilename();
114                    if( isExecute( name ) && KEY != null ) {
115                            try {
116                                    String nameSpace = doc.getNameSpace();
117                                    String key = (KEY.indexOf( ':' ) == 0 ) ? nameSpace + KEY : KEY ;
118    
119                                    List<OGElement> list = doc.getElementList( key );
120                                    for( OGElement ele : list ) {
121                                            OGNode newNode = new OGNode( execute( ele,nameSpace ) );                // 5.2.1.0 (2010/10/01)
122                                            rtndoc.changeNode( ele,newNode );
123                                    }
124                            } catch ( Throwable th ) {
125                                    th.printStackTrace();
126                            }
127                    }
128                    return rtndoc;
129            }
130    
131            /**
132             * 引数のリストを連結文字å?で連結した文字å?を作æ?して返しますã?
133             *
134             * @param       list    処ç�?‚’行うドキュメントリスãƒ?
135             * @param       sep             連結する区åˆ?‚Šæ–?­?
136             *
137             * @return      連結されたæ–?­—å?
138             */
139            protected String chainChar( final List<String> list , final String sep ){
140                    StringBuilder buf = new StringBuilder();
141    
142                    for( String st : list ) {
143                            if( buf.length() > 0 ) {
144                                    buf.append( sep ) ;
145                            }
146                            buf.append( st ) ;
147                    }
148                    return buf.toString();
149            }
150    
151            /**
152             * æŒ?®šã?Listオブジェクトが null でなくã?かつ、空でなã�?�‹、判定するã?
153             *
154             * @og.rev 5.2.1.0 (2010/10/01) 新規追åŠ?
155             *
156             * @param       list    、判定するListオブジェクãƒ?
157             *
158             * @return      nullでなくã?かつ、空でなã�??合ã?true
159             */
160            protected boolean isNotEmpty( final List<JspConvertEntity> list ) {
161                    return ( list != null && !list.isEmpty() ) ;
162            }
163    }