/*******************************************************************************
 * Copyright (c) 2008 IGA Tosiki, NTT DATA BUSINESS BRAINS Corp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    IGA Tosiki (NTT DATA BUSINESS BRAINS Corp.) - initial API and implementation
 *******************************************************************************/
/*
 * blanco Framework
 * Copyright (C) 2008 NTT DATA BUSINESS BRAINS CORPORATION
 * 
 * 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.tmx.util.task;

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

import blanco.commons.util.BlancoStringUtil;
import blanco.tmx.BlancoTmxParser;
import blanco.tmx.BlancoTmxSerializer;
import blanco.tmx.util.message.BlancoTmxUtilMessage;
import blanco.tmx.util.task.valueobject.BlancoTmxUtilConvertProcessInput;
import blanco.tmx.valueobject.BlancoTmx;
import blanco.tmx.valueobject.BlancoTmxTu;
import blanco.tmx.valueobject.BlancoTmxTuv;

public class BlancoTmxUtilConvertProcessImpl implements
        BlancoTmxUtilConvertProcess {
    private final BlancoTmxUtilMessage fMsg = new BlancoTmxUtilMessage();

    private BlancoTmxUtilConvertProcessInput fInput = null;

    /**
     * クラスをインスタンス化して処理を実行する際のエントリポイントです。
     * 
     * @param input
     *            処理の入力パラメータ。
     * @return 処理の実行結果。
     * @throws IOException
     *             入出力例外が発生した場合。
     * @throws IllegalArgumentException
     *             入力値に不正が見つかった場合。
     */
    public int execute(final BlancoTmxUtilConvertProcessInput input)
            throws IOException, IllegalArgumentException {
        fInput = input;

        final File sourceDir = new File(input.getSourcedir());

        final File targetDir = new File(input.getTargetdir());

        // 入力ディレクトリの存在チェック。
        if (sourceDir.exists() == false) {
            throw new IllegalArgumentException(fMsg.getMbtmut01(sourceDir
                    .getAbsolutePath()));
        }
        if (sourceDir.isDirectory() == false) {
            throw new IllegalArgumentException(fMsg.getMbtmut02(sourceDir
                    .getAbsolutePath()));
        }
        if (targetDir.exists() == false) {
            if (targetDir.mkdirs() == false) {
                throw new IllegalArgumentException(fMsg.getMbtmut03(targetDir
                        .getAbsolutePath()));
            }
        }
        if (targetDir.isDirectory() == false) {
            throw new IllegalArgumentException(fMsg.getMbtmut04(targetDir
                    .getAbsolutePath()));
        }
        if (sourceDir.getCanonicalPath().equals(targetDir.getCanonicalPath())) {
            throw new IllegalArgumentException(fMsg.getMbtmut05(sourceDir
                    .getAbsolutePath(), targetDir.getAbsolutePath()));
        }

        final File[] files = sourceDir.listFiles();
        if (files == null) {
            return 0;
        }
        for (int index = 0; index < files.length; index++) {
            if (files[index].isFile() == false) {
                continue;
            }
            if (files[index].getName().endsWith(".tmx")) {
                process(files[index], targetDir);
            }
        }

        return 0;
    }

    private void process(final File sourceFile, final File targetDir)
            throws IOException {

        final String bundleSymbolicName = sourceFile.getName().substring(0,
                sourceFile.getName().length() - ".tmx".length());

        final BlancoTmxParser parser = new BlancoTmxParser();
        final BlancoTmx tmx = parser.parse(sourceFile);

        // エンコーディングを上書き。
        if (BlancoStringUtil.null2Blank(fInput.getEncoding()).length() > 0) {
            tmx.setEncoding(fInput.getEncoding());
        }

        processTmx(tmx);

        final BlancoTmxSerializer serializer = new BlancoTmxSerializer();
        serializer.setIndent(fInput.getIsindentxml());
        serializer.serialize(tmx, new File(targetDir.getAbsolutePath() + "/"
                + bundleSymbolicName + ".tmx"));
    }

    private void processTmx(final BlancoTmx tmx) {
        for (BlancoTmxTu tu : tmx.getBody().getTuList()) {
            for (BlancoTmxTuv tuv : tu.getTuvList()) {
                if (fInput.getAdjustlang()) {
                    if ("en_US".equals(tuv.getLang())) {
                        tuv.setLang("EN-US");
                    }
                    if ("ja_JP".equals(tuv.getLang())) {
                        tuv.setLang("JA");
                    }
                }
            }
        }
    }
}
