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 */ 016package org.opengion.plugin.io; 017 018import java.io.BufferedReader; 019import java.io.IOException; 020 021import org.opengion.fukurou.util.StringUtil; 022import org.opengion.hayabusa.common.HybsSystem; 023import org.opengion.hayabusa.common.HybsSystemException; 024import org.opengion.hayabusa.db.DBTableModelUtil; 025import 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 */ 039public 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 = getSeparator().charAt( 0 ); // 5.2.0.0 (2010/09/01) 081 082 boolean nameNoSet = true; 083 table = DBTableModelUtil.newDBTable(); 084 085 // 3.5.4.5 (2004/01/23) カラム名の外部指定を優先して使用する。 086 if( columns != null && columns.length() > 0 ) { 087 names = StringUtil.csv2Array( columns ); 088 table.init( names.length ); 089 setTableDBColumn( names ) ; 090 nameNoSet = false; 091 } 092 093 int skip = getSkipRowCount(); // 5.2.0.0 (2010/09/01) 094 while((line = reader.readLine()) != null) { 095 // 5.2.0.0 (2010/09/01) ""で囲われているデータに改行が入っていた場合の対応 096 // 5.2.1.0 (2010/10/01) findbugs 対策(文字列の + 連結と、奇数判定ロジック) 097 int quotCount = StringUtil.countChar( line, '"' ); 098 if( quotCount % 2 != 0 ) { 099 String addLine = null; 100 StringBuilder buf = new StringBuilder( line ); 101 while(quotCount % 2 != 0 && (addLine = reader.readLine()) != null) { 102 buf.append( HybsSystem.CR ).append( addLine ); 103 quotCount += StringUtil.countChar( addLine, '"' ); 104 } 105 line = buf.toString(); 106 } 107 108 if( skip > 0 ) { skip--; continue; } // 5.1.6.0 (2010/05/01) 109 if( line.length() == 0 ) { continue; } 110 if( line.charAt( 0 ) == '#' ) { 111 String key = line.substring( 0,5 ); 112 if( nameNoSet && "#NAME".equalsIgnoreCase( key ) ) { 113 // 超イレギュラー処理 最初の TAB_SEPARATOR 以前の文字は無視する。 114 String line2 = line.substring( line.indexOf( sepa )+1 ); 115 names = StringUtil.csv2Array( line2 ,sepa ); 116 table.init( names.length ); 117 setTableDBColumn( names ) ; 118 nameNoSet = false; 119 } 120 else { continue; } 121 } 122 else { 123 if( nameNoSet ) { 124 String errMsg = "#NAME が見つかる前にデータが見つかりました。"; 125 throw new HybsSystemException( errMsg ); 126 } 127 if( numberOfRows < getMaxRowCount() ) { 128 setTableColumnValues( readData( line,names.length ) ); // 5.2.1.0 (2010/10/01) 129 numberOfRows ++ ; 130 } 131 else { 132 table.setOverflow( true ); 133 } 134 } 135 } 136 137 // 5.1.6.0 (2010/05/01) readDBTableのエラーチェック強化 138 if( nameNoSet ) { 139 String errMsg = "ファイルから有効なデータが見つかりませんでした。"; 140 throw new HybsSystemException( errMsg ); 141 } 142 } 143 catch ( IOException ex ) { 144 String errMsg = "ファイル読込みエラー[" + reader + "]" ; 145 throw new HybsSystemException( errMsg,ex ); // 3.5.5.4 (2004/04/15) 引数の並び順変更 146 } 147 } 148}