// XMLToSource.cpp: implementation of the XMLToSource class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AG_MDA.h"
#include "XMLToSource.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern ShapeElement *m_pRootShape;
extern UMLClassPointerList m_Classes;//所有的类
extern void AddToLog( CString str );
XMLToSource::XMLToSource()
{

}

XMLToSource::~XMLToSource()
{

}

BOOL XMLToSource::ReadALine(CFile &file, CString &str)
{
	char c;
	str = "";
	while( file.Read( &c , 1 ) )
	{
		if( c == '\r' )
		{
			file.Read( &c , 1 );
			str.TrimLeft();
			return TRUE;
		}
		else
		{
			str += c;
		}
	}
	return TRUE;
}

void XMLToSource::DecodeALine(StringList &list, CString str)
{
	if( str.GetLength() )
	{
		if( str.GetAt( 0 ) == '<' )
		{
			if( str.GetAt( 1 ) == '/' )
			{
				list.Add( new CString( str ) );
			}
			else
			{
				for( int i = 0 ; i < str.GetLength() ; i ++ )
				{
					if( str.GetAt( i ) == '>' ) break;
				}
				if( i != str.GetLength() - 1 )
				{
					CString tag1,tag2,con;
					tag1 = str.Left( i + 1 );
					tag2 = str.Right( i + 2 );
					con = str.Mid( i + 1 , str.GetLength() - i - i - 3 );
					list.Add( new CString( tag1 ) );
					list.Add( new CString( con ) );
					list.Add( new CString( tag2 ) );
				}
				else
				{
					list.Add( new CString( str ) );
				}
			}
		}
		else
		{//is content
			list.Add( new CString( str ) );
		}
	}
}

CString XMLToSource::DecodeItem(StringList &list, CString &content)
{
	CString rlt;
	CString *pStr;
	pStr = list.GetAt( 0 );
	rlt = *pStr;
	pStr = list.GetAt( 1 );
	content = *pStr;
	for(  int i = 0 ; i < 3 ; i ++ )
	{
		pStr = list.GetAt( 0 );
		delete pStr;
		list.RemoveAt( 0 );
	}
	return rlt;
}

UMLAttribute XMLToSource::DecodeAttribute(StringList &list)
{
	UMLAttribute att;
	CString *pStr;
	while( list.GetUpperBound()>= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<ATTRIBUTE>" )
		{
			list.RemoveAt( 0 );
			delete pStr;
			AddToLog("DecodeAttribute...");
		}
		else if( *pStr == "</ATTRIBUTE>" )
		{
			list.RemoveAt( 0 );
			delete pStr;
			AddToLog("DecodeAttribute Finished");
			break;
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , att.name );
			AddToLog(CString("Attribute name:") + att.name);
		}
		else if( *pStr == "<TYPE>" )
		{
			CString txt;
			DecodeItem( list , txt );
			att.m_pType = (UMLClass*)atoi(LPCTSTR(txt));
		}
		else if( *pStr == "<VISIBILITY>" )
		{
			CString txt;
			DecodeItem( list , txt );
			txt.MakeLower();
			if( txt == "public" )att.m_Visibility = VISIBILITY_PUBLIC;
			else if( txt == "private")att.m_Visibility = VISIBILITY_PRIVATE;
			else if( txt == "protected")att.m_Visibility = VISIBILITY_PROTECTED;
			else att.m_Visibility = VISIBILITY_PACKAGE;
		}
		else if( *pStr == "<MULTIPLICITY>" )
		{
			DecodeItem( list , att.m_Multiplicity );
		}
		else if( *pStr == "<INIVALUE>" )
		{
			DecodeItem( list ,att.m_InitialValue );
		}
		else if( *pStr == "<ORDER>" )
		{
			CString txt;
			DecodeItem( list , txt );
			if( txt == "NONE" )
			{
				att.m_OrderingKind = ORDERING_NONE;
			}
			else
			{
				att.m_OrderingKind = ORDERING_ORDER;
			}
		}
		else 
		{
			CString err;
			err= "DecodeActivityGraph error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return att;
}

UMLParameter XMLToSource::DecodeParameter(StringList &list)
{
	UMLParameter p;
	CString *pStr;
	while( list.GetUpperBound()>= 0)
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<PARAMETER>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
		else if( *pStr == "</PARAMETER>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if( *pStr == "<NAME>")
		{
			DecodeItem( list , p.name);
		}
		else if( *pStr == "<TYPE>")
		{
			CString txt;
			DecodeItem( list , txt );
			p.m_pType = (UMLClass*)atoi(LPCTSTR(txt));
		}
		else if( *pStr == "<DEFVALUE>" )
		{
			DecodeItem( list , p.m_DefaultValue );
		}
		else if( *pStr == "<DIR>" )
		{
			CString txt;
			DecodeItem( list ,txt );
			if( txt == "IN" )
			{
				p.m_DirKind = PARAMETER_DIR_IN;
			}
			else if(txt == "OUT" )
			{
				p.m_DirKind = PARAMETER_DIR_OUT;
			}
			else
			{
				p.m_DirKind = PARAMETER_DIR_INOUT;
			}
		}
		else
		{
			CString err;
			err= "DecodeParameterh error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return p;
}

RECT XMLToSource::DecodePos(StringList &list)
{
	RECT r;
	CString *pStr;
	while( list.GetUpperBound() >= 0)
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<POS>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
		else if(*pStr == "</POS>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if(*pStr == "<LEFT>" )
		{
			CString txt;
			DecodeItem( list , txt );
			r.left = atoi(LPCTSTR(txt));
		}
		else if(*pStr == "<TOP>" )
		{
			CString txt;
			DecodeItem( list , txt );
			r.top = atoi(LPCTSTR(txt));
		}
		else if(*pStr == "<RIGHT>" )
		{
			CString txt;
			DecodeItem( list , txt );
			r.right = atoi(LPCTSTR(txt));
		}
		else if(*pStr == "<BOTTOM>" )
		{
			CString txt;
			DecodeItem( list , txt );
			r.bottom = atoi(LPCTSTR(txt));
		}
		else
		{
			CString err;
			err= "DecodePos error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return r;
}

ShapeNamespace* XMLToSource::DecodeNamespace(StringList &list)
{
	ShapeNamespace *pSNS = new ShapeNamespace;
	CString *pStr;
	pSNS->m_pUMLNamespace = new UMLNamespace;
	if( !m_pRootShape ){
		m_pRootShape = pSNS;
		pSNS->m_pUMLNamespace->m_pNamespace = NULL;
	}
	//由于存在嵌套，所以必须把第一个去掉
	pStr = list.GetAt( 0 );
	delete pStr;
	list.RemoveAt( 0 );
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<NAMESPACE>" )
		{
			ShapeNamespace *p = DecodeNamespace( list );
			p->m_pUMLNamespace->m_pNamespace = pSNS->m_pUMLNamespace;
			p->m_pShapeNamespace = pSNS;
			pSNS->m_pOwnedShapes.Add( p );
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , pSNS->m_pUMLNamespace->name );
		}
		else if( *pStr == "<ID>" )
		{
			CString txt;
			DecodeItem( list , txt );
			pSNS->m_ID = atoi( LPCTSTR(txt) );
		}
		else if( *pStr == "</NAMESPACE>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if( *pStr == "<CLASS>" )
		{//解类
			ShapeClass *pSC = DecodeClass( list );
			pSNS->m_pOwnedShapes.Add( pSC );
		}
		else
		{
			CString err;
			err= "DecodeNamespace error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return pSNS;
}

ShapeClass* XMLToSource::DecodeClass(StringList &list)
{
	ShapeClass *pSC = new ShapeClass;
	CString *pStr;
	pSC->m_pClass = new UMLClass;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<CLASS>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			AddToLog("DecodeClass...");
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , pSC->m_pClass->UMLGeneralizableElement::name);
			AddToLog(CString("Name:")+pSC->m_pClass->UMLGeneralizableElement::name);
		}
		else if( *pStr == "<ID>")
		{
			CString txt;
			DecodeItem( list , txt );
			pSC->m_ID = atoi( LPCTSTR(txt));
		}
		else if( *pStr == "</CLASS>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			m_Classes.Add( pSC->m_pClass);
			AddToLog("DecodeClass Finished!");
			break;
		}
		else if( *pStr == "<ATTRIBUTE>")
		{
			UMLAttribute att = DecodeAttribute( list );
			pSC->m_pClass->m_Attributes.Add( att );
		}
		else if( *pStr == "<OPERATION>")
		{
			UMLOperation ope = DecodeOperation( list );
			pSC->m_pClass->m_Operations.Add( ope );
		}
		else if( *pStr == "<ACTIVITYGRAPH>" )
		{
			ShapeActivityGraph *pSAG = DecodeActivityGraph( list );
			pSC->m_pShapeActivityGraphs.Add( pSAG );
			pSAG->m_pActivityGraph->m_pClass = pSC->m_pClass;
		}
		else if( *pStr == "<MARK>" )
		{
			MarkClass *mc= new MarkClass;
			pSC->m_pMarkElement = mc;
			while( list.GetUpperBound() >= 0)
			{
				pStr = list.GetAt( 0 );
				if( *pStr == "<MARK>")
				{
					delete pStr;
					list.RemoveAt( 0 );
				}
				else if( *pStr == "</MARK>")
				{
					delete pStr;
					list.RemoveAt( 0 );
					break;
				}
				else if( *pStr == "<CREATEMAIN>" )
				{
					CString txt;
					DecodeItem( list ,txt );
					if( txt == "TRUE" )
					{
						mc->m_CreateMainFunc = TRUE;
					}
					else
					{
						mc->m_CreateMainFunc = FALSE;
					}
				}
				else if( *pStr == "<EXPRESSION>" )
				{
					DecodeItem( list , mc->m_MainExpression );
				}
				pSC->m_pMarkElement = mc;
			}
		}
		else
		{
			CString err;
			err= "Class error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return pSC;
}

UMLOperation XMLToSource::DecodeOperation(StringList &list)
{
	UMLOperation ope;
	CString *pStr;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<OPERATION>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
		else if ( *pStr == "</OPERATION>")
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , ope.name );
		}
		else if( *pStr == "<RETURNTYPE>")
		{
			CString txt;
			DecodeItem( list , txt );
			ope.m_ReturnType.m_pType = (UMLClass*)atoi(LPCTSTR(txt) );
		}
		else if( *pStr == "<VISIBILITY>" )
		{
			CString txt;
			DecodeItem( list , txt );
			if( txt == "PUBLIC" )
			{
				ope.m_Visibility = VISIBILITY_PUBLIC;
			}
			else if( txt == "PRIVATE" )
			{
				ope.m_Visibility = VISIBILITY_PRIVATE;
			}
			else if( txt == "PROTECTED" )
			{
				ope.m_Visibility = VISIBILITY_PROTECTED;
			}
			else
			{
				ope.m_Visibility = VISIBILITY_PACKAGE;
			}
		}
		else if( *pStr == "<PARAMETER>" )
		{
			UMLParameter pa = DecodeParameter( list );
			ope.m_TypedParameter.Add( pa );
		}
		else
		{
			CString err;
			err= "DecodeOperation error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			list.RemoveAt( 0 );
			delete pStr;
		}
	}
	return ope;
}

ShapeActivityGraph* XMLToSource::DecodeActivityGraph(StringList &list)
{
	ShapeActivityGraph *pSAG = new ShapeActivityGraph;
	UMLActivityGraph *pUAG = new UMLActivityGraph;
	pSAG->m_pActivityGraph = pUAG;
	CString *pStr;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<NAME>" )
		{
			DecodeItem(list , pUAG->name );
		}
		else if( *pStr == "<ACTIVITYGRAPH>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			AddToLog("DecodeActivityDiagram...");
		}
		else if( *pStr == "</ACTIVITYGRAPH>" )
		{
			delete pStr ;
			list.RemoveAt( 0 );
			AddToLog("DecodeActivityDiagram Finished!");
			break;
		}
		else if (*pStr == "<ID>" )
		{
			CString txt;
			DecodeItem( list , txt );
			AddToLog(CString("ID:")+txt);
			pSAG->m_ID = atoi(LPCTSTR(txt));
		}
		else if( *pStr == "<SWIMLANE>" )
		{
			ShapeSwimlane *p = DecodeSwimlane( list );
			pSAG->m_pShapeSwimlanes.Add( p );
			pSAG->m_pActivityGraph->m_Swimlanes.Add( p->m_pSwimlane );
		}
		else if( *pStr == "<ACTIVITY>" )
		{
			ShapeActivity *p = DecodeActivity( list );
			pSAG->m_pShapeActivities.Add( p );
			pSAG->m_pActivityGraph->m_Activities.Add( p->m_pUMLActivity );
			p->m_pUMLActivity->m_pOwnedElement = pSAG->m_pActivityGraph;
			p->m_pOwnedShape = pSAG;
		}
		else if( *pStr == "<EDGE>" )
		{
			ShapeTransEdge *edge = DecodeEdge( list );
			pSAG->m_pShapeTransEdges.Add(edge);
			pSAG->m_pActivityGraph->m_Edges.Add( edge->m_pTransEdge );
		}
		else if( *pStr == "<MARK>" )
		{
			MarkActivityGraph *m = new MarkActivityGraph;
			pSAG->m_pMarkElement = m;
			delete pStr;
			list.RemoveAt( 0 );
			m->m_UMLOperation = DecodeOperation( list );
			pStr = list.GetAt( 0 );
			delete pStr;
			list.RemoveAt( 0 );
		}
		else
		{
			CString err;
			err= "DecodeActivityGraph error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );

			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return pSAG;
}

ShapeSwimlane* XMLToSource::DecodeSwimlane(StringList &list)
{
	ShapeSwimlane *pSS = new ShapeSwimlane;
	CString *pStr;
	pSS->m_pSwimlane = new UMLSwimlane;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<ID>" )
		{
			CString txt;
			DecodeItem( list, txt );
			pSS->m_ID = atoi( LPCTSTR(txt ));
		}
		else if( *pStr == "<SWIMLANE>")
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
		else if( *pStr == "</SWIMLANE>")
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if( *pStr == "<POS>" )
		{
			pSS->m_Pos = DecodePos( list );
		}
		else if( *pStr == "<TYPE>" )
		{
			CString txt;
			DecodeItem( list , txt );
			pSS->m_pSwimlane->pType = (UMLClass*)atoi(LPCTSTR(txt));
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , pSS->m_pSwimlane->name );
		}
		else if( *pStr == "<ACTIVITY>" )
		{
			ShapeActivity *p = DecodeActivity( list );
			pSS->m_pActivities.Add( p );
			pSS->m_pSwimlane->m_Activities.Add( p->m_pUMLActivity );
			p->m_pOwnedShape = pSS;
			p->m_pUMLActivity->m_pOwnedElement = pSS->m_pSwimlane;
		}
		else if( *pStr == "<MARK>" )
		{
			MarkSwimlane *pMS = new MarkSwimlane;
			while( list.GetUpperBound() >= 0 )
			{
				pStr = list.GetAt( 0 );
				if( *pStr == "<GETMETHOD>" )
				{
					CString txt;
					DecodeItem(list ,txt );
					if( txt == "TRUE")
					{
						pMS->m_GetMethod  = TRUE;
					}
					else
					{
						pMS->m_GetMethod  = FALSE;
					}
				}
				else if( *pStr == "<SETMETHOD>" )
				{
					CString txt;
					DecodeItem(list ,txt );
					if( txt == "TRUE")
					{
						pMS->m_SetMethod = TRUE;
					}
					else
					{
						pMS->m_SetMethod = FALSE;
					}
				}
				else if( *pStr == "<TOPARAM>" )
				{
					CString txt;
					DecodeItem(list ,txt );
					if( txt == "TRUE")
					{
						pMS->m_ToParameter = TRUE;
					}
					else
					{
						pMS->m_ToParameter = FALSE;
					}
				}
				else if( *pStr == "<ATTRIBUTE>")
				{
					pMS->m_UMLAttribute = DecodeAttribute( list );
				}
				else if( *pStr == "<PARAMETER>" )
				{
					pMS->m_UMLParameter = DecodeParameter( list );
				}
				else if( *pStr == "<MARK>")
				{
					delete pStr;
					list.RemoveAt( 0 );
				}
				else if(*pStr == "</MARK>")
				{
					delete pStr;
					list.RemoveAt( 0 );
					break;
				}
			}
			pSS->m_pMarkElement = pMS;
		}
		else
		{
			CString err;
			err= "DecodeSwimlane error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			Beep(1000 , 200 );
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return pSS;
}

ShapeActivity* XMLToSource::DecodeActivity(StringList &list)
{
	ShapeActivity *pSA;
	CString *pStr;
	pSA = new ShapeActivity;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<ACTIVITY>" )
		{
			delete pStr;
			list.RemoveAt( 0 );
			AddToLog("DecodeActivity...");
		}
		else if( *pStr == "</ACTIVITY>")
		{
			delete pStr;
			list.RemoveAt( 0 );
			AddToLog("DecodeActivity Finished!");
			break;
		}
		else if( *pStr == "<ID>" )
		{
			CString txt;
			DecodeItem( list , txt );
			AddToLog(CString("ID:") + txt );
			pSA->m_ID = atoi( LPCTSTR(txt) );
		}
		else if( *pStr == "<POS>")
		{
			pSA->m_Pos = DecodePos( list );
		}
		else if( *pStr == "<KIND>" )
		{
			CString txt;
			DecodeItem( list ,txt );
			if( txt == "INIT" )
			{
				pSA->m_pUMLActivity = new UMLActionActivity;
				pSA->m_pUMLActivity->m_ActivityKind= ACTIVITY_INIT;
			}
			else if(txt == "FINISH" )
			{
				pSA->m_pUMLActivity = new UMLActionActivity;
				pSA->m_pUMLActivity->m_ActivityKind= ACTIVITY_FINISH;
			}
			else if( txt == "ACTION" )
			{
				pSA->m_pUMLActivity = new UMLActionActivity;
				pSA->m_pUMLActivity->m_ActivityKind= ACTIVITY_ACTION;
			}
			else if( txt == "SYNCH" )
			{
				pSA->m_pUMLActivity = new UMLSynchActivity;
				pSA->m_pUMLActivity->m_ActivityKind= ACTIVITY_SYNCH;
			}
			else if( txt == "SUB" )
			{
				pSA->m_pUMLActivity = new UMLSubActivity;
				pSA->m_pUMLActivity->m_ActivityKind= ACTIVITY_SUB;
			}
		}
		else if( *pStr == "<EXPRESSION>")
		{
			UMLActionActivity *p = (UMLActionActivity*)pSA->m_pUMLActivity;
			DecodeItem( list ,p->m_Expression );
		}
		else if( *pStr == "<SUBID>" )
		{
			CString txt;
			UMLSubActivity *p = (UMLSubActivity*)pSA->m_pUMLActivity;
			DecodeItem( list , txt );
			p->m_pActivityGraph = (UMLActivityGraph*)atoi( LPCTSTR(txt) );
		}
		else if( *pStr == "<NAME>" )
		{
			DecodeItem( list , pSA->m_pUMLActivity->name );
		}
		else if( *pStr == "<MARK>")
		{
			MarkSynch* pMS = new MarkSynch;
			while(list.GetUpperBound()>= 0 )
			{
				pStr = list.GetAt( 0 );
				if( *pStr == "<MARK>" )
				{
					delete pStr;
					list.RemoveAt( 0 );
				}
				else if( *pStr == "</MARK>" )
				{
					delete pStr;
					list.RemoveAt( 0 );
					break;
				}
				else if( *pStr == "<NAME>" )
				{
					DecodeItem( list , pMS->m_SynchName );
				}
				else if( *pStr == "<TYPE>" )
				{
					DecodeItem( list ,pMS->m_SynchType );
				}
				else
				{
					CString txt;
					DecodeItem( list , txt );
					if( txt == "PUBLIC" )
					{
						pMS->m_SynchVisibility = VISIBILITY_PUBLIC;
					}
					else if( txt == "PRIVATE" )
					{
						pMS->m_SynchVisibility = VISIBILITY_PRIVATE;
					}
					else if( txt == "PROTECTED" )
					{
						pMS->m_SynchVisibility = VISIBILITY_PROTECTED;
					}
					else
					{
						pMS->m_SynchVisibility = VISIBILITY_PACKAGE;
					}
				}
			}
			pSA->m_pMarkElement = pMS;
		}
		else
		{
			CString err;
			err= "DecodeActivity error!(";
			err += *pStr;
			err += ")";
			AddToLog( err );
			delete pStr;
			list.RemoveAt( 0 );
			Beep(1000 , 200 );
		}
	}
	return pSA;
}

ShapeTransEdge* XMLToSource::DecodeEdge(StringList &list)
{
	ShapeTransEdge *edge = new ShapeTransEdge;
	CString *pStr;
	edge->m_pTransEdge = new UMLTransEdge;
	while( list.GetUpperBound() >= 0 )
	{
		pStr = list.GetAt( 0 );
		if( *pStr == "<ID>" )
		{
			CString txt;
			DecodeItem(list ,txt );
			edge->m_ID = atoi( LPCTSTR(txt));
		}
		else if( *pStr == "<EDGE>")
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
		else if( *pStr == "</EDGE>")
		{
			delete pStr;
			list.RemoveAt( 0 );
			break;
		}
		else if( *pStr == "<POINTS>" )
		{
			POINT pt;
			while( list.GetUpperBound()>= 0)
			{
				pStr = list.GetAt( 0 );
				if( *pStr == "<POINTS>" )
				{
					delete pStr;
					list.RemoveAt( 0 );
				}
				else if( *pStr == "</POINTS>" )
				{
					delete pStr ;
					list.RemoveAt( 0 );
					break;
				}
				else if( *pStr == "<POS>" )
				{
					delete pStr;
					list.RemoveAt( 0 );
				}
				else if( *pStr == "</POS>" )
				{
					delete pStr;
					list.RemoveAt( 0 );
					edge->m_Points.Add( pt );
				}
				else if( *pStr == "<X>" )
				{
					CString txt;
					DecodeItem( list , txt );
					pt.x = atoi( LPCTSTR(txt));
				}
				else if( *pStr == "<Y>" )
				{
					CString txt;
					DecodeItem( list , txt );
					pt.y = atoi( LPCTSTR(txt));
				}
				else
				{
					CString err;
					err= "Decode Edge points error!(";
					err += *pStr;
					err += ")";
					AddToLog( err );
					Beep(1000 , 200 );
					delete pStr;
					list.RemoveAt( 0 );
				}
			}
		}
		else if( *pStr == "<TAIL>" )
		{
			CString txt;
			DecodeItem(list,txt );
			edge->m_pActivity[ 0 ] = (ShapeActivity*)atoi((LPCTSTR(txt)));
		}
		else if( *pStr == "<HEAD>" )
		{
			CString txt;
			DecodeItem(list,txt );
			edge->m_pActivity[ 1 ] = (ShapeActivity*)atoi((LPCTSTR(txt)));
		}
		else if( *pStr == "<NAME>")
		{
			DecodeItem( list , edge->m_pTransEdge->name );
		}
		else if( *pStr == "<CONDICTION>" )
		{
			DecodeItem( list ,edge->m_pTransEdge->m_BoolExpression );
		}
		else
		{
			delete pStr;
			list.RemoveAt( 0 );
		}
	}
	return edge;
}

void XMLToSource::Do(CString fn)
{
	CFile file;
	if( file.Open( fn ,CFile::modeRead ) )
	{
		StringList items;
		CString str;
		do
		{
			ReadALine(file,str);
			DecodeALine( items, str );
		}while(str.GetLength());
		file.Close();
		if( items.GetUpperBound()>=0)
		{
			DecodeNamespace( items );
		}
//设置所有的属性类型,操作类型，参数类型
		for( int i = 0 ; i <= ShapeElement::m_AllShapes.GetUpperBound() ; i ++ )
		{
			ShapeElement *s = ShapeElement::m_AllShapes.GetAt( i );
			if( s->GetType() == SHAPE_CLASS )
			{
				ShapeClass *pSC = (ShapeClass*)s;
				//设置类的属性
				for( int j = 0 ; j < pSC->m_pClass->m_Attributes.GetSize() ; j ++ )
				{
					UMLAttribute att = pSC->m_pClass->m_Attributes.GetAt( j );
					int id = (int)att.m_pType;
					s = ShapeElement::Find( id );
					att.m_pType = ((ShapeClass*)s)->m_pClass;
					pSC->m_pClass->m_Attributes.SetAt( j , att );
				}
				//设置类的操作
				for( j = 0 ; j < pSC->m_pClass->m_Operations.GetSize() ; j ++ )
				{
					UMLOperation ope = pSC->m_pClass->m_Operations.GetAt( j );
					int id = (int ) ope.m_ReturnType.m_pType;
					s = ShapeElement::Find( id );
					ope.m_ReturnType.m_pType = ((ShapeClass*)s)->m_pClass;
					for ( int k = 0 ; k < ope.m_TypedParameter.GetSize() ;k++ )
					{
						UMLParameter pa = ope.m_TypedParameter.GetAt( k ) ;
						id = (int ) pa.m_pType;
						s = ShapeElement::Find( id );
						pa.m_pType = ((ShapeClass*)s)->m_pClass;
						ope.m_TypedParameter.SetAt( k , pa );
					}
					pSC->m_pClass->m_Operations.SetAt( j , ope );
				}
			}
			else if( s->GetType() == SHAPE_SWIMLANE )
			{
				ShapeSwimlane *pSS = (ShapeSwimlane*)s;
				MarkSwimlane *pMS = (MarkSwimlane*)pSS->m_pMarkElement;
				int id = (int )pSS->m_pSwimlane->pType;
				s = ShapeElement::Find( id );
				pSS->m_pSwimlane->pType = ((ShapeClass*)s)->m_pClass;
				if( pMS->m_ToParameter )
				{
					id = (int )pMS->m_UMLParameter.m_pType ;
					s= ShapeElement::Find( id );
					pMS->m_UMLParameter.m_pType = ((ShapeClass*)s)->m_pClass;
				}
				else
				{
					id = (int )pMS->m_UMLAttribute.m_pType ;
					s= ShapeElement::Find( id );
					pMS->m_UMLAttribute.m_pType = ((ShapeClass*)s)->m_pClass;
				}
			}
		}

		//设置活动的转换边
		for( i = 0 ; i <= ShapeElement::m_AllShapes.GetUpperBound() ; i ++ )
		{
			ShapeElement *s = ShapeElement::m_AllShapes.GetAt( i );
			if( s->GetType() == SHAPE_TRANSEDGE )
			{
				ShapeTransEdge *e = (ShapeTransEdge*)s;
				int id1,id2;
				id1 = (int)e->m_pActivity[ 0 ];
				id2 = (int)e->m_pActivity[ 1 ];
				e->m_pActivity[ 0 ] =(ShapeActivity*) ShapeElement::Find( id1 );
				e->m_pActivity[ 1 ] =(ShapeActivity*) ShapeElement::Find( id2 );
				e->m_pTransEdge->m_pActivities[ 0 ] = e->m_pActivity[ 0 ]->m_pUMLActivity;
				e->m_pTransEdge->m_pActivities[ 1 ] = e->m_pActivity[ 1 ]->m_pUMLActivity;
				e->m_pActivity[ 0 ]->m_pOutTransEdges.Add( e );
				e->m_pActivity[ 0 ]->m_pUMLActivity->m_OutTransEdge.Add(e->m_pTransEdge );
				e->m_pActivity[ 1 ]->m_pInTransEdges.Add( e );
				e->m_pActivity[ 1 ]->m_pUMLActivity->m_InTransEdge.Add(e->m_pTransEdge );
			}
		}
		//设置泳道
	}
}
