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