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 = "6.0.1.2 (2014/08/08)" ; 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 * @og.rev 6.0.1.2 (2014/08/08) カラム飛ばしできる機能を追加 059 * 060 * @param table DBTableModelオブジェクト 061 * @param writer PrintWriterオブジェクト 062 */ 063 @Override 064 protected void writeData( final DBTableModel table,final PrintWriter writer ) { 065 int numberOfRows = table.getRowCount(); 066 boolean useNumber = isUseNumber(); 067 boolean useRenderer = isUseRenderer(); // 5.2.1.0 (2010/10/01) 068 069 for( int row=0; row<numberOfRows; row++ ) { 070 if( useNumber ) { 071 writer.print( quotation( String.valueOf( row+1 ) ) ); 072 writer.print( CSV_SEPARATOR ); 073 } 074 075 for( int i=0; i<numberOfColumns; i++ ) { 076 if( i != 0 ) { writer.print( CSV_SEPARATOR ); } 077 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 val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) ); 087 } 088 089 // 数字型には、ダブルクオートは付けません。 090 if( dbType[i] == NUMBER ) { 091 writer.print( val ); 092 } 093 else { 094 writer.print( quotation( val ) ); 095 } 096 } 097 writer.println(); 098 } 099 } 100 101 /** 102 * データを書き込む場合の,区切り文字をセットします。 103 * このクラスでは,CSV 固定の為,区切り文字のセットは無効になります。 104 * 105 * @param sprt 区切り文字 106 */ 107 @Override 108 public void setSeparator( final String sprt ) { 109 super.setSeparator( CSV_SEPARATOR ) ; // 3.5.6.0 (2004/06/18) 110 } 111}