From: Daniel C. \(kzu\) <dca...@us...> - 2005-10-31 12:28:33
|
Update of /cvsroot/mvp-xml/Design/v2/src/CustomTools/XGen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32177/v2/src/CustomTools/XGen Modified Files: XGenRunner.cs XGenTool.cs XmlSerializerGenerator.cs Log Message: Updated for multiple types. Index: XGenRunner.cs =================================================================== RCS file: /cvsroot/mvp-xml/Design/v2/src/CustomTools/XGen/XGenRunner.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- XGenRunner.cs 31 Oct 2005 09:31:24 -0000 1.1 +++ XGenRunner.cs 31 Oct 2005 12:28:17 -0000 1.2 @@ -11,12 +11,18 @@ /// <summary> /// Generates the code for the received type. /// </summary> - public XGenRunner(string outputFile, string forType, string targetNamespace) + public XGenRunner(string outputFile, string[] forTypes, string targetNamespace) { using (StreamWriter writer = new StreamWriter(outputFile)) { + Type[] types = new Type[forTypes.Length]; + for (int i = 0; i < forTypes.Length; i++) + { + types[i] = Type.GetType(forTypes[i], true); + } + writer.Write(XmlSerializerGenerator.GenerateCode( - Type.GetType(forType, true), targetNamespace)); + types, targetNamespace)); } } } Index: XGenTool.cs =================================================================== RCS file: /cvsroot/mvp-xml/Design/v2/src/CustomTools/XGen/XGenTool.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- XGenTool.cs 31 Oct 2005 11:49:18 -0000 1.4 +++ XGenTool.cs 31 Oct 2005 12:28:17 -0000 1.5 @@ -105,16 +105,29 @@ appSetup.ApplicationBase = outputPath; appSetup.ConfigurationFile = ConfigFile; - foreach (Selection selection in selections) - { - GetSerializationCode(appSetup, output, selection); - } + string[] targetTypes = GetTypesFromSelection(selections); + + GetSerializationCode(appSetup, output, targetTypes); + + DeleteDesignFromOutput(outputPath); SaveSelections(selections); return output.ToString(); } + private string[] GetTypesFromSelection(SelectionCollection selections) + { + string assemblyName = CurrentItem.ContainingProject.Properties.Item("AssemblyName").Value.ToString(); + string[] types = new string[selections.Count]; + for (int i = 0; i < selections.Count; i++) + { + types[i] = selections[i].ClassName + ", " + assemblyName; + } + + return types; + } + private void SaveSelections(SelectionCollection selections) { string key = BuildItemKey(CurrentItem); @@ -122,7 +135,7 @@ CurrentItem.ContainingProject.Globals.set_VariablePersists(key, true); } - private void GetSerializationCode(AppDomainSetup appSetup, StringBuilder output, Selection selection) + private void GetSerializationCode(AppDomainSetup appSetup, StringBuilder output, string[] targetTypes) { string codefile = Path.GetTempFileName(); AppDomain domain = AppDomain.CreateDomain(appSetup.ApplicationName, null, appSetup); @@ -134,9 +147,8 @@ typeof(XGenRunner).FullName, false, 0, null, new object[] { codefile, - selection.ClassName + ", " + - CurrentItem.ContainingProject.Properties.Item("AssemblyName").Value.ToString(), - selection.TargetNamespace }, + targetTypes, + base.FileNameSpace }, null, null, null); using (StreamReader reader = new StreamReader(codefile)) @@ -193,6 +205,20 @@ } } + private void DeleteDesignFromOutput(string outputPath) + { + string asmfile = GetAssemblyPath(Assembly.GetExecutingAssembly()); + try + { + File.Delete(Path.Combine(outputPath, Path.GetFileName(asmfile))); + } + catch (Exception ex) + { + // May already exist, be locked, etc. + System.Diagnostics.Debug.WriteLine(ex.ToString()); + } + } + private void ThrowIfNoClassFound() { if (CurrentItem.FileCodeModel == null || CurrentItem.FileCodeModel.CodeElements == null) Index: XmlSerializerGenerator.cs =================================================================== RCS file: /cvsroot/mvp-xml/Design/v2/src/CustomTools/XGen/XmlSerializerGenerator.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- XmlSerializerGenerator.cs 31 Oct 2005 11:49:18 -0000 1.2 +++ XmlSerializerGenerator.cs 31 Oct 2005 12:28:17 -0000 1.3 @@ -6,6 +6,7 @@ using System.Reflection; using System.Text.RegularExpressions; using System.Xml.Serialization; +using System.Text; namespace Mvp.Xml.Design.CustomTools.XGen { @@ -224,7 +225,19 @@ #region Private helper methods - public static string GenerateCode(Type type, string targetNamespace) + public static string GenerateCode(Type[] types, string targetNamespace) + { + string output = String.Empty; + + foreach (Type type in types) + { + output += GenerateCodeForType(type, targetNamespace, output); + } + + return output; + } + + private static string GenerateCodeForType(Type type, string targetNamespace, string previousOutput) { string output = GetXmlSerializerOutput(type); @@ -244,7 +257,7 @@ output = AppendReaderWriterMembersToObjectSerializer(type, output, rootObject); - CodeTypeDeclaration readerDeclaration = CreateAndAddEventRisingReader(targetNamespace, output, ns, rootObject); + CodeTypeDeclaration readerDeclaration = CreateAndAddEventRisingReader(targetNamespace, ns, rootObject, output, previousOutput); output = PrivateReadMethodsToProtectedVirtual(output); CodeTypeDeclaration writerDeclaration = CreateAndAddTypedWriter(targetNamespace, ns, rootObject); @@ -273,7 +286,6 @@ #else return output; #endif - //"\r\n#pragma warning disable 0642, 0219\r\n" + endlines.ToString() + "#pragma warning restore 0642, 0219"; } private static string MakeSerializerPartial(string output, string rootObject) @@ -371,7 +383,7 @@ return output; } - private static CodeTypeDeclaration CreateAndAddTypedWriter(string targetNamespace, System.CodeDom.CodeNamespace ns, string rootObject) + private static CodeTypeDeclaration CreateAndAddTypedWriter(string targetNamespace, CodeNamespace ns, string rootObject) { CodeTypeDeclaration writerDeclaration = new CodeTypeDeclaration(rootObject + "Writer"); writerDeclaration.BaseTypes.Add(rootObject + "Serializer.BaseWriter"); @@ -381,7 +393,8 @@ return writerDeclaration; } - private static CodeTypeDeclaration CreateAndAddEventRisingReader(string targetNamespace, string output, System.CodeDom.CodeNamespace ns, string rootObject) + private static CodeTypeDeclaration CreateAndAddEventRisingReader(string targetNamespace, CodeNamespace ns, + string rootObject, string output, string previousOutput) { CodeTypeDeclaration readerDeclaration = new CodeTypeDeclaration(rootObject + "Reader"); readerDeclaration.BaseTypes.Add(rootObject + "Serializer.BaseReader"); @@ -401,21 +414,25 @@ continue; } - // Create a delegate for the event to expose. - // public delegate void [objectBeingRead]DeserializedHandler(Mvp.Xml.Design.Tests.Customer customer); - CodeTypeDelegate del = new CodeTypeDelegate(objectBeingRead + "DeserializedHandler"); - del.Parameters.Add(new CodeParameterDeclarationExpression( - readMethodMatch.Groups[ReadMethodReturnType].Value, - MakeFirstLower(objectBeingRead))); - del.Comments.Add(new CodeCommentStatement("/// <remarks/>", true)); - ns.Types.Add(del); + string delegateName = objectBeingRead + "DeserializedHandler"; + if (!previousOutput.Contains(delegateName)) + { + // Create a delegate for the event to expose. + // public delegate void [objectBeingRead]DeserializedHandler(Mvp.Xml.Design.Tests.Customer customer); + CodeTypeDelegate del = new CodeTypeDelegate(delegateName); + del.Parameters.Add(new CodeParameterDeclarationExpression( + readMethodMatch.Groups[ReadMethodReturnType].Value, + MakeFirstLower(objectBeingRead))); + del.Comments.Add(new CodeCommentStatement("/// <remarks/>", true)); + ns.Types.Add(del); + } // Expose event. // public event [objectBeingRead]DeserializedHandler [objectBeingRead]Deserialized; CodeMemberEvent ev = new CodeMemberEvent(); ev.Name = objectBeingRead + "Deserialized"; ev.Attributes = MemberAttributes.Public; - ev.Type = new CodeTypeReference(del.Name); + ev.Type = new CodeTypeReference(delegateName); ev.Comments.Add(new CodeCommentStatement("/// <remarks/>", true)); readerDeclaration.Members.Add(ev); @@ -424,7 +441,7 @@ TemplateMethodOverride, readMethodMatch.Groups[ReadMethodReturnType].Value, readMethodMatch.Groups[ReadMethodName].Value, - del.Name, + delegateName, ev.Name, readMethodMatch.Groups[ReadMethodArguments].Value, // Arguments contain type + parameter name. Remove type for method call. |