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    }