/*
 * blancoCsv Copyright (C) 2005 Tosiki Iga
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 */
package blanco.csv.expand;

import java.io.File;
import java.util.List;

import blanco.cg.BlancoCgObjectFactory;
import blanco.cg.transformer.BlancoCgTransformerFactory;
import blanco.cg.valueobject.BlancoCgClass;
import blanco.cg.valueobject.BlancoCgMethod;
import blanco.cg.valueobject.BlancoCgSourceFile;
import blanco.commons.util.BlancoStringUtil;
import blanco.csv.resourcebundle.BlancoCsvRuntimeUtilResourceBundle;
import blanco.csv.valueobject.BlancoCsvStructure;

public class BlancoCsvExpandRuntimeUtil {
    /**
     * 内部的に利用するblancoCg用ファクトリ。
     */
    private BlancoCgObjectFactory fCgFactory = null;

    /**
     * 内部的に利用するblancoCg用ソースファイル情報。
     */
    private BlancoCgSourceFile fCgSourceFile = null;

    /**
     * 内部的に利用するblancoCg用クラス情報。
     */
    private BlancoCgClass fCgClass = null;

    final BlancoCsvRuntimeUtilResourceBundle bundle = new BlancoCsvRuntimeUtilResourceBundle();

    public void expand(final BlancoCsvStructure processStructure,
            final File directoryTarget) {

        // 従来と互換性を持たせるため、/mainサブフォルダに出力します。
        final File fileBlancoMain = new File(directoryTarget.getAbsolutePath()
                + "/main");

        fCgFactory = BlancoCgObjectFactory.getInstance();
        fCgSourceFile = fCgFactory.createSourceFile(processStructure
                .getRuntimePackage()
                + bundle.getPackagePrefix(),
                "このソースコードは blanco Frameworkによって自動生成されています。");
        fCgClass = fCgFactory.createClass(bundle.getClassName(),
                BlancoStringUtil.null2Blank(bundle.getClasscomment01()));
        fCgSourceFile.getClassList().add(fCgClass);
        fCgClass.getLangDoc().getDescriptionList().add(
                bundle.getClasscomment02());

        final String classNameblancoCsvIOException = processStructure
                .getRuntimePackage()
                + ".io.BlancoCsvIOException";

        {
            final BlancoCgMethod method = fCgFactory.createMethod(bundle
                    .getReadtokenName(), bundle.getReadtokenJavadoc());
            fCgClass.getMethodList().add(method);
            method.setStatic(true);

            // メソッドのシグニチャ指定
            method.getParameterList().add(
                    fCgFactory.createParameter(bundle.getReadtokenArg01Name(),
                            bundle.getReadtokenArg01Type(), bundle
                                    .getReadtokenArg01Javadoc()));
            method.getParameterList().add(
                    fCgFactory.createParameter(bundle.getReadtokenArg02Name(),
                            bundle.getReadtokenArg02Type(), bundle
                                    .getReadtokenArg02Javadoc()));
            method.getParameterList().add(
                    fCgFactory.createParameter(bundle.getReadtokenArg03Name(),
                            bundle.getReadtokenArg03Type(), bundle
                                    .getReadtokenArg03Javadoc()));
            method.setReturn(fCgFactory.createReturn(bundle
                    .getReadtokenReturnType(), bundle
                    .getReadtokenReturnJavadoc()));
            method.getThrowList().add(
                    fCgFactory.createException(classNameblancoCsvIOException,
                            "項目数が足りないなど与えられた文字列に関する例外が発生した場合。"));
            method.getThrowList().add(
                    fCgFactory.createException(bundle
                            .getReadtokenThrows01Type(), bundle
                            .getReadtokenThrows01Javadoc()));

            // メソッドの実装
            final List lineList = method.getLineList();
            lineList.add(bundle.getReadtokenLine01());
            lineList.add(bundle.getReadtokenLine02());
            lineList.add(bundle.getReadtokenLine03());
            lineList.add(bundle.getReadtokenLine04());
            lineList.add(bundle.getReadtokenLine05());
            lineList.add(bundle.getReadtokenLine06());
            lineList.add(bundle.getReadtokenLine07());
            lineList.add(bundle.getReadtokenLine08());
            lineList.add(bundle.getReadtokenLine09());
            lineList.add(bundle.getReadtokenLine10());
            lineList.add(bundle.getReadtokenLine11());
            lineList.add(bundle.getReadtokenLine12());
            lineList.add(bundle.getReadtokenLine13());
            lineList.add(bundle.getReadtokenLine14());
            lineList.add(bundle.getReadtokenLine15());
            lineList.add(bundle.getReadtokenLine16());
            lineList.add(bundle.getReadtokenLine17());
            lineList.add(bundle.getReadtokenLine18());
            lineList.add(bundle.getReadtokenLine19());
            lineList.add(bundle.getReadtokenLine20());
        }

        // メソッドのシグニチャ指定
        {
            final BlancoCgMethod method = fCgFactory.createMethod(bundle
                    .getReadtokenwithquoteName(), bundle
                    .getReadtokenwithquoteJavadoc());
            fCgClass.getMethodList().add(method);
            method.setStatic(true);

            method.getParameterList().add(
                    fCgFactory.createParameter(bundle
                            .getReadtokenwithquoteArg01Name(), bundle
                            .getReadtokenwithquoteArg01Type(), bundle
                            .getReadtokenwithquoteArg01Javadoc()));
            method.getParameterList().add(
                    fCgFactory.createParameter(bundle
                            .getReadtokenwithquoteArg02Name(), bundle
                            .getReadtokenwithquoteArg02Type(), bundle
                            .getReadtokenwithquoteArg02Javadoc()));
            method.getParameterList().add(
                    fCgFactory.createParameter(bundle
                            .getReadtokenwithquoteArg03Name(), bundle
                            .getReadtokenwithquoteArg03Type(), bundle
                            .getReadtokenwithquoteArg03Javadoc()));
            method.setReturn(fCgFactory.createReturn(bundle
                    .getReadtokenwithquoteReturnType(), bundle
                    .getReadtokenwithquoteReturnJavadoc()));
            method.getThrowList().add(
                    fCgFactory.createException(classNameblancoCsvIOException,
                            "ダブルクオートの不一致など与えられた文字列に関する例外が発生した場合。"));
            method.getThrowList().add(
                    fCgFactory.createException(bundle
                            .getReadtokenwithquoteThrows01Type(), bundle
                            .getReadtokenwithquoteThrows01Javadoc()));

            // メソッドの実装
            final List lineList = method.getLineList();
            lineList.add(bundle.getReadtokenwithquoteLine01());
            lineList.add(bundle.getReadtokenwithquoteLine02());
            lineList.add(bundle.getReadtokenwithquoteLine03());
            lineList.add(bundle.getReadtokenwithquoteLine04());
            lineList.add(bundle.getReadtokenwithquoteLine05());
            lineList.add(bundle.getReadtokenwithquoteLine06());
            lineList.add(bundle.getReadtokenwithquoteLine07());
            lineList.add(bundle.getReadtokenwithquoteLine08());
            lineList.add(bundle.getReadtokenwithquoteLine09());
            lineList.add(bundle.getReadtokenwithquoteLine10());
            lineList.add(bundle.getReadtokenwithquoteLine11());
            lineList.add(bundle.getReadtokenwithquoteLine12());
            lineList.add(bundle.getReadtokenwithquoteLine13());
            lineList.add(bundle.getReadtokenwithquoteLine14());
            lineList.add(bundle.getReadtokenwithquoteLine15());
            lineList.add(bundle.getReadtokenwithquoteLine16());
            lineList.add(bundle.getReadtokenwithquoteLine17());
            lineList.add(bundle.getReadtokenwithquoteLine18());
            lineList.add(bundle.getReadtokenwithquoteLine19());
            lineList.add(bundle.getReadtokenwithquoteLine20());
            lineList.add(bundle.getReadtokenwithquoteLine21());
            lineList.add(bundle.getReadtokenwithquoteLine22());
            lineList.add(bundle.getReadtokenwithquoteLine23());
            lineList.add(bundle.getReadtokenwithquoteLine24());
            lineList.add(bundle.getReadtokenwithquoteLine25());
            lineList.add(bundle.getReadtokenwithquoteLine26());
            lineList.add(bundle.getReadtokenwithquoteLine27());
            lineList.add(bundle.getReadtokenwithquoteLine28());
            lineList.add(bundle.getReadtokenwithquoteLine29());
            lineList.add(bundle.getReadtokenwithquoteLine30());
            lineList.add(bundle.getReadtokenwithquoteLine31());
            lineList.add(bundle.getReadtokenwithquoteLine32());
            lineList.add(bundle.getReadtokenwithquoteLine33());
            lineList.add(bundle.getReadtokenwithquoteLine34());
            lineList.add(bundle.getReadtokenwithquoteLine35());
            lineList.add(bundle.getReadtokenwithquoteLine36());
            lineList.add(bundle.getReadtokenwithquoteLine37());
            lineList.add(bundle.getReadtokenwithquoteLine38());
            lineList.add(bundle.getReadtokenwithquoteLine39());
            lineList.add(bundle.getReadtokenwithquoteLine40());
            lineList.add(bundle.getReadtokenwithquoteLine41());
            lineList.add(bundle.getReadtokenwithquoteLine42());
            lineList.add(bundle.getReadtokenwithquoteLine43());
            lineList.add(bundle.getReadtokenwithquoteLine44());
            lineList.add(bundle.getReadtokenwithquoteLine45());
            lineList.add(bundle.getReadtokenwithquoteLine46());
            lineList.add(bundle.getReadtokenwithquoteLine47());
            lineList.add(bundle.getReadtokenwithquoteLine48());
            lineList.add(bundle.getReadtokenwithquoteLine49());
            lineList.add(bundle.getReadtokenwithquoteLine50());
            lineList.add(bundle.getReadtokenwithquoteLine51());
            lineList.add(bundle.getReadtokenwithquoteLine52());
            lineList.add(bundle.getReadtokenwithquoteLine53());
            lineList.add(bundle.getReadtokenwithquoteLine54());

        }

        // メソッドのシグニチャ指定
        {
            final BlancoCgMethod method = fCgFactory.createMethod(bundle
                    .getGettokenwithquoteName(), bundle
                    .getGettokenwithquoteJavadoc());
            fCgClass.getMethodList().add(method);
            method.setStatic(true);

            method.getParameterList().add(
                    fCgFactory.createParameter("value", bundle
                            .getGettokenwithquoteArg01Type(), bundle
                            .getGettokenwithquoteArg01Javadoc()));
            method.setReturn(fCgFactory.createReturn(bundle
                    .getGettokenwithquoteReturnType(), bundle
                    .getGettokenwithquoteReturnJavadoc()));
            method.getThrowList().add(
                    fCgFactory.createException(bundle
                            .getGettokenwithquoteThrows01Type(), bundle
                            .getGettokenwithquoteThrows01Javadoc()));

            // メソッドの実装
            fCgSourceFile.getImportList().add(
                    bundle.getGettokenwithquoteAddusingtype01());
            fCgSourceFile.getImportList().add(
                    bundle.getGettokenwithquoteAddusingtype02());

            final List lineList = method.getLineList();
            lineList.add(bundle.getGettokenwithquoteLine01());
            lineList.add(bundle.getGettokenwithquoteLine02());
            lineList.add(bundle.getGettokenwithquoteLine03());
            lineList.add(bundle.getGettokenwithquoteLine04());
            lineList.add(bundle.getGettokenwithquoteLine05());
            lineList.add(bundle.getGettokenwithquoteLine06());
            lineList.add(bundle.getGettokenwithquoteLine07());
            lineList.add(bundle.getGettokenwithquoteLine08());
            lineList.add(bundle.getGettokenwithquoteLine09());
            lineList.add(bundle.getGettokenwithquoteLine10());
            lineList.add(bundle.getGettokenwithquoteLine11());
            lineList.add(bundle.getGettokenwithquoteLine12());
            lineList.add(bundle.getGettokenwithquoteLine13());
            lineList.add(bundle.getGettokenwithquoteLine14());
            lineList.add(bundle.getGettokenwithquoteLine15());
            lineList.add(bundle.getGettokenwithquoteLine16());
            lineList.add(bundle.getGettokenwithquoteLine17());
            lineList.add(bundle.getGettokenwithquoteLine18());
            lineList.add(bundle.getGettokenwithquoteLine19());
            lineList.add(bundle.getGettokenwithquoteLine20());
            lineList.add(bundle.getGettokenwithquoteLine21());
        }

        BlancoCgTransformerFactory.getJavaSourceTransformer().transform(
                fCgSourceFile, fileBlancoMain);
    }
}
