2 * Copyright (c) 2009 The openGion Project.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
16 package org.opengion.plugin.io;
18 import java.io.File; // 6.2.0.0 (2015/02/27)
19 import java.util.Arrays; // 6.2.0.0 (2015/02/27)
21 import org.opengion.fukurou.model.TableModelHelper; // 6.2.0.0 (2015/02/27) クラス名変更
22 import org.opengion.fukurou.model.EventReader_TEXT; // 6.2.0.0 (2015/02/27)
24 import org.opengion.hayabusa.common.HybsSystemException;
25 import org.opengion.hayabusa.io.AbstractTableReader; // 6.2.0.0 (2015/02/27)
27 import static org.opengion.fukurou.system.HybsConst.CR ; // 6.2.2.0 (2015/03/27)
30 * POI による、EXCELバイナリファイルを読み取る実装クラスです。
32 * ファイル名、シート名を指定して、データを読み取ることが可能です。
33 * 第一カラムが # で始まる行は、コメント行なので、読み飛ばします。
34 * カラム名の指定行で、カラム名が null の場合は、その列は読み飛ばします。
36 * 入力形式は、openXML形式にも対応しています。
37 * ファイルの内容に応じて、.xlsと.xlsxのどちらで読み取るかは、内部的に
40 * @og.rev 3.5.4.8 (2004/02/23) 新規作成
41 * @og.rev 4.3.6.7 (2009/05/22) ooxml形式対応
42 * @og.rev 6.2.0.0 (2015/02/27) TableReader クラスの呼び出し元メソッドの共通化(EXCEL,TEXT)。新規
46 * @author Kazuhiko Hasegawa
49 public class TableReader_Text extends AbstractTableReader {
50 /** このプログラムのVERSION文字列を設定します。 {@value} */
51 private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
56 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
58 public TableReader_Text() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
61 * DBTableModel から 各形式のデータを作成して、BufferedReader より読み取ります。
62 * コメント/空行を除き、最初の行は、必ず項目名が必要です。
63 * それ以降は、コメント/空行を除き、データとして読み込んでいきます。
64 * このメソッドは、EXCEL 読み込み時に使用します。
66 * @og.rev 4.0.0.0 (2006/09/31) 新規追加
67 * @og.rev 5.1.6.0 (2010/05/01) columns 処理 追加
68 * @og.rev 5.1.6.0 (2010/05/01) skipRowCountの追加
69 * @og.rev 5.1.8.0 (2010/07/01) Exception をきちっと記述(InvalidFormatException)
70 * @og.rev 5.2.1.0 (2010/10/01) setTableColumnValues メソッドを経由して、テーブルにデータをセットする。
71 * @og.rev 5.5.1.2 (2012/04/06) HeaderData を try の上にだし、エラーメッセージを取得できるようにする。
72 * @og.rev 5.5.7.2 (2012/10/09) sheetNos 追加による複数シートのマージ読み取りサポート
73 * @og.rev 5.5.8.2 (2012/11/09) HeaderData に デバッグフラグを渡します。
74 * @og.rev 6.0.2.5 (2014/10/31) debug=true 時に、進捗が見えるようにします。
75 * @og.rev 6.1.0.0 (2014/12/26) Excel関係改善
76 * @og.rev 6.2.0.0 (2015/02/27) TableReader クラスの呼び出し元メソッドの共通化(EXCEL,TEXT)。新規
77 * @og.rev 6.2.1.0 (2015/03/13) TableReaderModel を外部からセットします。
78 * @og.rev 6.2.3.0 (2015/05/01) 行読み飛ばし nullSkipClm追加
80 * @param file 読み取り元ファイル
81 * @param enc ファイルのエンコード文字列(未使用)
84 public void readDBTable( final File file , final String enc ) {
85 final boolean isDebug = isDebug(); // 5.5.7.2 (2012/10/09) デバッグ情報
87 if( isDebug ) { System.out.println( " Filename=" + file ) ; }
89 final TableModelHelper helper = new TableModelHelper() {
92 * カラム名配列がそろった段階で、イベントが発生します。
94 * openGion での標準的な処理は、colNo==0 の時に、val の先頭が、#NAME
95 * で始まるレコードを、名前配列として認識します。
96 * #value( String,int,int ) で、この #NAME だけは、継続処理されます。
97 * その上で、#NAME レコードが終了した時点で、カラム名配列が完成するので
98 * そこで初めて、このメソッドが呼ばれます。
100 * @og.rev 6.0.3.0 (2014/11/13) 新規作成
101 * @og.rev 6.1.0.0 (2014/12/26) omitNames 属性を追加
103 * @param names カラム名配列
104 * @see #value( String,int,int )
107 public void columnNames( final String[] names ) {
108 setTableDBColumn( names ) ;
112 * #NAME のオリジナルカラム名配列がそろった段階で、イベントが発生します。
114 * @og.rev 7.3.1.3 (2021/03/09) #NAMEのオリジナルを取得できるようにします。
116 * @param names カラム名配列
119 public void originalNames( final String[] names ) {
120 setOriginalNames( names ) ;
124 * row にあるセルのオブジェクト値がそろった段階で、イベントが発生します。
126 * @og.rev 6.0.3.0 (2014/11/13) 新規作成
127 * @og.rev 6.2.1.0 (2015/03/13) setTableColumnValuesに、行番号を引数に追加
129 * @param vals 文字列値の1行分の配列
130 * @param rowNo 行番号(0~)
133 public void values( final String[] vals,final int rowNo ) {
134 if( isDebug && rowNo % 100 == 0 ) {
135 System.out.println( " No.[" + rowNo + "]=" + Arrays.toString( vals ) ) ;
137 setTableColumnValues( vals,rowNo ); // 6.2.1.0 (2015/03/13)
141 helper.setDebug( isDebug ); // 6.2.0.0 (2015/02/27)
142 helper.setConstData( constKeys , constAdrs ); // 6.1.0.0 (2014/12/26) 外部から固定値情報を指定。
143 helper.setNames( columns , isUseNumber() ); // 6.1.0.0 (2014/12/26) 外部からカラム名配列を指定。
144 helper.setSkipRowCount( getSkipRowCount() ); // 6.1.0.0 (2014/12/26) 外部からスキップ行数を指定。
145 helper.setNullBreakClm( nullBreakClm ); // 6.2.0.0 (2015/02/27) 外部からnullBreakClmを指定。
146 helper.setNullSkipClm( nullSkipClm ); // 6.2.3.0 (2015/05/01) 外部からnullSkipClmを指定。
148 final EventReader_TEXT reader = new EventReader_TEXT();
149 reader.setSeparator( separator ); // 6.2.0.0 (2015/02/27) 外部からエンコードを指定。
150 reader.setEncode( enc ); // 6.2.0.0 (2015/02/27) 外部からエンコードを指定。
152 reader.eventReader( file , helper );
154 // 最後まで、#NAME が見つから無かった場合
155 if( !helper.isNameSet() ) {
156 final String errMsg = "最後まで、#NAME が見つかりませんでした。"
158 + "ファイル形式が異なるか、もしくは損傷している可能性があります。"
159 + "Class=[Text], File=[" + file + "], Encode=[" + enc + "], Separator=[" + separator + "]"
161 throw new HybsSystemException( errMsg );
164 if( isDebug ) { System.out.println( " TableReader End." ) ; }