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.html;
017
018 import org.opengion.hayabusa.common.HybsSystem;
019 import org.opengion.hayabusa.common.HybsSystemException;
020 import org.opengion.hayabusa.db.DBTableModel;
021 import org.opengion.fukurou.util.StringUtil;
022 import org.opengion.fukurou.model.Formatter;
023
024 import java.util.regex.Pattern;
025 import java.util.regex.Matcher;
026
027 /**
028 * [PN],[OYA] などの [] でæŒ?®šされたカラãƒ?�§表されたフォーマットデータに対してã€?
029 * DBTableModelオブジェクトを適用して å�?‚«ラãƒ?�«実データを割り当てるオブジェクトですã?
030 *
031 * 特に、[XXXX]に対して、[#XXXX]、[$XXXX]、[$XXXX]などの特殊記号が使用できますã?
032 * 特殊記号の解釈ã?、HTMLFormatTextField系とHTMLFormatTable系で異なりますã?で
033 * ご注意くã�?�•ã�??
034 *
035 * @og.rev 3.5.4.0 (2003/11/25) 新規追åŠ?
036 * @og.group 画面表示
037 *
038 * @version 4.0
039 * @author Kazuhiko Hasegawa
040 * @since JDK5.0,
041 */
042 public class TableFormatter {
043
044 /** フォーマットタイプã?æŒ?®šã?特殊なマã?ク {@value} */
045 public static final String HYBS_ITD_MARKER = "h_itd_marker";
046 private static final Pattern ptnKey = Pattern.compile( "[ \t]+</td" ); // 4.3.2.0 (2008/09/10)
047
048 private FormatterType formatType = null;
049 private int[] location = null;
050 private String[] format = null;
051 private String formatTag = null;
052 private String rowspan = " rowspan=\"2\"";
053 private String trTag = null;
054 private boolean noClass = false;
055 // 3.5.6.0 (2004/06/18) '!' 値のみ 追åŠ?既存ã? '$' は、レンãƒ?ƒ©ー
056 private char[] type = null; // '#':ラベルのみ '$':レンãƒ?ƒ©ー '!':値のみ そã?ä»?通常
057 private String usableKey = null; // キーæƒ??のカラãƒ?–‡字å?
058 private int usableKeyNo = -1; // キーæƒ??のカラãƒ?•ª号
059 private String usableList = "1" ;
060
061 private String keyBreakClm = null; // 5.7.6.3 (2014/05/23) キーブレイクをチェãƒ?‚¯するカラãƒ?D
062 private int breakClmNo = -1; // 5.7.6.3 (2014/05/23) キーブレイクカラãƒ?•ª号
063 private String breakVal = null; // 5.7.6.3 (2014/05/23) キーブレイクをチェãƒ?‚¯する値
064
065 private String itdBody = ""; // 3.5.6.0 (2004/06/18) 追åŠ?
066 private Formatter formatter = null;
067
068 /**
069 * フォーマットをセãƒ?ƒˆしますã?
070 * フォーマットにã€?lt;table>を含ã‚??合ã?TextField扱ã�?�ªので、フォーマットå?割
071 * しません。table を含まずã?tr を含ã‚??合ã?ã€?¼‘行å?のãƒ??タとして扱ã�?‚ºã€?
072 * trTag を求めますã?
073 * trTag と format との間に、行ã?ãƒ?ƒ€ーがå?りますã?
074 * Tomcat6では、JSPのパã?ス時に、tabやspaceはそã?ままパã?スされるためã?</td>å‰?
075 * のスペã?ス削除処ç�?‚‚行いますã?
076 *
077 * @og.rev 4.3.2.0 (2008/09/10) </td>前ã?スペã?スを取り消しますã?
078 * @og.rev 5.5.0.3 (2012/03/13) <tr>を取らなã�?ƒ•ラグ追åŠ?
079 *
080 * @param fmt [カラãƒ?��] 形式ã?フォーマットデータ
081 * @param flg falseにすると先é?のtrタグを取るå?ç�?‚’行いません(5.5.0.3)
082 */
083 public void setFormat( final String fmt , final boolean flg ) {
084 int tbl = fmt.indexOf( "<table" );
085 int str = fmt.indexOf( "<tr" );
086
087 // tr を含み、かつ、tableを含まなã�?�‹、含んでã‚?tr の後ろにtableがあるå?合ã?
088 if( str >= 0 && ( tbl < 0 || str < tbl ) && flg ) { // 5.5.0.3(2012/03/13)
089 int end = fmt.indexOf( '>',str );
090 formatTag = fmt.substring(end+1);
091 trTag = fmt.substring(0,end+1) ;
092 }
093 else {
094 formatTag = fmt;
095 trTag = null;
096 }
097 // 4.3.2.0 (2008/09/10) </td>前ã?スペã?スを取り消すã€?
098 Matcher matcher = ptnKey.matcher( formatTag );
099 formatTag = matcher.replaceAll( "</td" );
100
101 }
102
103 /**
104 * フォーマットをセãƒ?ƒˆしますã?
105 * フォーマットにã€?lt;table>を含ã‚??合ã?TextField扱ã�?�ªので、フォーマットå?割
106 * しません。table を含まずã?tr を含ã‚??合ã?ã€?¼‘行å?のãƒ??タとして扱ã�?‚ºã€?
107 * trTag を求めますã?
108 * trTag と format との間に、行ã?ãƒ?ƒ€ーがå?りますã?
109 * Tomcat6では、JSPのパã?ス時に、tabやspaceはそã?ままパã?スされるためã?</td>å‰?
110 * のスペã?ス削除処ç�?‚‚行いますã?
111 *
112 * @og.rev 5.5.0.3 (2012/03/13) 引数追åŠ?�«つきã?
113 *
114 * @param fmt [カラãƒ?��] 形式ã?フォーマットデータ
115 */
116 public void setFormat( final String fmt ) {
117 setFormat( fmt , true );
118 }
119
120 /**
121 * フォーマットを取得しますã?
122 *
123 * @og.rev 3.5.5.8 (2004/05/20) 新規追åŠ?
124 * @og.rev 5.1.7.0 (2010/06/01) サニタイズ戻しå?ç�?"\\]\\"から"["に戻ã�?を追åŠ?
125 *
126 * @return フォーマットデータ
127 */
128 public String getFormat() {
129 if( trTag != null ) {
130 return decodeSanitizedStr( trTag + formatTag );
131 }
132 else {
133 return decodeSanitizedStr( formatTag );
134 }
135 }
136
137 /**
138 * DBTableModelを利用して、フォーマットデータをå?期化しますã?
139 *
140 * @og.rev 3.5.5.0 (2004/03/12) [KEY.カラãƒ?��] 機è?追åŠ?
141 * @og.rev 3.5.5.2 (2004/04/02) [I] で、行番号を作æ?しますã?
142 * @og.rev 3.5.6.0 (2004/06/18) '!' 値のみ 追åŠ?既存ã? '$' は、レンãƒ?ƒ©ー
143 * @og.rev 3.6.0.0 (2004/09/17) [ROW.ID] で、行毎ã?チェãƒ?‚¯ボックスのIDを返しますã?
144 * @og.rev 5.1.7.0 (2010/06/01) サニタイズ戻しå?ç�?"\\]\\"から"["に戻ã�?を追åŠ?
145 * @og.rev 5.7.6.3 (2014/05/23) キーブレイクをチェãƒ?‚¯する keyBreakClm 属æ?追åŠ?
146 *
147 * @param table DBTableModelオブジェクãƒ?
148 */
149 public void makeFormat( final DBTableModel table ) {
150 formatter = new Formatter( table );
151 formatter.setFormat( formatTag );
152 location = formatter.getClmNos();
153 format = formatter.getFormat();
154
155 // 5.1.7.0 (2010/06/01) サニタイズ戻しå?ç�?"\\]\\"から"["に戻ã�?を追åŠ?
156 if( format != null ) {
157 for( int i=0; i<format.length; i++ ) {
158 format[i] = decodeSanitizedStr( format[i] );
159 }
160 }
161
162 type = formatter.getType();
163
164 // こã?フォーマットを使用するかどã�?�‹を指定する判定条件の初期設定ですã?
165 if( usableKey != null ) {
166 usableKeyNo = table.getColumnNo( usableKey );
167 }
168
169 // 5.7.6.3 (2014/05/23) キーブレイクをチェãƒ?‚¯する keyBreakClm 属æ?追åŠ?
170 if( keyBreakClm != null ) {
171 breakClmNo = table.getColumnNo( keyBreakClm );
172 breakVal = null; // 初期化しますã?
173 }
174 }
175
176 /**
177 * ãƒ??ブルフォーマットã?タイプをæŒ?®šしますã?
178 * enum FormatterType で、指定しますã?
179 *
180 * @og.rev 4.0.0.0 (2007/05/02) enum 定義に変更
181 *
182 * @param ftype フォーマットã?タイãƒ?
183 */
184 public void setFormatType( final FormatterType ftype ) {
185 formatType = ftype;
186 }
187
188 /**
189 * こã?フォーマットã?タイプを返しますã?
190 *
191 * こã?フォーマットã?タイプを返しますã?
192 *
193 * @og.rev 4.0.0.0 (2007/05/02) enum 定義に変更
194 *
195 * @return こã?フォーマットã?タイプを返しますã?
196 */
197 public FormatterType getFormatType() {
198 return formatType;
199 }
200
201 /**
202 * ãƒ??ブルの rowspan 属æ?をセãƒ?ƒˆしますã?
203 * rowspan は、ã?ãƒ?ƒ€ー部のフォーマットã?行数ですã?初期値は ?’è¡?ですã?
204 * 設定ã?ã€?2" などの、数字部のみをセãƒ?ƒˆしますã?
205 *
206 * @param rowspan 属æ?
207 */
208 public void setRowspan( final String rowspan ) {
209 if( rowspan == null || rowspan.length() == 0 || rowspan.equals( "1" ) ) {
210 this.rowspan = "";
211 }
212 else {
213 this.rowspan = " rowspan=\"" + rowspan + "\"";
214 }
215 }
216
217 /**
218 * 設定された rowspan を返しますã?
219 * これは、フォーマットã?段çµ??数を取りå?しますã?
220 * æ–?—å?としては、rowspan="2" とã�?�†形で取り出しますã?
221 *
222 * @return フォーマット文字å?
223 */
224 public String getRowspan() {
225 return rowspan;
226 }
227
228 /**
229 * ロケーション番号のサイズを返しますã?
230 * フォーãƒ?½�置番号はã€? から getLocationSize()-1 までの数字をæŒ?®šしますã?
231 * ロケーションサイズは、aaa[ABC]bbb[DEF]ccc[GHI]ddd となってã�?‚‹場合ã?
232 * aaa , bbb , ccc , ddd は、フォーマットで、サイズは?”ã?
233 * ABC , DEF , GHI に対応するカラãƒ?•ª号がロケーションで、サイズは?“ã?
234 * こã?メソãƒ?ƒ‰で返すのは、ロケーション番号(??の方ですã?
235 *
236 * @return ロケーション番号のサイズ
237 */
238 public int getLocationSize() {
239 return location.length;
240 }
241
242 /**
243 * カラãƒ??ロケーション番号を返しますã?
244 * 引数はã€? から、getLocationSize()-1 までの数でæŒ?®šしますã?
245 * æŒ?®šã?位置の、フォーマットã?カラãƒ?��に対応するロケーション番号
246 * を返しますã?
247 *
248 * @param no フォーãƒ?½�置番号
249 *
250 * @return ロケーション番号
251 */
252 public int getLocation( final int no ) {
253 return location[no];
254 }
255
256 /**
257 * フォーマット文字å?を返しますã?
258 * 引数はã€? から、getLocationSize() までの数でæŒ?®šしますã?
259 * æŒ?®šã?フォーマットが、aaa[ABC]bbb[DEF]ccc[GHI]ddd となってã�?‚‹場合ã?
260 * aaa , bbb , ccc , ddd を引数 0 , 1 , 2 , 3 で返しますã?
261 *
262 * @param no フォーãƒ?½�置番号
263 *
264 * @return フォーマット文字å?
265 */
266 public String getFormat( final int no ) {
267 return format[no];
268 }
269
270 /**
271 * シスãƒ?ƒ フォーマット文字å?を返しますã?
272 * シスãƒ?ƒ フォーマット文字å?は、[KEY.カラãƒ?��] などの特殊記号でæŒ?®šされた
273 * カラãƒ?��の事で、location には、ã?イナスの値が設定されますã?
274 * マイナスの値に応じて、å?ç�?‚’変えることがå?来ますã?
275 *
276 * [KEY.カラãƒ?��] : 行番号付きカラãƒ?��
277 * [I] : 行番号
278 * [ROW.ID] : 行毎ã?チェãƒ?‚¯ボックスのID
279 * [ROW.JSON] : 行毎ã?全ãƒ??タのJavaScriptオブジェクト形å¼?
280 *
281 * @og.rev 3.5.5.0 (2004/03/12) [KEY.カラãƒ?��] 機è?追åŠ?
282 * @og.rev 3.5.5.2 (2004/04/02) [I] で、行番号を作æ?しますã?
283 * @og.rev 3.6.0.0 (2004/09/17) [ROW.ID] で、行毎ã?チェãƒ?‚¯ボックスのIDを返しますã?
284 * @og.rev 4.0.0.0 (2007/05/02) Formatter を使用するように変更
285 *
286 * @param row 行番号
287 * @param loc 位置番号
288 *
289 * @return フォーマット文字å?
290 */
291 public String getSystemFormat( final int row,final int loc ) {
292 if( loc == Formatter.SYS_ROWNUM ) {
293 return String.valueOf( row );
294 }
295 else if( loc == Formatter.SYS_JSON ) {
296 return formatter.getJson( row );
297 }
298
299 String errMsg = "シスãƒ?ƒ フォーマットã?、下記ã?形式しか使用できませんã€?" + loc + "]" + HybsSystem.CR
300 + " : [KEY.カラãƒ?��] : 行番号付きカラãƒ?��" + HybsSystem.CR
301 + " : [I] : 行番号" + HybsSystem.CR
302 + " : [ROW.ID] : 行毎ã?チェãƒ?‚¯ボックスのID" + HybsSystem.CR
303 + " : [ROW.JSON] : 行毎ã?全ãƒ??タのJavaScriptオブジェクト形å¼? ;
304 throw new HybsSystemException( errMsg );
305 }
306
307 /**
308 * タイプ文字å?を返しますã?
309 * タイプとは、[XXX] の記述で、[#XXX] は、XXXカラãƒ??ラベルをã?[$XXX]は、XXXカラãƒ??
310 * レンãƒ?ƒ©ーをã?[!XXX} は、å?のみ取り出す指定を行いますã?
311 * 主に、TextField系のフォーマットとTable系では、意味合いが異なりますã?でã€?
312 * ご注意くã�?�•ã�??
313 *
314 * @param no フォーãƒ?½�置番号
315 *
316 * @return タイプ文字å? '#':ラベルのみ '$':レンãƒ?ƒ©ー '!':値のみ そã?ä»?通常
317 */
318 public char getType( final int no ) {
319 return type[no];
320 }
321
322 /**
323 * 設定された フォーマットã? trタグを返しますã?
324 * これは、trタグにclass属æ?他ã?設定がされてã�?�Ÿ場合に、変換後ã?
325 * æ–?—å?にも反æ˜?�•せる為にå¿?¦�ですã?
326 *
327 * @og.rev 5.1.7.0 (2010/06/01) サニタイズ戻しå?ç�?"\\]\\"から"["に戻ã�?を追åŠ?
328 *
329 * @return フォーマットã? trタグ
330 */
331 public String getTrTag() {
332 if( trTag == null ) { return ""; }
333
334 return decodeSanitizedStr( trTag ) ;
335 }
336
337 /**
338 * カラãƒ??クラスå�?X,S9 など)のセãƒ?ƒˆを行うかどã�?�‹æŒ?®šしますã?
339 *
340 * "true" で、クラス属æ?を設定しません。これã?ã€?¼£?³?³ファイルに書かれてã�?‚‹属æ?ã‚?
341 * 使用しなã�?�“とを意味しますã?
342 * 初期値はã€?false" ですã?
343 *
344 * @param flag クラス名使用の有無(true:使用しなã�?false:使用するã€?
345 */
346 public void setNoClass( final String flag ) {
347 noClass = StringUtil.nval( flag,noClass );
348 }
349
350 /**
351 * カラãƒ??クラスå�?X,S9 など)のセãƒ?ƒˆを行うかどã�?�‹取得しますã?
352 *
353 * "true" で、クラス属æ?を設定しません。これã?ã€?¼£?³?³ファイルに書かれてã�?‚‹属æ?ã‚?
354 * 使用しなã�?�“とを意味しますã?
355 * 初期値はã€?false" ですã?
356 *
357 * @return クラス名使用の有無(true:使用しなã�?false:使用するã€?
358 */
359 public boolean isNoClass() {
360 return noClass;
361 }
362
363 /**
364 * フォーマットã?使用可否を判断するキーとなるカラãƒ?��を指定しますã?
365 *
366 * キーがã?usableList に含まれる場合ã?、このフォーãƒ?‚’使用できますã?
367 * キー(カラãƒ?��)が指定されなã�??合ã?、常に使用されますã?
368 * ※ 現時点では、BODYタイプã?み使用してã�?�¾すã?
369 *
370 * @param key フォーマットã?使用可否を判断するカラãƒ?��
371 */
372 public void setUsableKey( final String key ) {
373 usableKey = key;
374 }
375
376 /**
377 * フォーマットã?使用可否を判断するæ–?—å?リストをæŒ?®šしますã?
378 *
379 * キーがã?こã?æ–?—å?リスト中に存在する場合ã?、このフォーãƒ?‚’使用できますã?
380 * こã?æ–?—å?リストã?、固定なæ–?—å?ですã?{@XXXX}は使用できますが、[XXXX]は
381 * 使用できませんã€?
382 * 初期値はã€?1" ですã?
383 * ※ 現時点では、BODYタイプã?み使用してã�?�¾すã?
384 *
385 * @param list フォーマットã?使用可否を判断するæ–?—å?リスãƒ?
386 * @see TableFormatter#isUse( int,DBTableModel )
387 */
388 public void setUsableList( final String list ) {
389 if( list != null ) {
390 usableList = list;
391 }
392 }
393
394 /**
395 * ここでæŒ?®šしたカラãƒ??値がã?キーブレイクした場合ã?こã?タグを使用しますã?
396 *
397 * キーブレイクで 使用可否を指定する為の機è?ですã?
398 * こã?設定å?は、usableKey,usableList とは、独立してã�?‚‹ため、それぞれで
399 * 有効になれã?、使用されると判断されますã?
400 * キーブレイク判定では、最初ã?1件目は、å¿?�š使用されると判断されますã?
401 *
402 * @og.rev 5.7.6.3 (2014/05/23) 新規追åŠ?
403 *
404 * @param kclm キーブレイクをチェãƒ?‚¯するカラãƒ?D
405 */
406 public void setKeyBreakClm( final String kclm ) {
407 keyBreakClm = kclm;
408 }
409
410 /**
411 * こã?フォーマットを使用するかどã�?�‹の問い合わせを返しますã?
412 *
413 * "true" で、使用しますã?setUsableKey( String ) で、指定された
414 * カラãƒ?��の値がã?setUsableList( String ) でæŒ?®šされたæ–?—å?に含まれてã�?‚Œばã€?
415 * 使用しますã?カラãƒ?��がセãƒ?ƒˆされなã�??合ã?、デフォルトå?("true")が使用されますã?
416 * ※ 現時点では、BODYタイプã?み使用してã�?�¾すã?
417 * カラãƒ??ãƒ??タに、不正なスペã?スがå?るå?合を想定して、trim() してã�?�¾すã?
418 * よって、usableList の値にスペã?スは使用できませんã€?
419 *
420 * 5.7.6.3 (2014/05/23) 以降ã?、keyBreakClm によるキーブレイクチェãƒ?‚¯も追åŠ?�•れましたã€?
421 * 従来の usableKey,usableList とは、独立してã�?‚‹ため、それぞれで有効になれã?ã€?
422 * 使用されると判断されますã?
423 *
424 * @og.rev 3.5.6.2 (2004/07/05) 判定評価用カラãƒ??値ã‚?trim() しますã?
425 * @og.rev 5.7.6.3 (2014/05/23) キーブレイクをチェãƒ?‚¯する keyBreakClm 属æ?追åŠ?
426 *
427 * @param row 行番号
428 * @param table DBTableModelオブジェクãƒ?
429 *
430 * @return こã?フォーãƒ?‚’使用するかどã�?�‹(true:使用する/false:使用しなã�?
431 * @see TableFormatter#setUsableKey( String )
432 * @see TableFormatter#setUsableList( String )
433 */
434 public boolean isUse( final int row, final DBTableModel table ) {
435 // 5.7.6.3 (2014/05/23) keyBreakClm 属æ?の追åŠ?�§、å?ç�?ƒジãƒ?‚¯の見直ã�?
436 // if( usableKeyNo < 0 ) { return true; }
437 // String val = table.getValue( row,usableKeyNo ).trim();
438 // return val.length() == 0 ? false : usableList.indexOf( val ) >= 0 ;
439
440 // どちらも設定されてã�?�ªければ、使用されã‚?=true)
441 if( usableKeyNo < 0 && breakClmNo < 0 ) { return true; }
442
443 // 以下ã?どちらかは設定されてã�?‚‹ため、true の時点で、使用されã‚?=true)を返すã€?
444 if( usableKeyNo >= 0 ) {
445 String val = table.getValue( row,usableKeyNo ).trim();
446 if( usableList.indexOf( val ) >= 0 ) { return true; }
447 }
448
449 if( breakClmNo >= 0 ) {
450 String val = table.getValue( row,breakClmNo ).trim();
451 if( !val.equals( breakVal ) ) { // 同じでなã�??合ã?、true
452 breakVal = val;
453 return true;
454 }
455 }
456
457 return false ; // æœ?¾Œまで残ると、使用されなã�?�¨判断、false を返すã€?
458 }
459
460 /**
461 * itdフォーマット文字å?を設定しますã?
462 *
463 * itd ボディ部のæ–?—å?を指定しますã?
464 * itd ボディは、繰り返し処ç�?‚’行いますã?これをã?上位ã?ボディæ–?—å?の中の
465 * HYBS_ITD_MARKER æ–?—å? と置き換えますã?
466 *
467 * @og.rev 3.5.6.0 (2004/06/18) itdフォーマット文字å?の取り込み
468 *
469 * @param itd itdフォーマットã?æ–?—å?
470 */
471 public void setItdBody( final String itd ) {
472 if( itd != null ) {
473 itdBody = itd;
474 }
475 }
476
477 /**
478 * itdフォーマット文字å?を取得しますã?
479 *
480 * itd ボディ部のæ–?—å?を取得しますã?
481 * itd ボディは、繰り返し処ç�?‚’行いますã?これをã?上位ã?ボディæ–?—å?の中の
482 * HYBS_ITD_MARKER æ–?—å? と置き換えますã?
483 *
484 * @og.rev 3.5.6.0 (2004/06/18) itdフォーマット文字å?の取り込み
485 *
486 * @return itdフォーマットã?æ–?—å?
487 */
488 public String getItdBody() {
489 return itdBody;
490 }
491
492 /**
493 * サニタイズの戻ã�?"\\]\\"から"["に戻ã�?を行いますã?
494 *
495 * @og.rev 5.1.7.0 (2010/06/01) 新規作æ?
496 *
497 * @param str サニタイズされた文字å?
498 *
499 * @return サニタイズ戻しå?ç�?¾Œã?æ–?—å?
500 */
501 private String decodeSanitizedStr( final String str ) {
502 if( str != null && str.indexOf( "\\]\\" ) >= 0 ) {
503 return str.replace( "\\]\\", "[" );
504 }
505 else {
506 return str;
507 }
508 }
509 }