ModelFactory.cpp

#include "StdAfx.h"
#include "ModelFactory.h"


#include "Model.h"

#include "Link.h"
#include "LinkFactory.h"


using namespace Core;
using namespace System;
using namespace System::Diagnostics;


/// 生成する
Model^ ModelFactory::Create( System::String^ fileName )
{
    Debug::Assert( IO::File::Exists( fileName ), "存在するファイルである" );
    Debug::WriteLine( "モデルを生成する" );


    // ルート要素を取得する
    Xml::XmlElement^ root = GetRootElement( fileName );

    // ... リンクの配列を生成する
    Links^ links = LinkFactory::CreateLinks( root );


    // インスタンスを生成する
    Model^ result = FactoryMethod( links );

    result->Name = root->GetAttribute( "name" );                     // 名称
    result->Voltage = Double::Parse( root[ "voltage" ]->InnerText ); // 電圧

    return result;
}

/// インスタンスを生成して返す
Model^ ModelFactory::FactoryMethod( Links^ links )
{
    // 生成する
    return( gcnew Model( links ) );
}


/// ルート要素を取得する
Xml::XmlElement^ ModelFactory::GetRootElement( System::String^ fileName )
{
    Xml::XmlDocument^ document = gcnew Xml::XmlDocument();

    try
    {
        // XML ドキュメントを読み込む
        document->Load( fileName );
    }
    catch( IO::FileNotFoundException^ e )
    {
        Debug::WriteLine( "ファイルが見つからなかった : " + e->Message );
        throw;
    }
    catch( Xml::XmlException^ e )
    {
        Debug::WriteLine( "ファイルの読み込みエラー : " + e->Message );
        throw;
    }

    // ドキュメントのルート要素を取得する
    return document->DocumentElement;
}