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 * 089 * @return フォーマット変換結果 090 */ 091 public String format() { 092 093 final String[] to; 094 if( inValue != null && inValue.indexOf( SEPARATOR ) >= 0 ) { 095 to = StringUtil.csv2Array( inValue, SEPARATOR ); 096 } 097 else { 098 to = new String[] { inValue }; 099 } 100 101 String newText = inText; 102 int i = 0; 103 for( ; i < to.length; i++ ) { 104 newText = StringUtil.replace( newText, FROM[i], to[i] ); 105 } 106 for( ; i < FROM.length; i++ ) { 107 newText = StringUtil.replace( newText, FROM[i], "" ); 108 } 109 110 // 3.8.8.2 (2007/01/26) 自分自身を、$0 に割り当てる。 111 newText = StringUtil.replace( newText, "$0", inValue ); 112 113 // 4.3.4.0 (2008/12/01) $Cの置換え 114 newText = StringUtil.replace( newText, "$C", inName ); 115 116 outValue = to[0]; 117 outText = newText; 118 return outText; 119 } 120 121 /** 122 * 第一引数($1に相当)を返します。 123 * 引数はコロン(:)で区切られて渡されています。内部で使用する本当の引数は 124 * 第一引数です。これは、フォーマット時の$1に割り当てられます。 125 * フォーマット変換前に取得すると、null が返ります。 126 * 127 * @return 第一引数($1に相当) 128 */ 129 public String getValue() { 130 return outValue; 131 } 132 133 /** 134 * フォーマット変換結果を返します。 135 * これは、#format() 処理を実行した結果を内部でキャッシュしています。 136 * 何度も結果だけを取得したい場合に使用します。(変換処理は実行しません) 137 * フォーマット変換前に取得すると、null が返ります。 138 * 139 * @return フォーマット変換結果 140 */ 141 public String getText() { 142 return outText; 143 } 144}