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