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    package 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     */
039    public 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    }