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 = "5.2.1.0 (2010/10/01)" ; 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 * 052 * @param table DBTableModelオブジェクト 053 * @param writer PrintWriterオブジェクト 054 */ 055 @Override 056 protected void writeData( final DBTableModel table,final PrintWriter writer ) { 057 int numberOfRows = table.getRowCount(); 058 String SEPARATOR = getSeparator(); 059 boolean useNumber = isUseNumber(); 060 boolean useRenderer = isUseRenderer(); // 5.2.1.0 (2010/10/01) 061 062 for( int row=0; row<numberOfRows; row++ ) { 063 if( useNumber ) { 064 writer.print( String.valueOf( row+1 ) ); 065 writer.print( SEPARATOR ); 066 } 067 for( int i=0; i<numberOfColumns; i++ ) { 068 int clm = clmNo[i]; 069 String val = table.getValue(row,clm); 070// if( "NVAR".equals( dbColumn[clm].getDbType()) ) { 071 if( dbType[i] == NVAR ) { 072 val = StringUtil.getReplaceEscape( val ); 073 } 074 // 5.2.1.0 (2010/10/01) useRenderer 対応 075 else if( useRenderer ) { 076 val = StringUtil.spanCut( dbColumn[clm].getRendererValue( val ) ); 077 } 078 079 if( i != 0 ) { writer.print( SEPARATOR ); } 080 // 開始日などの 00000000 を文字列タイプで渡す 081 if( CLASS_VARCHAR2.indexOf( dbColumn[clm].getClassName() ) < 0 ) { 082 writer.print( val ); 083 } 084 else { 085 writer.print( excelT( val ) ); 086 } 087 } 088 writer.println(); 089 } 090 } 091 092 /** 093 * EXCELの文字列変換関数である T を適用します。 094 * 値を、 =T("値") という文字列に変換します。 095 * 値にダブルクオートが存在する場合は、その直前にもう一つダブルクオートを 096 * 入れて、エスケープします。 097 * 098 * @og.rev 3.4.0.1 (2003/09/03) JDK1.3 対応に修正。 099 * 100 * @param data 元のString文字列 101 * 102 * @return T関数を適用した文字列 103 */ 104 private String excelT( final String data ) { 105 if( data == null ) { return "" ; } 106 107 StringBuilder strBuf = new StringBuilder( data ); 108 int len = data.lastIndexOf( '\"' ); 109 while( len >= 0 ) { 110 strBuf.insert( len,"\"" ) ; 111 len = data.lastIndexOf( "\"",len-1 ); 112 } 113 114 strBuf.insert( 0,"=T(\"" ).append( "\")" ); 115 116 return strBuf.toString(); 117 } 118}