/*
 * blanco Framework
 * Copyright (C) 2004-2005 IGA Tosiki
 * 
 * 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.meta2xml.task;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.TransformerException;

import blanco.meta2xml.BlancoMeta2XmlConstants;
import blanco.meta2xml.BlancoMeta2XmlProcessMeta2Xml;
import blanco.meta2xml.BlancoMeta2XmlXml2JavaClass;

public class BlancoMeta2XmlProcessTask extends
        AbstractBlancoMeta2XmlProcessTask {
    protected void process() throws IllegalArgumentException {
        System.out.println("- " + BlancoMeta2XmlConstants.PRODUCT_NAME + " ("
                + BlancoMeta2XmlConstants.VERSION + ")");

        try {
            final File fileMetadir = new File(getMetadir());
            if (fileMetadir.exists() == false) {
                throw new IllegalArgumentException("メタディレクトリ[" + getMetadir()
                        + "]が存在しません。");
            }

            // テンポラリディレクトリを作成。
            new File(getTmpdir() + BlancoMeta2XmlConstants.TARGET_SUBDIRECTORY)
                    .mkdirs();

            // 指定されたメタディレクトリを処理します。
            final BlancoMeta2XmlProcessMeta2Xml meta2Xml = new BlancoMeta2XmlProcessMeta2Xml();
            meta2Xml.setCacheMeta2Xml(getCache().equals("true"));
            meta2Xml.processDirectory(fileMetadir, getTmpdir()
                    + BlancoMeta2XmlConstants.TARGET_SUBDIRECTORY);

            // XML化されたメタファイルからValueObjectを生成
            // 最初にテンポラリフォルダを走査
            final File fileWorkdir = new File(getTmpdir()
                    + BlancoMeta2XmlConstants.TARGET_SUBDIRECTORY);
            final File[] fileMeta2 = fileWorkdir.listFiles();
            if (fileMeta2 == null) {
                throw new IllegalArgumentException("ワークディレクトリ["
                        + fileWorkdir.getAbsolutePath() + "]のファイル一覧の取得に失敗しました。");
            }

            for (int index = 0; index < fileMeta2.length; index++) {
                if (fileMeta2[index].getName().endsWith(".xml") == false) {
                    continue;
                }

                new BlancoMeta2XmlXml2JavaClass().process(fileMeta2[index],
                        new File(getTargetdir()));
            }
        } catch (IOException e) {
            throw new IllegalArgumentException(e.toString());
        } catch (TransformerException e) {
            throw new IllegalArgumentException(e.toString());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
