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 java.io.PrintWriter;
019
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.db.DBTableModel;
022
023/**
024 * CSV形式ダブルクォートファイル(CSV)形式書き込みクラスです。
025 * 標準と異なるのは、文字列のみ、ダブルクオート処理を行い、数字型は、ダブルクオートも
026 * ゼロカンマも付けません。
027 *
028 * DefaultTableWriter を継承していますので,ラベル,名前,データの出力部のみ
029 * オーバーライドして,可変長CSV形式ファイルの出力機能を実現しています。
030 *
031 * @og.rev 5.6.9.4 (2013/10/31) 新規作成
032 * @og.group ファイル出力
033 *
034 * @version  4.0
035 * @author       Kazuhiko Hasegawa
036 * @since    JDK5.0,
037 */
038public class TableWriter_CSV3 extends TableWriter_Default {
039        /** このプログラムのVERSION文字列を設定します。   {@value} */
040        private static final String VERSION = "7.2.6.1 (2020/07/17)" ;
041
042        /**
043         * デフォルトコンストラクター
044         *
045         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
046         */
047        public TableWriter_CSV3() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
048
049        /**
050         * DBTableModel から データを作成して,PrintWriter に書き出します。
051         *
052         * @param       writer PrintWriterオブジェクト
053         */
054        @Override
055        public void writeDBTable( final PrintWriter writer )  {
056                super.setSeparator( CSV_SEPARATOR );    // 3.5.6.0 (2004/06/18)
057                super.writeDBTable( writer );
058        }
059
060        /**
061         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
062         * このクラスでは,データを ダブルコーテーション(")で囲みます。
063         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
064         *
065         * @og.rev 6.0.1.2 (2014/08/08) カラム飛ばしできる機能を追加
066         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
067         * @og.rev 7.2.6.1 (2020/07/17) 行番号にダブルクオートは付けません。
068         *
069         * @param       table  DBTableModelオブジェクト
070         * @param       writer PrintWriterオブジェクト
071         */
072        @Override
073        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
074                final int numberOfRows =        table.getRowCount();
075                final boolean useNumber = isUseNumber();
076                final boolean useRenderer = isUseRenderer();    // 5.2.1.0 (2010/10/01)
077
078                for( int row=0; row<numberOfRows; row++ ) {
079                        if( useNumber ) {
080//                              writer.print( quotation( String.valueOf( row+1 ) ) );
081                                writer.print( String.valueOf( row+1 ) );                                // 7.2.6.1 (2020/07/17) 行番号にダブルクオートは付けません。
082                                writer.print( CSV_SEPARATOR );
083                        }
084
085                        for( int i=0; i<numberOfColumns; i++ ) {
086                                if( i != 0 ) { writer.print( CSV_SEPARATOR ); }
087                                final int clm = clmNo[i];
088                                if( clm < 0 ) { continue; }                     // 6.0.1.2 (2014/08/08) カラム飛ばし
089
090                                String val = table.getValue(row,clm);
091                                if( dbType[i] == NVAR ) {
092                                        val = StringUtil.getReplaceEscape( val );
093                                }
094                                // 5.2.1.0 (2010/10/01) useRenderer 対応
095                                else if( useRenderer ) {
096                                        // 6.0.4.0 (2014/11/28) データ出力用のレンデラー
097                                        val = dbColumn[clm].getWriteValue( val );
098                                }
099
100                                // 数字型には、ダブルクオートは付けません。
101                                if( dbType[i] == NUMBER ) {
102                                                writer.print( val );
103                                }
104                                else {
105                                        writer.print( quotation( val ) );
106                                }
107                        }
108                        writer.println();
109                }
110        }
111
112        /**
113         * データを書き込む場合の,区切り文字をセットします。
114         * このクラスでは,CSV 固定の為,区切り文字のセットは無効になります。
115         *
116         * @param       sprt 区切り文字
117         */
118        @Override
119        public void setSeparator( final String sprt ) {
120                super.setSeparator( CSV_SEPARATOR ) ;   // 3.5.6.0 (2004/06/18)
121        }
122}