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.BufferedReader;
019    import java.io.IOException;
020    
021    import org.opengion.fukurou.util.StringUtil;
022    import org.opengion.hayabusa.common.HybsSystem;
023    import org.opengion.hayabusa.common.HybsSystemException;
024    import org.opengion.hayabusa.db.DBTableModelUtil;
025    import org.opengion.hayabusa.io.AbstractTableReader;
026    
027    /**
028     * æŒ?®šã?区åˆ?‚Š記号(初期値:タブ区åˆ?‚Š)ファイルの読み取りクラスですã?
029     *
030     * 名前?Œデータの入力部のみオーバã?ライドすれã??Œ各種入力フォーマットに合わせた
031     * サブクラスを実現する事が可能ですã?
032     *
033     * @og.group ファイル入åŠ?
034     *
035     * @version  4.0
036     * @author   Kazuhiko Hasegawa
037     * @since    JDK5.0,
038     */
039    public class TableReader_Default extends AbstractTableReader {
040            //* こã?プログラãƒ??VERSIONæ–?­—å?を設定しますã?       {@value} */
041            private static final String VERSION = "5.2.1.0 (2010/10/01)" ;
042    
043            /**
044             * DBTableModel から å�?½¢式ã?ãƒ??タを作æ?して,BufferedReader より読み取りますã?
045             * コメンãƒ?空行を除きã?æœ??の行ã?、å¿?�šé ?›®名がå¿?¦�ですã?
046             * それ以降ã?、コメンãƒ?空行を除きã?ãƒ??タとして読み込んでã�?��ますã?
047             * こã?メソãƒ?ƒ‰は、EXCEL 読み込み時に使用しますã?
048             *
049             * @og.rev 4.0.0.0 (2006/09/31) 新規追åŠ?
050             * @see #isExcel()
051             */
052            @Override
053            public void readDBTable() {
054                    String errMsg = "こã?クラスでは実è£?�•れてã�?�¾せんã€?;
055                    throw new UnsupportedOperationException( errMsg );
056            }
057    
058            /**
059             * DBTableModel から å�?½¢式ã?ãƒ??タを作æ?して,BufferedReader より読み取りますã?
060             * コメンãƒ?空行を除きã?æœ??の行ã?、å¿?�šé ?›®名がå¿?¦�ですã?
061             * それ以降ã?、コメンãƒ?空行を除きã?ãƒ??タとして読み込んでã�?��ますã?
062             *
063             * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
064             * @og.rev 3.5.4.2 (2003/12/15) writer の null チェãƒ?‚¯をå»?­¢しますã?
065             * @og.rev 3.5.4.3 (2004/01/05) 引数に、BufferedReader を受け取ル要に変更しますã?
066             * @og.rev 3.5.4.5 (2004/01/23) カラãƒ?��の外部æŒ?®šを優先して使用するã€?
067             * @og.rev 5.1.6.0 (2010/05/01) readDBTableのエラーチェãƒ?‚¯強åŒ?
068             * @og.rev 5.1.6.0 (2010/05/01) skipRowCountの追åŠ?
069             * @og.rev 5.2.0.0 (2010/09/01) ""で囲われてã�?‚‹ãƒ??タに改行が入ってã�?�Ÿ場合ã?対å¿?
070             * @og.rev 5.2.1.0 (2010/10/01) AbstractTableReader.java と重è¤?�—てã�?‚‹ç®?‰€の対å¿?
071             *
072             * @param   reader BufferedReaderオブジェクãƒ?
073             */
074            @Override
075            public void readDBTable( final BufferedReader reader ) {
076                    try {
077                            String line;
078                            String[] names = null;
079                            int numberOfRows = 0;
080    //                      char  sepa = separator.charAt( 0 );
081                            char  sepa = getSeparator().charAt( 0 );        // 5.2.0.0 (2010/09/01)
082    
083                            boolean nameNoSet = true;
084                            table = DBTableModelUtil.newDBTable();
085    
086                            // 3.5.4.5 (2004/01/23) カラãƒ?��の外部æŒ?®šを優先して使用するã€?
087                            if( columns != null && columns.length() > 0 ) {
088                                    names = StringUtil.csv2Array( columns );
089                                    table.init( names.length );
090                                    setTableDBColumn( names ) ;
091                                    nameNoSet = false;
092                            }
093    
094    //                      int skip = skipRowCount;                                                // 5.1.6.0 (2010/05/01)
095                            int skip = getSkipRowCount();                                   // 5.2.0.0 (2010/09/01)
096                            while((line = reader.readLine()) != null) {
097                                    // 5.2.0.0 (2010/09/01) ""で囲われてã�?‚‹ãƒ??タに改行が入ってã�?�Ÿ場合ã?対å¿?
098    //                              int quotCount = StringUtil.countChar( line, '"' );
099    //                              if( quotCount % 2 == 1 ) {
100    //                                      String addLine = null;
101    //                                      while(quotCount % 2 == 1 && (addLine = reader.readLine()) != null) {
102    //                                              line +=  HybsSystem.CR + addLine;
103    //                                              quotCount += StringUtil.countChar( addLine, '"' );
104    //                                      }
105    //                              }
106                                    // 5.2.1.0 (2010/10/01) findbugs 対ç­?æ–?­—å?の + 連結と、å¥?•°判定ロジãƒ?‚¯)
107                                    int quotCount = StringUtil.countChar( line, '"' );
108                                    if( quotCount % 2 != 0 ) {
109                                            String addLine = null;
110                                            StringBuilder buf = new StringBuilder( line );
111                                            while(quotCount % 2 != 0 && (addLine = reader.readLine()) != null) {
112                                                    buf.append( HybsSystem.CR ).append( addLine );
113                                                    quotCount += StringUtil.countChar( addLine, '"' );
114                                            }
115                                            line = buf.toString();
116                                    }
117    
118                                    if( skip > 0 ) { skip--; continue; }         // 5.1.6.0 (2010/05/01)
119                                    if( line.length() == 0 ) { continue; }
120                                    if( line.charAt( 0 ) == '#' ) {
121                                            String key = line.substring( 0,5 );
122                                            if( nameNoSet && ( key.equalsIgnoreCase( "#NAME" ) )) {
123                                                    // è¶?‚¤レギュラー処ç�?æœ??の TAB_SEPARATOR 以前ã?æ–?­—ã?無視するã?
124                                                    String line2 = line.substring( line.indexOf( sepa )+1 );
125                                                    names = StringUtil.csv2Array( line2 ,sepa );
126                                                    table.init( names.length );
127                                                    setTableDBColumn( names ) ;
128                                                    nameNoSet = false;
129                                            }
130                                            else  { continue; }
131                                    }
132                                    else {
133                                            if( nameNoSet ) {
134                                                    String errMsg = "#NAME が見つかる前にãƒ??タが見つかりましたã€?;
135                                                    throw new HybsSystemException( errMsg );
136                                            }
137                                            if( numberOfRows < getMaxRowCount() ) {
138                                                    setTableColumnValues( readData( line,names.length ) );          // 5.2.1.0 (2010/10/01)
139    //                                              table.addColumnValues( readData( line,names.length ) );
140                                                    numberOfRows ++ ;
141                                            }
142                                            else {
143                                                    table.setOverflow( true );
144                                            }
145                                    }
146                            }
147    
148                            // 5.1.6.0 (2010/05/01) readDBTableのエラーチェãƒ?‚¯強åŒ?
149                            if( nameNoSet ) {
150                                    String errMsg = "ファイルから有効なãƒ??タが見つかりませんでしたã€?;
151                                    throw new HybsSystemException( errMsg );
152                            }
153                    }
154                    catch ( IOException ex ) {
155                            String errMsg = "ファイル読込みエラー[" + reader + "]"  ;
156                            throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並びé ?¤‰更
157                    }
158            }
159    }