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    }