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.plugin.io;
017
018import org.opengion.fukurou.util.StringUtil;
019import org.opengion.hayabusa.db.DBTableModel;
020
021import java.io.PrintWriter;
022
023/**
024 * Excelでの文字変換関数 =T("値") という文字列で書き出すクラスです。
025 *
026 * DefaultTableWriter を継承して,データの出力部のみオーバーライドして,
027 * 文字列カラム(クラス名VARCHAR2のカラム)に、=T("値") という文字列で出力する
028 * ファイルの出力機能を実現しています。
029 *
030 * @og.rev 3.1.4.1 (2003/04/21) 新規作成
031 * @og.group ファイル出力
032 *
033 * @version  4.0
034 * @author       Kazuhiko Hasegawa
035 * @since    JDK5.0,
036 */
037public class TableWriter_T extends TableWriter_Default {
038        /** このプログラムのVERSION文字列を設定します。   {@value} */
039        private static final String VERSION = "6.4.6.0 (2016/05/27)" ;
040
041        /**
042         * デフォルトコンストラクター
043         *
044         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
045         */
046        public TableWriter_T() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
047
048        /**
049         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
050         *
051         * @og.rev 3.7.0.2 (2005/02/14) 行番号情報を、出力する(true)/しない(false)を指定
052         * @og.rev 3.8.0.1 (2005/06/17) DBTypeが NVAR の場合は、元のUnicodeに戻します。
053         * @og.rev 5.1.6.0 (2010/05/01) DbType の初期値(dbType)を利用する。
054         * @og.rev 5.2.1.0 (2010/10/01) useRenderer 対応
055         * @og.rev 6.0.1.2 (2014/08/08) カラム飛ばしできる機能を追加
056         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
057         * @og.rev 6.4.4.2 (2016/04/01) contains 判定を行う新しいメソッドを使用します。
058         * @og.rev 6.4.6.0 (2016/05/27) isNumber , isDate 追加。
059         *
060         * @param       table  DBTableModelオブジェクト
061         * @param       writer PrintWriterオブジェクト
062         */
063        @Override
064        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
065                final int numberOfRows = table.getRowCount();
066                final String SEPARATOR = getSeparator();
067                final boolean useNumber = isUseNumber();
068                final boolean useRenderer = isUseRenderer();    // 5.2.1.0 (2010/10/01)
069
070                for( int row=0; row<numberOfRows; row++ ) {
071                        if( useNumber ) {
072                                writer.print( String.valueOf( row+1 ) );
073                                writer.print( SEPARATOR );
074                        }
075                        for( int i=0; i<numberOfColumns; i++ ) {
076                                if( i != 0 ) { writer.print( SEPARATOR ); }
077                                final int clm = clmNo[i];
078                                if( clm < 0 ) { continue; }                     // 6.0.1.2 (2014/08/08) カラム飛ばし
079
080                                String val = table.getValue(row,clm);
081                                if( dbType[i] == NVAR ) {
082                                        val = StringUtil.getReplaceEscape( val );
083                                }
084                                // 5.2.1.0 (2010/10/01) useRenderer 対応
085                                else if( useRenderer ) {
086                                        // 6.0.4.0 (2014/11/28) データ出力用のレンデラー
087                                        val = dbColumn[clm].getWriteValue( val );
088                                }
089
090                                // 開始日などの 00000000 を文字列タイプで渡す
091                                // 6.4.4.2 (2016/04/01) contains 判定を行う新しいメソッドを使用します。
092                                if( dbColumn[clm].isNumberType() ) {            // 6.4.6.0 (2016/05/27)
093                                        writer.print( val );
094                                }
095                                else {
096                                        writer.print( excelT( val ) );
097                                }
098                        }
099                        writer.println();
100                }
101        }
102
103        /**
104         * EXCELの文字列変換関数である T を適用します。
105         * 値を、 =T("値") という文字列に変換します。
106         * 値にダブルクオートが存在する場合は、その直前にもう一つダブルクオートを
107         * 入れて、エスケープします。
108         *
109         * @og.rev 3.4.0.1 (2003/09/03) JDK1.3 対応に修正。
110         *
111         * @param        data 元のString文字列
112         *
113         * @return       T関数を適用した文字列
114         * @og.rtnNotNull
115         */
116        private String excelT( final String data ) {
117                if( data == null ) { return "" ; }
118
119                final StringBuilder strBuf = new StringBuilder( BUFFER_MIDDLE )         // 6.1.0.0 (2014/12/26) refactoring
120                        .append( data );
121                int len = data.lastIndexOf( '"' );                              // 6.0.2.5 (2014/10/31) refactoring
122                while( len >= 0 ) {
123                        strBuf.insert( len,"\"" ) ;
124                        len = data.lastIndexOf( '"',len-1 );            // 6.0.2.5 (2014/10/31) refactoring
125                }
126
127                strBuf.insert( 0,"=T(\"" ).append( "\")" );
128
129                return strBuf.toString();
130        }
131}