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.hayabusa.common.HybsSystemException;
021import org.opengion.hayabusa.db.DBTableModel;
022
023/**
024 * プロパティファイル形式(エンジン専用特殊形式)の書き出しクラスです。
025 * Ver4 では、プロパティファイル形式をサポートしていません。
026 *
027 * DefaultTableWriter を継承していますので,ラベル,名前,データの出力部のみ
028 * オーバーライドして,プロパティーファイルの出力機能を実現しています。
029 *
030 * @og.group ファイル出力
031 *
032 * @version  4.0
033 * @author   Kazuhiko Hasegawa
034 * @since    JDK5.0,
035 */
036public class TableWriter_Properties extends TableWriter_Default {
037        //* このプログラムのVERSION文字列を設定します。   {@value} */
038        private static final String VERSION = "4.0.0.0 (2005/08/31)" ;
039
040        private static final String  headerSequence = "NL-D";   // ヘッダー項目の並び順
041
042//      /**
043//       * デフォルトコンストラクター
044//       *
045//       */
046//      public TableWriter_Properties() {
047//              super();
048//      }
049
050        /**
051         * DBTableModel から データを作成して,PrintWriter に書き出します。
052         *
053         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
054         * @og.rev 3.5.4.3 (2004/01/05) 引数に PrintWriter を受け取るように変更します。
055         *
056         * @param       writer PrintWriterオブジェクト
057         */
058        @Override
059        public void writeDBTable( final PrintWriter writer )  {
060                setHeaderSequence( headerSequence );
061                super.writeDBTable( writer );
062        }
063
064        /**
065         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
066         * このクラスでは,データを ダブルコーテーション(")で囲みます。
067         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
068         *
069         * @og.rev 2.3.1.2 (2003/01/28) データ出力時に、改行が余分に出される箇所を修正。
070         * @og.rev 3.1.1.0 (2003/03/28) 同期メソッド(synchronized付き)を非同期に変更する。
071         *
072         * @param       table  DBTableModelオブジェクト
073         * @param       writer PrintWriterオブジェクト
074         */
075        @Override
076        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
077                if( numberOfColumns < 2 ) {
078                        String errMsg = "Properties では、最低、キーと値の2つ以上のカラムが必要です。"
079                                                + " numberOfColumns=[" + numberOfColumns + "]" ;
080                        throw new HybsSystemException( errMsg );
081                }
082
083                int numberOfRows =  table.getRowCount();
084                String separator = getSeparator();
085
086                for( int row=0; row<numberOfRows; row++ ) {
087                        writer.print( table.getValue(row,clmNo[0]) );
088                        writer.print( "=" );
089                        writer.print( table.getValue(row,clmNo[1]) );
090
091                        for( int i=2; i<numberOfColumns; i++ ) {
092                                int clm = clmNo[i];
093                                writer.print( separator );
094                                writer.print( table.getValue(row,clm) );
095                        }
096                        writer.println();
097                }
098        }
099}