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 * 加工なしダブルクォート区切り文字指定データの書き出しクラスです。
025 *
026 * DefaultTableWriter を継承して,データの出力部のみオーバーライドして,
027 * データそのものを加工なしで、ダブルコーテーションで処理して出力します。
028 * 本来は、DefaultTableWriter の出力形態のはずですが、過去の互換性との関係で、
029 * なまデータを出力するクラスを、追加作成しました。
030 * 従来の CSV2 は、このクラスを使用してください。
031 *
032 * @og.rev 3.7.0.3 (2005/03/01) 新規作成
033 * @og.group ファイル出力
034 *
035 * @version  4.0
036 * @author       Kazuhiko Hasegawa
037 * @since    JDK5.0,
038 */
039public class TableWriter_Data2 extends TableWriter_Default {
040        //* このプログラムのVERSION文字列を設定します。   {@value} */
041        private static final String VERSION = "5.2.1.0 (2010/10/01)" ;
042
043        /**
044         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
045         *
046         * @og.rev 3.7.0.3 (2005/03/01) 新規作成
047         * @og.rev 3.8.0.1 (2005/06/17) DBTypeが NVAR の場合は、元のUnicodeに戻します。
048         * @og.rev 5.1.6.0 (2010/05/01) DbType の初期値(dbType)を利用する。
049         * @og.rev 5.2.1.0 (2010/10/01) useRenderer 対応
050         *
051         * @param       table  DBTableModelオブジェクト
052         * @param       writer PrintWriterオブジェクト
053         */
054        @Override
055        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
056                int numberOfRows = table.getRowCount();
057                String separator = getSeparator();
058                boolean useNumber = isUseNumber();
059                boolean useRenderer = isUseRenderer();  // 5.2.1.0 (2010/10/01)
060
061                for( int row=0; row<numberOfRows; row++ ) {
062                        if( useNumber ) {
063                                writer.print( quotation( String.valueOf( row+1 ) ) );
064                                writer.print( separator );
065                        }
066
067                        for( int i=0; i<numberOfColumns; i++ ) {
068                                if( i != 0 ) { writer.print( separator ); }
069                                int clm = clmNo[i];
070                                String val = table.getValue(row,clm);
071//                              if( "NVAR".equals( dbColumn[clm].getDbType()) ) {
072                                if( dbType[i] == NVAR ) {
073                                        val = StringUtil.getReplaceEscape( val );
074                                }
075                                // 5.2.1.0 (2010/10/01) useRenderer 対応
076                                else if( useRenderer ) {
077                                        val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) );
078                                }
079
080                                writer.print( quotation( val ) );
081                        }
082                        writer.println();
083                }
084        }
085}