/* 
 * $Id: URI.cpp,v 1.5 2006/03/05 05:00:09 ozawa Exp $
 *
 * Copyright 2005- ONGS Inc. All rights reserved.
 * 
 * author: Masanori OZAWA (ozawa@ongs.co.jp)
 * version: $Revision: 1.5 $
 *
 * 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 "URI.h"

#include "URI_Impl.h"
#include "URI_OldImpl.h"

using namespace XDTP;

URI::URI()
{
    m_URIPtr = NULL;
}

URI::~URI()
{
    if (m_URIPtr) xmlFreeURI(m_URIPtr);
}

/**
 * URIを解析します。
 * 
 * @param uri URI
 * @return 解析結果を処理するURIオブジェクト
 */
RefPtr<URI> URI::parse(const Glib::ustring& uri)
{
    if (getenv(XDTP_URI_TRANSITION)) {
#ifdef DEBUG
        static bool first = false;
        if (!first) {
            fprintf(stdout, "%s: enable XDTP_URI_TRANSITION" LINE_SEP, APP_NAME);
            first = true;
        }
#endif
        RefPtr<URI> result(new URI_OldImpl(uri));
        return result;
    }
    
    RefPtr<URI> result(new URI_Impl(uri));
    return result;
}

const URI& URI::operator=(const URI& uri)
{
    if (m_URIPtr) {
        xmlFreeURI(m_URIPtr);
        m_URIPtr = NULL;
    }
    
    /**
     * URIは正規化後の問題のない状態を保持するため、
     * URI_Impl , URI_OldImpl 双方の違いをここで
     * 処理する必要はない。
     * これらクラスは、コンストラクタの段階で問題を
     * 吸収する処理を行う。 by ozawa 2005/11/02
     */
    xmlChar* uristr = xmlSaveUri(uri.m_URIPtr);
    if (!uristr) THROW("Out of memory!");

    m_URIPtr = xmlParseURI((const char*)uristr);
    xmlFree(uristr);
    
    return (*this);
}

Glib::ustring URI::toString() const
{
    Glib::ustring result;
    
    xmlChar* uristr = xmlSaveUri(m_URIPtr);
    if (!uristr) THROW("Out of memory!");
    result = (const char*)uristr;
    xmlFree(uristr);
    
    return result;
}


syntax highlighted by Code2HTML, v. 0.9.1