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.fukurou.util.StringUtil;
019import org.opengion.hayabusa.common.HybsSystem;
020import org.opengion.hayabusa.db.AbstractRenderer;
021import org.opengion.hayabusa.db.CellRenderer;
022import org.opengion.hayabusa.db.DBColumn;
023
024/**
025 * RICHLABEL レンデラは、特殊文字のエスケープを元に戻して表示する場合に
026 * 使用するクラスです。
027 * readonlyのテキストエリアでname属性は付けません。(データは送信されません)
028 * 
029 * 属性値に1以上の数値を指定した場合はSLABELと同等の処理を行います。
030 * その際に、有効なタグについては除外して表示します。(一覧表示用)
031 *
032 * カラムの表示に必要な属性は, DBColumn オブジェクト より取り出します。
033 * このクラスは、DBColumn オブジェクト毎に1つ作成されます。
034 *
035 *  @og.rev 5.9.33.0 (2018/06/01) 新規作成
036 *  @og.rev 5.10.1.0 (2018/06/29) ignoreStrに値を追加。
037 * @og.group データ編集
038 *
039 * @version  5.9.33.0
040 * @author   T.OTA
041 * @since    JDK7.0,
042 */
043public class Renderer_RICHLABEL extends AbstractRenderer {
044        //* このプログラムのVERSION文字列を設定します。   {@value} */
045        private static final String VERSION = "5.9.33.0 (2018/06/01)" ;
046
047        private static final CellRenderer dbCell = new Renderer_RICHLABEL();
048
049        // 2018/06/28 MODIFY pre,img,table,thead,tbody,tr,th,td,ul,li,h[数値]を追加
050        private static final String ignoreStr = "strong|font|a|br|p|span|div|pre|img|table|thead|tbody|tr|th|td|ul|li|h\\d";
051        private final int cutSize;      
052
053        /**
054         * デフォルトコンストラクタ
055         * 
056         * @og.rev 5.10.4.2 (2018/10/19) 出力文字数指定対応
057         */
058        public Renderer_RICHLABEL() {
059                cutSize = 0;    // oota tmp add
060        }
061
062        // oota tmp add start
063        /**
064         * デフォルトコンストラクタ
065         * 
066         * @og.rev 5.10.4.2 (2018/10/19) 出力文字数指定対応
067         * @param clm
068         */
069        public Renderer_RICHLABEL(final DBColumn clm) {
070                String param = clm.getRendererParam();
071                int i;
072                try {
073                        i = Integer.parseInt( param );
074                }
075                catch( Exception e) {
076                        i = 0;
077                }
078                cutSize = i;
079        }
080        // oota tmp add end
081
082        /**
083         * 各オブジェクトから自分のインスタンスを返します。
084         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
085         * まかされます。
086         * 
087         * @og.rev 5.10.4.2 (2018/10/19) 出力文字数指定対応
088         *
089         * @param       clm     DBColumnオブジェクト
090         *
091         * @return      CellEditorオブジェクト
092         */
093        public CellRenderer newInstance( final DBColumn clm ) {
094                // return dbCell;
095                String param = clm.getRendererParam();
096
097                if( param != null && param.length() > 0 ) {
098                        return new Renderer_RICHLABEL(clm);
099                }else {
100                        return dbCell;
101                }
102        }
103
104        /**
105         * データの表示用文字列を返します。
106         * 
107         * @og.rev 5.10.4.2 (2018/10/19) 出力文字数指定対応
108         *
109         * @param   value 入力値
110         *
111         * @return  データの表示用文字列
112         */
113        @Override
114        public String getValue( final String value ) {
115                // 通常
116//              return StringUtil.escapeFilter(value,ignoreStr);
117
118                // タグ情報を除去して、表示する
119                String escapeStr = StringUtil.escapeFilter(value,ignoreStr);
120                if(cutSize <= 0) {
121                        // paramが未設定(0以下)の場合は、リッチラベルをhtmlに表示する形式で返す。
122                        return escapeStr;
123                }else {
124                        // paramに1以上の数値が設定されている場合、SLABELと同様の表示形式で返す。
125                        // 改行は改行コードに変換してお
126                        String trg = StringUtil.tagCut(
127                                        escapeStr
128                                        .replaceAll("(?i)<br[ /]*>",System.lineSeparator())
129                                        );
130
131                        // 簡易的処理。すべてが全角であっても、制限以内である。
132                        // 以下はSLABELと同様の計算式
133                        int len = trg.length();
134                        if( len*2 <= cutSize) { return trg; };
135
136                        int byteSize = 0;
137                        int adrs;
138                        for( adrs=0; adrs<len && byteSize<cutSize; adrs++) {
139                                char ch = trg.charAt(adrs);
140                                if( ch <= 0x7f || ( ch >= 0xff61 && ch <= 0xff9f )) {
141                                        byteSize ++;
142                                }else {
143                                        byteSize += 2;
144                                }
145                        }
146
147                        // 正確にカウントした結果、制限以内であったため。
148                        if( adrs==len && byteSize<=cutSize ) {
149                                return trg;
150                        }
151                        else if( byteSize>cutSize ) {                // オーバーした場合
152                                adrs-- ;
153                        }
154
155                        StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_SMALL );
156                        buf.append("<span title=\"");
157                        buf.append( trg );
158                        buf.append("\">");
159                        buf.append(trg.substring(0,adrs));
160                        buf.append("...</span>");
161
162                        return   buf.toString();
163                }
164        }
165}