/*******************************************************************************
 * 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.html.normalizer.task;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import blanco.commons.util.BlancoFileUtil;
import blanco.html.normalizer.message.BlancoHtmlNormalizerMessage;
import blanco.html.normalizer.parser.helper.BlancoHtmlNormalizeQuoteContentHandler;
import blanco.html.normalizer.task.valueobject.BlancoHtmlNormalizeQuoteProcessInput;
import blanco.html.parser.BlancoHtmlParser;
import blanco.html.parser.BlancoHtmlParserFactory;

/**
 * HTML のクオートを正常化するための処理。
 */
public class BlancoHtmlNormalizeQuoteProcessImpl implements
        BlancoHtmlNormalizeQuoteProcess {
    protected BlancoHtmlNormalizeQuoteProcessInput fInput;

    protected BlancoHtmlNormalizerMessage fMsg = new BlancoHtmlNormalizerMessage();

    public int execute(final BlancoHtmlNormalizeQuoteProcessInput input)
            throws IOException, IllegalArgumentException {
        fInput = input;

        final File sourceDir = new File(fInput.getSourcedir());
        final File targetDir = new File(fInput.getTargetdir());

        if (sourceDir.exists() == false) {
            throw new IllegalArgumentException(fMsg.getMbhtni01(fInput
                    .getSourcedir()));
        }
        if (sourceDir.isDirectory() == false) {
            throw new IllegalArgumentException(fMsg.getMbhtni02(fInput
                    .getSourcedir()));
        }
        if (targetDir.exists() == false) {
            if (targetDir.mkdirs() == false) {
                throw new IllegalArgumentException(fMsg.getMbhtni03(fInput
                        .getTargetdir()));
            }
        } else if (targetDir.isDirectory() == false) {
            throw new IllegalArgumentException(fMsg.getMbhtni04(fInput
                    .getTargetdir()));
        }
        if (sourceDir.getCanonicalPath().equals(targetDir.getCanonicalPath())) {
            throw new IllegalArgumentException(fMsg.getMbhtni05(fInput
                    .getSourcedir(), fInput.getTargetdir()));
        }

        process();

        return 0;
    }

    protected void process() throws IOException {
        final File sourceDir = new File(fInput.getSourcedir());
        final File targetDir = new File(fInput.getTargetdir());

        final File[] files = sourceDir.listFiles();
        if (files == null) {
            return;
        }
        for (final File file : files) {
            if (file.isFile() == false) {
                continue;
            }

            final BlancoHtmlParser parser = BlancoHtmlParserFactory
                    .getInstance();
            final BlancoHtmlNormalizeQuoteContentHandler handler = new BlancoHtmlNormalizeQuoteContentHandler();
            parser.setHandler(handler);
            final File targetFile = new File(targetDir.getAbsolutePath() + "/"
                    + file.getName());
            handler.setWriter(new BufferedWriter(new FileWriter(targetFile)));

            final byte[] fileData = BlancoFileUtil.file2Bytes(file);

            parser.parse(fileData);

            handler.getWriter().close();
        }
    }
}
