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; 017package org.opengion.fukurou.util; 018 019 020/** 021 * DBCell で共通的に使用される フォーマッタークラスです。 022 * フォーマットは、$1,$2,$3,$4・・・$9という文字列を含んだ入力テキストです。 023 * これに、AAA:BBB:CCC:DDD という値(value)を、コロン(:)で分割し、 024 * おのおのの、$1,$2,$3,$4 に割り当てなおして、文字列を合成します。 025 * また、$1 は、本来の値として使用しますので、getValut()メソッドで、 026 * 取り出せるようになっています。 027 * さらに、元の文字列"AAA:BBB:CCC:DDD"は、$0 に割り当てられます。割り当てが 028 * ない変数は、""(ゼロ文字列)として、扱われます。 029 * 030 * @og.rev 3.4.0.2 (2003/09/05) 新規作成 031 * @og.rev 5.2.2.0 (2010/11/01) パッケージ移動(hayabusa.html ⇒ fukurou.util) 032 * @og.group データ表示 033 * @og.group データ編集 034 * 035 * @version 4.0 036 * @author Kazuhiko Hasegawa 037 * @since JDK5.0, 038 */ 039public class StringFormat { 040 private static final String[] FROM = new String[] { "$1","$2","$3","$4","$5","$6","$7","$8","$9" } ; 041 /** 初期セパレータ {@value} */ 042 public static final char SEPARATOR = ':' ; 043 private final String inText ; 044 private final String inValue ; 045 private final String inName; // 4.3.4.0 (2008/12/01) $Cの置換え追加 046 private String outText = null; 047 private String outValue = null; 048 049// /** 050// * コンストラクター 051// * テキストとコロン(:)で区切られた引数を指定してオブジェクトを構築します。 052// * テキストには、$1,$2,$3,$4・・・$9という文字列を含んだ入力テキストです。 053// * 値は、コロン(:)で区切られて、$1,$2等に順番に割り当てられます。 054// * 055// * @param text $1,$2,$3,$4・・・$9という文字列を含んだ入力テキスト 056// * @param value コロン(:)で区切られた引数(AAA:BBB:CCC:DDD) 057// */ 058// public StringFormat( final String text, final String value ) { 059// inText = text; 060// inValue = value; 061// } 062 063 /** 064 * コンストラクター 065 * テキストとコロン(:)で区切られた引数を指定してオブジェクトを構築します。 066 * テキストには、$1,$2,$3,$4・・・$9という文字列を含んだ入力テキストです。 067 * 値は、コロン(:)で区切られて、$1,$2等に順番に割り当てられます。 068 * nameは$Cで置き換える文字列です。 069 * 070 * @og.rev 4.3.4.0 (2008/12/01) $C対応追加 071 * 072 * @param text $1,$2,$3,$4・・・$9という文字列を含んだ入力テキスト 073 * @param value コロン(:)で区切られた引数(AAA:BBB:CCC:DDD) 074 * @param name $Cと置き換える文字列 075 */ 076 public StringFormat( final String text, final String value, final String name ) { 077 inText = text; 078 inValue = value; 079 inName = name; 080 } 081 082 /** 083 * フォーマット変換を行い結果を返します。 084 * 変換時に、$1,$2・・・等に割り当てられない変数には、ゼロ文字列("")が割り当てられます。 085 * 086 * @og.rev 3.8.8.2 (2007/01/26) 自分自身を、$0 に割り当てる。 087 * @og.rev 4.3.4.0 (2008/12/01) $Cの置換え追加 088 * @og.rev 5.9.27.0 (2017/12/01) 6.8.2.4対応分 FROM配列($1〜$9)の制約で、イベント名が9個しか使えなかったのを修正します。 089 * 090 * @return フォーマット変換結果 091 */ 092 public String format() { 093 094 final String[] to; 095 if( inValue != null && inValue.indexOf( SEPARATOR ) >= 0 ) { 096 to = StringUtil.csv2Array( inValue, SEPARATOR ); 097 } 098 else { 099 to = new String[] { inValue }; 100 } 101 102 String newText = inText; 103 final int toLen = Math.min( FROM.length , to.length ); // 5.9.27.0 (2017/12/01) ループを回す際に、少ないほうで回します。 104 int i = 0; 105// for( ; i < to.length; i++ ) { 106 for( ; i<toLen; i++ ) { 107 newText = StringUtil.replace( newText, FROM[i], to[i] ); 108 } 109 for( ; i < FROM.length; i++ ) { 110 newText = StringUtil.replace( newText, FROM[i], "" ); 111 } 112 113 // 3.8.8.2 (2007/01/26) 自分自身を、$0 に割り当てる。 114 newText = StringUtil.replace( newText, "$0", inValue ); 115 116 // 4.3.4.0 (2008/12/01) $Cの置換え 117 newText = StringUtil.replace( newText, "$C", inName ); 118 119 outValue = to[0]; 120 outText = newText; 121 return outText; 122 } 123 124 /** 125 * 第一引数($1に相当)を返します。 126 * 引数はコロン(:)で区切られて渡されています。内部で使用する本当の引数は 127 * 第一引数です。これは、フォーマット時の$1に割り当てられます。 128 * フォーマット変換前に取得すると、null が返ります。 129 * 130 * @return 第一引数($1に相当) 131 */ 132 public String getValue() { 133 return outValue; 134 } 135 136 /** 137 * フォーマット変換結果を返します。 138 * これは、#format() 処理を実行した結果を内部でキャッシュしています。 139 * 何度も結果だけを取得したい場合に使用します。(変換処理は実行しません) 140 * フォーマット変換前に取得すると、null が返ります。 141 * 142 * @return フォーマット変換結果 143 */ 144 public String getText() { 145 return outText; 146 } 147 148 /** 149 * 第一引数($1に相当)を返します。 150 * 引数はコロン(:)で区切られて渡されています。内部で使用する本当の引数は 151 * 第一引数です。これは、フォーマット時の$1に割り当てられます。 152 * フォーマット変換前に取得すると、null が返ります。 153 * 154 * @og.rev 5.9.8.2 (2016/05/16) 6.4.5.3の反映。簡易処理用の static メソッド作成 155 * 156 * @param oldVal 元の値 157 * @return 第一引数($1に相当) 158 */ 159 public static String getValue( final String oldVal ) { 160 if( oldVal != null ) { 161 final int ad = oldVal.indexOf( ':' ); 162 return ad < 0 ? oldVal : oldVal.substring( 0,ad ); 163 } 164 return oldVal; 165 } 166}