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)形式書き込みクラスです。
025 * 標準と異なるのは、文字列のみ、ダブルクオート処理を行い、数字型は、ダブルクオートも
026 * ゼロカンマも付けません。
027 *
028 * DefaultTableWriter を継承していますので,ラベル,名前,データの出力部のみ
029 * オーバーライドして,可変長カンマ区切り文字ファイルの出力機能を実現しています。
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 = "5.6.9.4 (2013/10/31)" ;
041
042        /**
043         * DBTableModel から データを作成して,PrintWriter に書き出します。
044         *
045         * @param       writer PrintWriterオブジェクト
046         */
047        @Override
048        public void writeDBTable( final PrintWriter writer )  {
049                super.setSeparator( CSV_SEPARATOR );    // 3.5.6.0 (2004/06/18)
050                super.writeDBTable( writer );
051        }
052
053        /**
054         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
055         * このクラスでは,データを ダブルコーテーション(")で囲みます。
056         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
057         *
058         * @param       table  DBTableModelオブジェクト
059         * @param       writer PrintWriterオブジェクト
060         */
061        @Override
062        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
063                int numberOfRows =      table.getRowCount();
064                boolean useNumber = isUseNumber();
065                boolean useRenderer = isUseRenderer();  // 5.2.1.0 (2010/10/01)
066
067                for( int row=0; row<numberOfRows; row++ ) {
068                        if( useNumber ) {
069                                writer.print( quotation( String.valueOf( row+1 ) ) );
070                                writer.print( CSV_SEPARATOR );
071                        }
072
073                        for( int i=0; i<numberOfColumns; i++ ) {
074                                if( i != 0 ) { writer.print( CSV_SEPARATOR ); }
075                                int clm = clmNo[i];
076                                String val = table.getValue(row,clm);
077                                if( dbType[i] == NVAR ) {
078                                        val = StringUtil.getReplaceEscape( val );
079                                }
080                                // 5.2.1.0 (2010/10/01) useRenderer 対応
081                                else if( useRenderer ) {
082                                        val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) );
083                                }
084
085                                // 数字型には、ダブルクオートは付けません。
086                                if( dbType[i] == NUMBER ) {
087                                                writer.print( val );
088                                }
089                                else {
090                                        writer.print( quotation( val ) );
091                                }
092                        }
093                        writer.println();
094                }
095        }
096
097        /**
098         * データを書き込む場合の,区切り文字をセットします。
099         * このクラスでは,CSV 固定の為,区切り文字のセットは無効になります。
100         *
101         * @param       sprt 区切り文字
102         */
103        @Override
104        public void setSeparator( final String sprt ) {
105                super.setSeparator( CSV_SEPARATOR ) ;   // 3.5.6.0 (2004/06/18)
106        }
107}