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.plugin.column;
017
018import org.opengion.hayabusa.db.AbstractRenderer;
019import org.opengion.hayabusa.db.CellRenderer;
020import org.opengion.hayabusa.db.DBColumn;
021import static org.opengion.fukurou.util.StringUtil.isNull;
022
023/**
024 * SUBSTR レンデラーは、カラムのデータを特定の文字列の範囲のみに分割するクラスです。
025 *
026 * パラメータに、開始文字列$$終了文字列 を指定すると、最初の開始文字列から
027 * 最後に現れた終了文字列の間のみを抜き出します。
028 *
029 * 開始文字列が未設定や見つからない場合は、文字列の最初から、終了文字列が未設定や
030 * 見つからない場合は、最後まで抜き出し対象とします。
031 *
032 * $$も存在しない場合は、開始文字列のみ指定されたとして処理します。
033 * パラメータに何も指定されなかった場合は、何も加工しません。
034 *
035 * @og.group データ変換
036 *
037 * @og.rev 7.2.6.0 (2020/06/30) 行単位に抜き出す機能追加
038 *
039 * @version  7.2
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK11.0,
042 */
043public class Renderer_SUBSTR extends AbstractRenderer {
044        /** このプログラムのVERSION文字列を設定します。   {@value} */
045        private static final String VERSION = "7.2.6.0 (2020/06/30)" ;
046
047        private static final String SEPARATOR = "$$" ;
048
049        private static final CellRenderer DB_CELL = new Renderer_SUBSTR( "","" ) ;
050
051        // 7.2.6.0 (2020/06/30) 行単位に抜き出す機能追加
052        private final String  stStr ;           // 開始文字列
053        private final String  enStr ;           // 終了文字列
054        private final boolean isUse ;           // 処理するかどうか
055
056        /**
057         * デフォルトコンストラクター
058         *
059         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
060         */
061        public Renderer_SUBSTR() {
062                this( "","" );
063        }
064
065        /**
066         * 必要なパラメータを指定したprivateコンストラクター。
067         *
068         * パラメータで、先頭からスキップする文字列と以降を無視する文字列を指定する。
069         *
070         * @og.rev 7.2.6.0 (2020/06/30) 行単位に抜き出す機能追加
071         *
072         * @param       stStr   開始文字列
073         * @param       enStr   終了文字列
074         */
075        private Renderer_SUBSTR( final String stStr,final String enStr ) {
076                super();
077                this.stStr      = stStr;
078                this.enStr      = enStr;
079                this.isUse      = !isNull( stStr ) || !isNull( enStr );
080        }
081
082        /**
083         * 各オブジェクトから自分のインスタンスを返します。
084         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
085         * まかされます。
086         *
087         * @og.rev 7.2.6.0 (2020/06/30) 行単位に抜き出す機能追加
088         *
089         * @param       clm     DBColumnオブジェクト
090         *
091         * @return      CellRendererオブジェクト
092         * @og.rtnNotNull
093         */
094        public CellRenderer newInstance( final DBColumn clm ) {
095                final String param = clm.getRendererParam();
096                if( isNull( param ) ) {
097                        return DB_CELL;
098                }
099                else {
100                        String  stStr   = "";
101                        String  enStr   = "";
102
103                        final int ad = param.indexOf( SEPARATOR );
104                        if( ad < 0 ) {
105                                stStr = param;
106                        }
107                        else {
108                                stStr = param.substring( 0,ad );
109                                enStr = param.substring( ad+SEPARATOR.length() );
110                        }
111                        return new Renderer_SUBSTR( stStr,enStr );
112                }
113        }
114
115        /**
116         * データの表示用文字列を返します。
117         *
118         * @og.rev 7.2.6.0 (2020/06/30) 行単位に抜き出す機能追加
119         *
120         * @param   value 入力値
121         *
122         * @return  データの表示用文字列
123         * @og.rtnNotNull
124         */
125        @Override
126        public String getValue( final String value ) {
127                final String rtn = ( value == null ) ? "" : value ;
128
129                if( isUse ) {
130                        int st = rtn.indexOf(     stStr );              // 引数が空の文字列の場合も 0
131                        int ed = rtn.lastIndexOf( enStr );
132
133                        if( st < 0 ) { st = 0; }
134                        if( ed < 0 ) { ed = rtn.length(); }
135
136                        return rtn.substring( st,ed );
137                }
138                return rtn ;
139        }
140
141        /**
142         * データ出力用の文字列を作成します。
143         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
144         * データを返します。
145         * 基本は、#getValue( String ) をそのまま返します。
146         *
147         * @param   value 入力値
148         *
149         * @return  データ出力用の文字列
150         * @og.rtnNotNull
151         * @see         #getValue( String )
152         */
153        @Override
154        public String getWriteValue( final String value ) {
155                return getValue( value );
156        }
157}