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.PrintWriter; 019 020import org.opengion.fukurou.util.StringUtil; 021import org.opengion.hayabusa.db.DBTableModel; 022 023/** 024 * Excelでの文字変換関数 =T("値") という文字列で書き出すクラスです。 025 * 026 * DefaultTableWriter を継承して,データの出力部のみオーバーライドして, 027 * 文字列カラム(クラス名VARCHAR2のカラム)に、=T("値") という文字列で出力する 028 * ファイルの出力機能を実現しています。 029 * 030 * @og.rev 3.1.4.1 (2003/04/21) 新規作成 031 * @og.group ファイル出力 032 * 033 * @version 4.0 034 * @author Kazuhiko Hasegawa 035 * @since JDK5.0, 036 */ 037public class TableWriter_T extends TableWriter_Default { 038 /** このプログラムのVERSION文字列を設定します。 {@value} */ 039 private static final String VERSION = "6.0.1.2 (2014/08/08)" ; 040 041 /** 文字列タイプ名 {@value} */ 042 public static final String CLASS_VARCHAR2 = "VARCHAR2"; 043 044 /** 045 * PrintWriter に DBTableModelのテーブル情報を書き込みます。 046 * 047 * @og.rev 3.7.0.2 (2005/02/14) 行番号情報を、出力する(true)/しない(false)を指定 048 * @og.rev 3.8.0.1 (2005/06/17) DBTypeが NVAR の場合は、元のUnicodeに戻します。 049 * @og.rev 5.1.6.0 (2010/05/01) DbType の初期値(dbType)を利用する。 050 * @og.rev 5.2.1.0 (2010/10/01) useRenderer 対応 051 * @og.rev 6.0.1.2 (2014/08/08) カラム飛ばしできる機能を追加 052 * 053 * @param table DBTableModelオブジェクト 054 * @param writer PrintWriterオブジェクト 055 */ 056 @Override 057 protected void writeData( final DBTableModel table,final PrintWriter writer ) { 058 int numberOfRows = table.getRowCount(); 059 String SEPARATOR = getSeparator(); 060 boolean useNumber = isUseNumber(); 061 boolean useRenderer = isUseRenderer(); // 5.2.1.0 (2010/10/01) 062 063 for( int row=0; row<numberOfRows; row++ ) { 064 if( useNumber ) { 065 writer.print( String.valueOf( row+1 ) ); 066 writer.print( SEPARATOR ); 067 } 068 for( int i=0; i<numberOfColumns; i++ ) { 069 if( i != 0 ) { writer.print( SEPARATOR ); } 070 int clm = clmNo[i]; 071 if( clm < 0 ) { continue; } // 6.0.1.2 (2014/08/08) カラム飛ばし 072 073 String val = table.getValue(row,clm); 074 if( dbType[i] == NVAR ) { 075 val = StringUtil.getReplaceEscape( val ); 076 } 077 // 5.2.1.0 (2010/10/01) useRenderer 対応 078 else if( useRenderer ) { 079 val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) ); 080 } 081 082// if( i != 0 ) { writer.print( SEPARATOR ); } 083 // 開始日などの 00000000 を文字列タイプで渡す 084 if( CLASS_VARCHAR2.indexOf( dbColumn[clm].getClassName() ) < 0 ) { 085 writer.print( val ); 086 } 087 else { 088 writer.print( excelT( val ) ); 089 } 090 } 091 writer.println(); 092 } 093 } 094 095 /** 096 * EXCELの文字列変換関数である T を適用します。 097 * 値を、 =T("値") という文字列に変換します。 098 * 値にダブルクオートが存在する場合は、その直前にもう一つダブルクオートを 099 * 入れて、エスケープします。 100 * 101 * @og.rev 3.4.0.1 (2003/09/03) JDK1.3 対応に修正。 102 * 103 * @param data 元のString文字列 104 * 105 * @return T関数を適用した文字列 106 */ 107 private String excelT( final String data ) { 108 if( data == null ) { return "" ; } 109 110 StringBuilder strBuf = new StringBuilder( data ); 111 int len = data.lastIndexOf( '\"' ); 112 while( len >= 0 ) { 113 strBuf.insert( len,"\"" ) ; 114 len = data.lastIndexOf( "\"",len-1 ); 115 } 116 117 strBuf.insert( 0,"=T(\"" ).append( "\")" ); 118 119 return strBuf.toString(); 120 } 121}