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     * ゼロカンマも付けませんã€?
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     */
038    public class TableWriter_CSV3 extends TableWriter_Default {
039            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
040            private static final String VERSION = "5.6.9.4 (2013/10/31)" ;
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             * @param       table  DBTableModelオブジェクãƒ?
059             * @param       writer PrintWriterオブジェクãƒ?
060             */
061            @Override
062            protected void writeData( final DBTableModel table,final PrintWriter writer ) {
063                    int numberOfRows =      table.getRowCount();
064                    boolean useNumber = isUseNumber();
065                    boolean useRenderer = isUseRenderer();  // 5.2.1.0 (2010/10/01)
066    
067                    for( int row=0; row<numberOfRows; row++ ) {
068                            if( useNumber ) {
069                                    writer.print( quotation( String.valueOf( row+1 ) ) );
070                                    writer.print( CSV_SEPARATOR );
071                            }
072    
073                            for( int i=0; i<numberOfColumns; i++ ) {
074                                    if( i != 0 ) { writer.print( CSV_SEPARATOR ); }
075                                    int clm = clmNo[i];
076                                    String val = table.getValue(row,clm);
077                                    if( dbType[i] == NVAR ) {
078                                            val = StringUtil.getReplaceEscape( val );
079                                    }
080                                    // 5.2.1.0 (2010/10/01) useRenderer 対å¿?
081                                    else if( useRenderer ) {
082                                            val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) );
083                                    }
084    
085                                    // 数字型には、ダブルクオートã?付けませんã€?
086                                    if( dbType[i] == NUMBER ) {
087                                                    writer.print( val );
088                                    }
089                                    else {
090                                            writer.print( quotation( val ) );
091                                    }
092                            }
093                            writer.println();
094                    }
095            }
096    
097            /**
098             * ãƒ??タを書き込ã‚??合ã?,区åˆ?‚Šæ–?­—をセãƒ?ƒˆしますã?
099             * こã?クラスでは?ŒCSV 固定ã?為,区åˆ?‚Šæ–?­—ã?セãƒ?ƒˆは無効になりますã?
100             *
101             * @param       sprt 区åˆ?‚Šæ–?­?
102             */
103            @Override
104            public void setSeparator( final String sprt ) {
105                    super.setSeparator( CSV_SEPARATOR ) ;   // 3.5.6.0 (2004/06/18)
106            }
107    }