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
020//import org.opengion.fukurou.util.StringUtil;                                                                  // 8.5.2.0 (2023/07/14) Delete
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 = "8.5.2.0 (2023/07/14)" ;
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         * @og.rev 8.5.2.0 (2023/07/14) 一部の機能廃止による修正(問合・トラブル 0200010980)
069         *
070         * @param       table   DBTableModelオブジェクト
071         * @param       writer  PrintWriterオブジェクト
072         */
073        @Override
074        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
075                final int numberOfRows = table.getRowCount();
076                final boolean useNumber = isUseNumber();
077                final boolean useRenderer = isUseRenderer();            // 5.2.1.0 (2010/10/01)
078
079                for( int row=0; row<numberOfRows; row++ ) {
080                        if( useNumber ) {
081//                              writer.print( quotation( String.valueOf( row+1 ) ) );
082                                writer.print( String.valueOf( row+1 ) );        // 7.2.6.1 (2020/07/17) 行番号にダブルクオートは付けません。
083                                writer.print( CSV_SEPARATOR );
084                        }
085
086                        for( int i=0; i<numberOfColumns; i++ ) {
087                                if( i != 0 ) { writer.print( CSV_SEPARATOR ); }
088                                final int clm = clmNo[i];
089                                if( clm < 0 ) { continue; }                                     // 6.0.1.2 (2014/08/08) カラム飛ばし
090
091                                String val = table.getValue(row,clm);
092//                              if( dbType[i] == NVAR ) {                                                                               // 8.5.2.0 (2023/07/14) Delete
093//                                      val = StringUtil.getReplaceEscape( val );                                       // 8.5.2.0 (2023/07/14) Delete
094//                              }                                                                                                                               // 8.5.2.0 (2023/07/14) Delete
095                                // 5.2.1.0 (2010/10/01) useRenderer 対応
096//                              else if( useRenderer ) {                                                                                // 8.5.2.0 (2023/07/14) Modify
097                                if( useRenderer ) {
098                                        // 6.0.4.0 (2014/11/28) データ出力用のレンデラー
099                                        val = dbColumn[clm].getWriteValue( val );
100                                }
101
102                                // 数字型には、ダブルクオートは付けません。
103                                if( dbType[i] == NUMBER ) {
104                                                writer.print( val );
105                                }
106                                else {
107                                        writer.print( quotation( val ) );
108                                }
109                        }
110                        writer.println();
111                }
112        }
113
114        /**
115         * データを書き込む場合の区切り文字をセットします。
116         * このクラスでは、CSV 固定の為、区切り文字のセットは無効になります。
117         *
118         * @param       sprt    区切り文字
119         */
120        @Override
121        public void setSeparator( final String sprt ) {
122                super.setSeparator( CSV_SEPARATOR ) ;                           // 3.5.6.0 (2004/06/18)
123        }
124}