/*
 * $Id: xmlsaver.cpp,v 1.1 2005/10/19 14:05:44 ozawa Exp $
 *
 * Copyright 2005- ONGS Inc. All rights reserved.
 * 
 * author: Masanori OZAWA (ozawa@ongs.co.jp)
 * version: $Revision: 1.1 $
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY ONGS INC ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ONGS INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the ONGS Inc.
 * 
 */

#include "xdtp.h"
#include "xmlsaver.h"

#include "libxslt/xsltutils.h"

using namespace XDTP;

/**
 * コンストラクタ
 */
XMLSaver::XMLSaver()
{
}

/**
 * デストラクタ
 */
XMLSaver::~XMLSaver()
{
}

/**
 * XMLをファイルに保存します。
 * 
 * @param aPath 出力先ファイル
 * @param aDocument XMLドキュメント
 * @param aXSL XSLT処理を行った場合は出力形式を保持するXSLドキュメント。
 *             処理を行っていない場合は NULL を指定する。
 * @param anEncoding 出力エンコード
 * @return 正常に保存した場合は true を戻す。
 */
bool XMLSaver::save(const Glib::ustring& aPath, const xmlDocPtr aDocument,
                    const xsltStylesheetPtr aXSL, const Glib::ustring& anEncoding)
{
    int status = -1;

    if (aXSL) {
        // fixme:
        // libxsltにて出力エンコードを指定する関数が存在しない。
        // これを強制的に回避するため、内部変数を直接書き換える。
        // 将来的に不安なコードであるため、できるだけ早くに適切な
        // 関数に置き換えたい。 by ozawa 2005/09/27
        if (aXSL->encoding) free(aXSL->encoding);
        aXSL->encoding = (xmlChar*)g_strdup(anEncoding.c_str());
        // ここまで - fixme
        
        status = xsltSaveResultToFilename(aPath.c_str(), aDocument, aXSL, 0);
    }
    else {
        status = xmlSaveFormatFileEnc(aPath.c_str(), aDocument, anEncoding.c_str(), 1);
    }

    return (-1 != status);
}

/**
 * XMLを文字列に変換します。
 * 
 * @param aDocument XMLドキュメント
 * @param anEncoding 出力エンコード
 * @return 変換後の文字列。変換に失敗した場合は空文字列。
 */
std::string XMLSaver::toString(const xmlDocPtr aDocument, const Glib::ustring& anEncoding)
{
    int size = 0;
    xmlChar* pMem = NULL;
    
    xmlDocDumpMemoryEnc(aDocument, &pMem, &size, anEncoding.c_str());
    
    std::string result = (const char*)pMem;

    xmlFree(pMem);

    return result;
}


syntax highlighted by Code2HTML, v. 0.9.1