⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data2xml.cs

📁 UML设计相关的源码。作UML相关开发的不容错过。
💻 CS
📖 第 1 页 / 共 4 页
字号:

/* ---------------------------------------------------------------------------------------------------
 *  
 *                          PocketUML v0.01.0016
 *  
 *        
 *    Written by Jie Tang.
 *    Bug report : jiet@msn.com
 *  
 *
 * Copyright 2002 James <jiet@msn.com>
 * All rights reserved.
 *
 * This source file(s) may be redistributed unmodified by any means
 * PROVIDING they are not sold for profit without the authors expressed
 * written consent, and providing that this notice and the authors name
 * and all copyright notices remain intact.
 *
 * Any use of the software in source or binary forms, with or without
 * modification, must include, in the user documentation ("About" box and
 * printed documentation) and internal comments to the code, notices to
 * the end user as follows:
 *
 * "Portions Copyright 2002 Tang Jie
 *
 * An email letting me know that you are using it would be nice as well.
 * That's not much to ask considering the amount of work that went into
 * this.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED. USE IT AT YOUT OWN RISK. THE AUTHOR ACCEPTS NO
 * LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
 *
 */
//-----------------------------------------------------------------------------------------------------
//										Update Information.
//-----------------------------------------------------------------------------------------------------

///
/// PocketUML Namespace
/// Read code data to XML Files.
/// 
/// PocketUML v0.1
/// Created by Jie Tang  04/10/2002.
///


///
/// PocketUML Namespace
/// Parse UML Data from c# project source code...
/// 
/// PocketUML v0.1
/// Created by James Tang  04/10/2002.
/// Bug report : JieT@Msn.com
namespace PocketUML
{
	///
	/// PocketUML.Data's Data Operation namespace
	/// Operate the data
	namespace DataOperator
	{	
		using System;
		using System.Xml;
		using PocketUML.Data;

		/// *********************************************************** ///
		///                    UPDATE    RECORD                         ///
		/// *********************************************************** ///
		/// Created 05/10/2002. James Tang                              ///
		/// *********************************************************** ///
		/// Fixed problem : can't output & read variable data 
		/// Data 08/10/2002
		/// *********************************************************** ///

		/// <summary>
		/// Convert Data to XML files or read data From XML Files
		/// </summary>
		public class Data2XML
		{

			#region Write Code Data to File
			/// <summary>
			/// Write Code data to file
			/// </summary>
			/// <param name="fileName"></param>
			/// <param name="codeData"></param>
			public static void WriteToFile( String fileName, CodeData codeData )
			{
				try
				{
					XmlTextWriter xmlWriter = new XmlTextWriter( fileName, System.Text.Encoding.UTF8 );
				
					// Write file header
					WriteHeader( xmlWriter );
				
					// First Write Solutions
					xmlWriter.WriteStartElement( "Solution" );
					SolutionData sluData = (SolutionData) codeData.SolutionList[0];
					xmlWriter.WriteAttributeString( "Name", sluData.Name );
			
			
					for( int nProjectIndex = 0 ; nProjectIndex < sluData.Element.Count; nProjectIndex ++ )
					{
						// Then Write Projects
						ProjectData prjData  = (ProjectData) sluData.Element[nProjectIndex]; 
				
						xmlWriter.WriteStartElement( "Project" );
						xmlWriter.WriteAttributeString( "Name", prjData.Name );

						for( int nProjectItemIndex = 0 ; nProjectItemIndex < prjData.Element.Count ; nProjectItemIndex ++ )
						{
							ProjectItemData prjItemData = (ProjectItemData) prjData.Element[nProjectItemIndex];

							// Get Code Elements Data
							// Only one element in those collections
							CodeModelData codeModelData = (CodeModelData) prjItemData.Element[0];
							CodeElementsData codeElementData = (CodeElementsData) codeModelData.Element[0];
							//*****************MODIFY BY 06/10/2002*********************//
							// Add Output ProjectItem Data                              //
							// Used to generate code                                    //
							//**********************************************************//
							xmlWriter.WriteStartElement( "ProjectItem" );
							xmlWriter.WriteAttributeString( "Name", prjItemData.Name );
						
							for( int nCodeElementIndex = 0 ; nCodeElementIndex < codeElementData.Element.Count ; nCodeElementIndex ++ )
							{
								WriteElements( xmlWriter, (ElementData) codeElementData.Element[nCodeElementIndex] );
							}

							xmlWriter.WriteFullEndElement();   // Project Item Element End
						}
						xmlWriter.WriteFullEndElement();  // Project Element End
					}
					xmlWriter.WriteFullEndElement();  // Solution Element End

					xmlWriter.Close();
				}
				catch ( Exception e )
				{
				
				}
			}

			/// <summary>
			/// Parse data elements and writer it into xml files
			/// </summary>
			/// <param name="xmlWriter"></param>
			/// <param name="elementData"></param>
			private static void WriteElements( XmlTextWriter xmlWriter, ElementData elementData )
			{
				// Write each elements
				if( elementData.ElementType == enumElementType.elementTypeUnknow )
				{
					// Initialization a new element data object
					xmlWriter.WriteStartElement( "UnknowType" );
					xmlWriter.WriteAttributeString( "Name", elementData.Name );
					xmlWriter.WriteFullEndElement();
				}
				else if( elementData.ElementType == enumElementType.elementTypeNameSpace )
				{	
					///
					/// Namespace Type
					///
					WriteNameSpaceData( xmlWriter, (NameSpaceData) elementData );

				}
				else if( elementData.ElementType == enumElementType.elementTypeClass )
				{
					///
					/// Class Type
					///
					WriteClassData( xmlWriter, (ClassData) elementData );
				}
				else if( elementData.ElementType == enumElementType.elementTypeDelegate )
				{
					///
					/// Delegate Type
					///
					WriteDelegateData( xmlWriter, (DelegateData) elementData );
				
				}
				else if( elementData.ElementType == enumElementType.elementTypeEnum )
				{
					///
					/// Enum Type
					///
					WriteEnumData( xmlWriter, (EnumData) elementData );
				
				}
				else if( elementData.ElementType == enumElementType.elementTypeFunction )
				{
					///
					/// Function Type
					///
					WriteFunctionData( xmlWriter, (FunctionData) elementData );

				}
				else if( elementData.ElementType == enumElementType.elementTypeInterface )
				{
					///
					/// Interface Type
					///
					WriteInterfaceData( xmlWriter, (InterfaceData) elementData );

				}
				else if( elementData.ElementType == enumElementType.elementTypeProperty )
				{
					///
					/// Property Type
					/// 
					WritePropertyData( xmlWriter, (PropertyData) elementData);

				}
				else if( elementData.ElementType == enumElementType.elementTypeStruct )
				{
					///
					/// Struct Type
					///
					WriteStructData( xmlWriter, (StructData) elementData);
				
				}
				else if( elementData.ElementType == enumElementType.elementTypeParameter )
				{	
					///
					/// Parameter Type
					///
					WriteParameterData( xmlWriter, (ParameterData) elementData );
				}
				else if( elementData.ElementType == enumElementType.elementTypeVariable )
				{	
					///
					/// Variable Type
					///
					WriteVariableData( xmlWriter, (VariableData) elementData );
				}
			}

			private static void WriteParameterData( XmlTextWriter xmlWriter, ParameterData parameterData )
			{
				///
				/// Parameter Type
				///
				// Initialization a new element data object
				xmlWriter.WriteStartElement( "Parameter" );
				xmlWriter.WriteAttributeString( "Name", parameterData.Name );

				if( IsWriteObjectInfo == true )
				{
					xmlWriter.WriteAttributeString( "Parent", parameterData.Parent.ToString() );
					xmlWriter.WriteAttributeString( "VSObjectReference", parameterData.VSObjectReference.ToString() );
				}
				xmlWriter.WriteFullEndElement();
			}

			private static void WriteClassData( XmlTextWriter xmlWriter, ClassData classData )
			{
				///
				/// Class Type
				///
				xmlWriter.WriteStartElement( "Class" );
				xmlWriter.WriteAttributeString( "Name", classData.Name );
				xmlWriter.WriteAttributeString( "IsAbstract", XmlConvert.ToString(classData.IsAbstract) );
				xmlWriter.WriteAttributeString( "AccessType", classData.AccessType.ToString() );
				xmlWriter.WriteAttributeString( "Comment", classData.DocComment );
				if( IsWriteObjectInfo == true )
				{
					xmlWriter.WriteAttributeString( "Parent", classData.Parent.ToString() );
					xmlWriter.WriteAttributeString( "VSObjectReference", classData.VSObjectReference.ToString() );
					xmlWriter.WriteAttributeString( "ParentNameSpace", classData.ParentNameSpaceName );
				}

				// Bulid parent class name to ClassData.BaseList
				if( classData.BaseList.Count > 0 )
				{
					for( int i = 0; i < classData.BaseList.Count; i ++ )
					{
						xmlWriter.WriteAttributeString( "BaseClass", classData.BaseList[i].ToString() );
					}
				}

				// Check weather this class contain members
				if( classData.Element.Count > 0 )
				{
					for( int i = 0; i < classData.Element.Count; i ++ )
					{
						WriteElements( xmlWriter, (ElementData) classData.Element[i] );
					}
				}
				xmlWriter.WriteFullEndElement();
			}

			private static void WriteDelegateData( XmlTextWriter xmlWriter, DelegateData delegateData )
			{
				///
				/// Delegate Type
				///
				xmlWriter.WriteStartElement( "Delegate" );
				xmlWriter.WriteAttributeString( "Name", delegateData.Name );
				xmlWriter.WriteAttributeString( "AccessType", delegateData.AccessType.ToString() );
				xmlWriter.WriteAttributeString( "Comment", delegateData.DocComment );
				if( IsWriteObjectInfo == true )
				{
					xmlWriter.WriteAttributeString( "Parent", delegateData.Parent.ToString() );
					xmlWriter.WriteAttributeString( "VSObjectReference", delegateData.VSObjectReference.ToString() );
					xmlWriter.WriteAttributeString( "ParentNameSpace", delegateData.ParentNameSpaceName );
				}

				// Save the Phototypes data 
				xmlWriter.WriteStartElement( "Phototypes" );
				DelegateData        destination = delegateData;
				{
					xmlWriter.WriteAttributeString( "ClassName",destination.PhotoType.className );
					xmlWriter.WriteAttributeString( "fullName",destination.PhotoType.fullName );        
					xmlWriter.WriteAttributeString( "noName",destination.PhotoType.noName );          
					xmlWriter.WriteAttributeString( "paramDefaultValus",destination.PhotoType.paramDefaultValus );
					xmlWriter.WriteAttributeString( "paramNames",destination.PhotoType.paramNames );
					xmlWriter.WriteAttributeString( "paramTypes",destination.PhotoType.paramTypes );
					xmlWriter.WriteAttributeString( "phototypeInitExpression",destination.PhotoType.phototypeInitExpression );
					xmlWriter.WriteAttributeString( "phototypeType",destination.PhotoType.phototypeType );
					xmlWriter.WriteAttributeString( "uniqueSignature",destination.PhotoType.uniqueSignature );
				}
				xmlWriter.WriteFullEndElement(); // Phototypes end
			
				// Get the parameter data
				if( delegateData.Element.Count > 0 )
				{
					for( int i = 0 ; i < delegateData.Element.Count ; i ++ )
					{
						WriteElements( xmlWriter, (ElementData) delegateData.Element[i] );
					}
				}

				xmlWriter.WriteFullEndElement(); // Delegate end
			}

			private static void WriteEnumData( XmlTextWriter xmlWriter, EnumData enumData )
			{
				///
				/// Enum Type
				///
				xmlWriter.WriteStartElement( "Enum" );
				xmlWriter.WriteAttributeString( "Name", enumData.Name );
				xmlWriter.WriteAttributeString( "AccessType", enumData.AccessType.ToString() );
				xmlWriter.WriteAttributeString( "Comment", enumData.DocComment );
				if( IsWriteObjectInfo == true )
				{
					xmlWriter.WriteAttributeString( "Parent", enumData.Parent.ToString() );
					xmlWriter.WriteAttributeString( "VSObjectReference", enumData.VSObjectReference.ToString() );
					xmlWriter.WriteAttributeString( "ParentNameSpace", enumData.ParentNameSpaceName );
				}
				// Check weather this class contain members
				if( enumData.Element.Count > 0 )
				{
					for( int i = 0; i < enumData.Element.Count; i ++ )
					{
						WriteElements( xmlWriter, (ElementData) enumData.Element[i] );
					}
				}
				xmlWriter.WriteFullEndElement();
			}

			private static void WriteFunctionData( XmlTextWriter xmlWriter, FunctionData functionData )
			{
				///
				/// Function Type
				///
				xmlWriter.WriteStartElement( "Function" );
				xmlWriter.WriteAttributeString( "Name", functionData.Name );
				xmlWriter.WriteAttributeString( "FunctionType", functionData.FunctionType.ToString() );
				xmlWriter.WriteAttributeString( "AccessType", functionData.AccessType.ToString() );
				xmlWriter.WriteAttributeString( "Comment", functionData.DocComment );
				xmlWriter.WriteAttributeString( "CanOverrided", XmlConvert.ToString(functionData.CanOverride ));
				xmlWriter.WriteAttributeString( "IsOverloaded", XmlConvert.ToString(functionData.IsOverloaded ));
				xmlWriter.WriteAttributeString( "IsShared", XmlConvert.ToString(functionData.IsShared ));
				if( IsWriteObjectInfo == true )
				{
					xmlWriter.WriteAttributeString( "Parent", functionData.Parent.ToString() );
					xmlWriter.WriteAttributeString( "VSObjectReference", functionData.VSObjectReference.ToString() );
					xmlWriter.WriteAttributeString( "ParentNameSpace", functionData.ParentNameSpaceName );
				}

				// Save the Phototypes data 
				xmlWriter.WriteStartElement( "Phototypes" );
				FunctionData        destination = functionData;
				{
					xmlWriter.WriteAttributeString( "ClassName",destination.PhotoType.className );
					xmlWriter.WriteAttributeString( "fullName",destination.PhotoType.fullName );        
					xmlWriter.WriteAttributeString( "noName",destination.PhotoType.noName );          
					xmlWriter.WriteAttributeString( "paramDefaultValus",destination.PhotoType.paramDefaultValus );
					xmlWriter.WriteAttributeString( "paramNames",destination.PhotoType.paramNames );
					xmlWriter.WriteAttributeString( "paramTypes",destination.PhotoType.paramTypes );
					xmlWriter.WriteAttributeString( "phototypeInitExpression",destination.PhotoType.phototypeInitExpression );
					xmlWriter.WriteAttributeString( "phototypeType",destination.PhotoType.phototypeType );
					xmlWriter.WriteAttributeString( "uniqueSignature",destination.PhotoType.uniqueSignature );
				}
				xmlWriter.WriteFullEndElement(); // Phototypes end

				// Get the parameter data
				if( functionData.Element.Count > 0 )
				{
					for( int i = 0 ; i < functionData.Element.Count ; i ++ )
					{
						WriteElements( xmlWriter, (ElementData) functionData.Element[i] );
					}
				}

				xmlWriter.WriteFullEndElement();  // Function end
			}

			private static void WriteInterfaceData( XmlTextWriter xmlWriter, InterfaceData interfaceData )
			{
				///
				/// Interface Type
				///

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -