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 */ 016 package org.opengion.plugin.io; 017 018 import java.io.PrintWriter; 019 020 import org.opengion.fukurou.util.StringUtil; 021 import org.opengion.hayabusa.db.DBTableModel; 022 023 /** 024 * カンマ区åˆ?‚Šãƒ?ƒ–ルクォートゼãƒã‚«ãƒ³ãƒžãƒ•ァイル(CSV)形弿›¸ãè¾¼ã¿ã‚¯ãƒ©ã‚¹ã§ã™ã? 025 * 026 * DefaultTableWriter を継承ã—ã¦ã?¾ã™ã?ã§?Œãƒ©ãƒ™ãƒ«?Œåå‰ï¼Œãƒ‡ãƒ¼ã‚¿ã®å‡ºåŠ›éƒ¨ã®ã¿ 027 * オーãƒã?ライドã—ã¦?Œå¯å¤‰é•·ã‚«ãƒ³ãƒžåŒºåˆ?‚Šæ–?—ファイルã®å‡ºåŠ›æ©Ÿè?を実ç¾ã—ã¦ã?¾ã™ã? 028 * 029 * @og.group ファイル出åŠ? 030 * 031 * @version 4.0 032 * @author Kazuhiko Hasegawa 033 * @since JDK5.0, 034 */ 035 public class TableWriter_CSV extends TableWriter_Default { 036 //* ã“ã?プãƒã‚°ãƒ©ãƒ??VERSIONæ–?—å?ã‚’è¨å®šã—ã¾ã™ã? {@value} */ 037 private static final String VERSION = "5.6.9.4 (2013/10/31)" ; 038 039 // 4.3.4.4 (2009/01/01) 040 // /** 041 // * ãƒ?ƒ•ォルトコンストラクター 042 // * 043 // */ 044 // public TableWriter_CSV() { 045 // super(); 046 // } 047 048 /** 049 * DBTableModel ã‹ã‚‰ ãƒ??タを作æ?ã—ã¦,PrintWriter ã«æ›¸ãå?ã—ã¾ã™ã? 050 * 051 * @og.rev 3.1.1.0 (2003/03/28) åŒæœŸãƒ¡ã‚½ãƒ?ƒ‰(synchronized付ã)ã‚’éžåŒæœŸã«å¤‰æ›´ã™ã‚‹ã€? 052 * @og.rev 3.5.4.3 (2004/01/05) 引数㫠PrintWriter ã‚’å—ã‘å–るよã?«å¤‰æ›´ã—ã¾ã™ã? 053 * 054 * @param writer PrintWriterオブジェクãƒ? 055 */ 056 @Override 057 public void writeDBTable( final PrintWriter writer ) { 058 super.setSeparator( CSV_SEPARATOR ); // 3.5.6.0 (2004/06/18) 059 super.writeDBTable( writer ); 060 } 061 062 /** 063 * PrintWriter ã« DBTableModelã®ãƒ??ブルæƒ??を書ãè¾¼ã¿ã¾ã™ã? 064 * ã“ã?クラスã§ã¯?Œãƒ‡ãƒ¼ã‚¿ã‚?ãƒ?ƒ–ルコーãƒ??ション(")ã§å›²ã¿ã¾ã™ã? 065 * PrintWriter ã« DBTableModelã®ãƒ??ブルæƒ??を書ãè¾¼ã¿ã¾ã™ã? 066 * 067 * @og.rev 2.0.0.1 (2002/09/20) å…ˆé?ã? ã§ã‹ã¤æ•°å—タイãƒ?S9)ã§ãªã??åˆã« ' ã‚’å?力ã™ã‚‹ã‚ˆã?«ä¿®æ£ 068 * @og.rev 2.0.0.5 (2002/09/30) å…ˆé?ã? ã§ã‹ã¤æ•°å—タイãƒ?S9 or R)ã§ãªã??åˆã« ' ã‚’å?力ã™ã‚‹ã‚ˆã?«ä¿®æ£ã€? 069 * @og.rev 2.3.1.2 (2003/01/28) ãƒ??タ出力時ã«ã€æ”¹è¡ŒãŒä½™å?ã«å‡ºã•れるç®?‰€ã‚’ä¿®æ£ã€? 070 * @og.rev 3.1.0.0 (2003/03/20) DBColumn ã‹ã‚‰ã€getDbType() ã‚ーを直接å–り出ã? 071 * @og.rev 3.1.1.0 (2003/03/28) åŒæœŸãƒ¡ã‚½ãƒ?ƒ‰(synchronized付ã)ã‚’éžåŒæœŸã«å¤‰æ›´ã™ã‚‹ã€? 072 * @og.rev 3.7.0.2 (2005/02/14) è¡Œç•ªå·æƒ??ã‚’ã?出力ã™ã‚?true)/ã—ãªã?false)を指å®? 073 * @og.rev 3.8.0.1 (2005/06/17) DBTypeã?NVAR ã®å ´åˆã?ã€å?ã®Unicodeã«æˆ»ã—ã¾ã™ã? 074 * @og.rev 5.1.6.0 (2010/05/01) DbType ã®åˆæœŸå€¤(dbType)を利用ã™ã‚‹ã€? 075 * @og.rev 5.2.1.0 (2010/10/01) useRenderer 対å¿? 076 * @og.rev 5.6.9.4 (2013/10/31) æ•°å—åž‹ã§ãªã??åˆã?å…ˆé?ã?0 ã®æ™? 077 * 078 * @param table DBTableModelオブジェクãƒ? 079 * @param writer PrintWriterオブジェクãƒ? 080 */ 081 @Override 082 protected void writeData( final DBTableModel table,final PrintWriter writer ) { 083 int numberOfRows = table.getRowCount(); 084 boolean useNumber = isUseNumber(); 085 boolean useRenderer = isUseRenderer(); // 5.2.1.0 (2010/10/01) 086 087 for( int row=0; row<numberOfRows; row++ ) { 088 if( useNumber ) { 089 writer.print( quotation( String.valueOf( row+1 ) ) ); 090 writer.print( CSV_SEPARATOR ); 091 } 092 093 for( int i=0; i<numberOfColumns; i++ ) { 094 if( i != 0 ) { writer.print( CSV_SEPARATOR ); } 095 int clm = clmNo[i]; 096 String val = table.getValue(row,clm); 097 // if( "NVAR".equals( dbColumn[clm].getDbType()) ) { 098 if( dbType[i] == NVAR ) { 099 val = StringUtil.getReplaceEscape( val ); 100 } 101 // 5.2.1.0 (2010/10/01) useRenderer 対å¿? 102 else if( useRenderer ) { 103 val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) ); 104 } 105 106 // é–‹å§‹æ—¥ãªã©ã® 00000000 ã‚’æ–‡å—å?ã‚¿ã‚¤ãƒ—ã§æ¸¡ã? 107 if( val != null && val.length() > 0 && val.charAt(0) == '0' && 108 // NUMBER_TYPE_LIST.indexOf( dbColumn[clm].getDbType() ) < 0 ) { 109 // dbType[i] == NUMBER ) { 110 dbType[i] != NUMBER ) { // 5.6.9.4 (2013/10/31) æ•°å—åž‹ã§ãªã??åˆã?å…ˆé?ã?0 ã®æ™? 111 writer.print( quotation( "'" + val ) ); 112 } 113 else { 114 writer.print( quotation( val ) ); 115 } 116 } 117 writer.println(); 118 } 119 } 120 121 /** 122 * ãƒ??タを書ãè¾¼ã‚??åˆã?,区åˆ?‚Šæ–?—ã‚’ã‚»ãƒ?ƒˆã—ã¾ã™ã? 123 * ã“ã?クラスã§ã¯?ŒCSV 固定ã?為,区åˆ?‚Šæ–?—ã?ã‚»ãƒ?ƒˆã¯ç„¡åйã«ãªã‚Šã¾ã™ã? 124 * 125 * @og.rev 3.1.1.0 (2003/03/28) åŒæœŸãƒ¡ã‚½ãƒ?ƒ‰(synchronized付ã)ã‚’éžåŒæœŸã«å¤‰æ›´ã™ã‚‹ã€? 126 * 127 * @param sprt 区åˆ?‚Šæ–?? 128 */ 129 @Override 130 public void setSeparator( final String sprt ) { 131 super.setSeparator( CSV_SEPARATOR ) ; // 3.5.6.0 (2004/06/18) 132 } 133 }