From: Daniel C. \(kzu\) <dca...@us...> - 2005-01-09 20:20:27
|
Update of /cvsroot/mvp-xml/Design/v1/src/CustomTools/XGen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30050/v1/src/CustomTools/XGen Modified Files: ClassPicker.cs ClassPicker.resx XGenTool.cs Log Message: Enabled the Mvp.Xml.XsdInfer custom tool to infer a schema automatically from a class model. Updated the XGen custom tool so that it shows a class picker when multiple classes exist in a file, to determine the roots of deserialization processors. Updated description of the XmlValidate description. Index: ClassPicker.cs =================================================================== RCS file: /cvsroot/mvp-xml/Design/v1/src/CustomTools/XGen/ClassPicker.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ClassPicker.cs 21 Oct 2004 20:42:06 -0000 1.1 +++ ClassPicker.cs 9 Jan 2005 20:20:14 -0000 1.2 @@ -3,18 +3,22 @@ using System.Collections; using System.ComponentModel; using System.Windows.Forms; +using System.Text; + +using EnvDTE; namespace Mvp.Xml.Design.CustomTools.XGen { - /// <summary> - /// Summary description for ClassPicker. - /// </summary> - public class ClassPicker : System.Windows.Forms.Form + internal class ClassPicker : System.Windows.Forms.Form { + #region Designer Stuff private System.Windows.Forms.Label label1; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnAccept; - private System.Windows.Forms.CheckedListBox lstClasses; + private Mvp.Xml.Design.ListViewEx lstClasses; + private System.Windows.Forms.TextBox txtEditor; + private System.Windows.Forms.ColumnHeader colClass; + private System.Windows.Forms.ColumnHeader colTargetNs; /// <summary> /// Required designer variable. /// </summary> @@ -54,19 +58,15 @@ /// </summary> private void InitializeComponent() { - this.lstClasses = new System.Windows.Forms.CheckedListBox(); this.label1 = new System.Windows.Forms.Label(); this.btnCancel = new System.Windows.Forms.Button(); this.btnAccept = new System.Windows.Forms.Button(); + this.lstClasses = new Mvp.Xml.Design.ListViewEx(); + this.colClass = new System.Windows.Forms.ColumnHeader(); + this.colTargetNs = new System.Windows.Forms.ColumnHeader(); + this.txtEditor = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // - // lstClasses - // - this.lstClasses.Location = new System.Drawing.Point(8, 56); - this.lstClasses.Name = "lstClasses"; - this.lstClasses.Size = new System.Drawing.Size(384, 214); - this.lstClasses.TabIndex = 0; - // // label1 // this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -74,7 +74,7 @@ | System.Windows.Forms.AnchorStyles.Right))); this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(384, 48); + this.label1.Size = new System.Drawing.Size(624, 48); this.label1.TabIndex = 1; this.label1.Text = "Select the classes you will use as the root of a deserialization process. A custo" + "m XmlSerializer and supporting classes will be generated for each of the selecte" + @@ -83,7 +83,8 @@ // btnCancel // this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnCancel.Location = new System.Drawing.Point(316, 280); + this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.btnCancel.Location = new System.Drawing.Point(556, 280); this.btnCancel.Name = "btnCancel"; this.btnCancel.TabIndex = 2; this.btnCancel.Text = "&Cancel"; @@ -91,19 +92,61 @@ // btnAccept // this.btnAccept.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnAccept.Location = new System.Drawing.Point(236, 280); + this.btnAccept.DialogResult = System.Windows.Forms.DialogResult.OK; + this.btnAccept.Location = new System.Drawing.Point(476, 280); this.btnAccept.Name = "btnAccept"; this.btnAccept.TabIndex = 3; this.btnAccept.Text = "&Accept"; // + // lstClasses + // + this.lstClasses.AllowColumnReorder = true; + this.lstClasses.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lstClasses.CheckBoxes = true; + this.lstClasses.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.colClass, + this.colTargetNs}); + this.lstClasses.DoubleClickActivation = false; + this.lstClasses.FullRowSelect = true; + this.lstClasses.Location = new System.Drawing.Point(8, 56); + this.lstClasses.Name = "lstClasses"; + this.lstClasses.Size = new System.Drawing.Size(624, 216); + this.lstClasses.Sorting = System.Windows.Forms.SortOrder.Ascending; + this.lstClasses.TabIndex = 0; + this.lstClasses.View = System.Windows.Forms.View.Details; + this.lstClasses.SubItemClicked += new Mvp.Xml.Design.SubItemEventHandler(this.lstClasses_SubItemClicked); + // + // colClass + // + this.colClass.Text = "Class"; + this.colClass.Width = 344; + // + // colTargetNs + // + this.colTargetNs.Text = "Target Namespace"; + this.colTargetNs.Width = 273; + // + // txtEditor + // + this.txtEditor.Location = new System.Drawing.Point(12, 280); + this.txtEditor.Name = "txtEditor"; + this.txtEditor.TabIndex = 4; + this.txtEditor.Text = ""; + this.txtEditor.Visible = false; + // // ClassPicker // + this.AcceptButton = this.btnAccept; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); - this.ClientSize = new System.Drawing.Size(400, 309); + this.CancelButton = this.btnCancel; + this.ClientSize = new System.Drawing.Size(640, 309); + this.Controls.Add(this.txtEditor); + this.Controls.Add(this.lstClasses); this.Controls.Add(this.btnAccept); this.Controls.Add(this.btnCancel); this.Controls.Add(this.label1); - this.Controls.Add(this.lstClasses); this.Name = "ClassPicker"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = " Class Picker"; @@ -111,5 +154,133 @@ } #endregion + + #endregion Designer Stuff + + string defaultNs; + + public ClassPicker(CodeElements elements, string defaultNs, SelectionCollection selections) + { + InitializeComponent(); + // Add all classes. + this.defaultNs = defaultNs; + IterateElements(elements); + if (lstClasses.Items.Count == 1) + { + lstClasses.Items[0].Checked = true; + } + + if (selections != null) + { + foreach (Selection sel in selections) + { + foreach (ListViewItem item in lstClasses.Items) + { + if (item.Text == sel.ClassName) + { + item.Checked = true; + item.SubItems.Clear(); + item.SubItems.Add(sel.TargetNamespace); + } + } + } + } + } + + private void IterateElements(CodeElements elements) + { + foreach (CodeElement element in elements) + { + if (element is CodeNamespace) + { + IterateElements(((CodeNamespace)element).Members); + } + else if (element is CodeClass) + { + lstClasses.Items.Add(new ListViewItem( + new string[] { ((CodeClass)element).FullName, this.defaultNs })); + } + } + } + + public SelectionCollection Selections + { + get + { + SelectionCollection col = new SelectionCollection(); + foreach (ListViewItem item in lstClasses.Items) + { + if (item.Checked) + { + col.Add(new Selection(item.Text, item.SubItems[1].Text)); + } + } + return col; + } + } + + private void lstClasses_SubItemClicked(object sender, Mvp.Xml.Design.SubItemEventArgs e) + { + if (e.SubItem != 0) + { + lstClasses.StartEditing(txtEditor, e.Item, e.SubItem); + } + } + } + + internal class SelectionCollection : CollectionBase + { + public static SelectionCollection FromString(string serializedData) + { + string[] items = serializedData.Split('|'); + SelectionCollection col = new SelectionCollection(); + foreach (string item in items) + { + col.Add(Selection.FromString(item)); + } + return col; + } + public int Add(Selection selection) + { + return base.InnerList.Add(selection); + } + public void Remove(Selection selection) + { + base.InnerList.Remove(selection); + } + public Selection this[int idx] + { + get { return (Selection)base.InnerList[idx]; } + set { base.InnerList[idx] = value; } + } + public override string ToString() + { + string[] items = new string[base.Count]; + for (int i = 0; i < items.Length; i++) + { + items[i] = base.InnerList[i].ToString(); + } + return String.Join("|", items); + } + } + + internal class Selection + { + public static Selection FromString(string serializedData) + { + string[] values = serializedData.Split(';'); + return new Selection(values[0], values[1]); + } + public Selection(string className, string targetNs) + { + this.ClassName = className; + this.TargetNamespace = targetNs; + } + public override string ToString() + { + return this.ClassName + ";" + this.TargetNamespace; + } + public string ClassName; + public string TargetNamespace; } } Index: ClassPicker.resx =================================================================== RCS file: /cvsroot/mvp-xml/Design/v1/src/CustomTools/XGen/ClassPicker.resx,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ClassPicker.resx 21 Oct 2004 20:42:06 -0000 1.1 +++ ClassPicker.resx 9 Jan 2005 20:20:14 -0000 1.2 @@ -97,15 +97,6 @@ <resheader name="writer"> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> - <data name="lstClasses.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>Private</value> - </data> - <data name="lstClasses.Locked" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>False</value> - </data> - <data name="lstClasses.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> - <value>Private</value> - </data> <data name="label1.Locked" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>False</value> </data> @@ -133,6 +124,36 @@ <data name="btnAccept.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>Private</value> </data> + <data name="lstClasses.Locked" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>False</value> + </data> + <data name="lstClasses.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="lstClasses.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="colClass.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="colClass.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="colTargetNs.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="colTargetNs.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="txtEditor.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> + <data name="txtEditor.Locked" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>False</value> + </data> + <data name="txtEditor.Modifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> + <value>Private</value> + </data> <data name="$this.Locked" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>False</value> </data> @@ -154,12 +175,12 @@ <data name="$this.TrayHeight" type="System.Int32, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>80</value> </data> - <data name="$this.Name"> - <value>ClassPicker</value> - </data> <data name="$this.SnapToGrid" type="System.Boolean, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>True</value> </data> + <data name="$this.Name"> + <value>ClassPicker</value> + </data> <data name="$this.DefaultModifiers" type="System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>Private</value> </data> Index: XGenTool.cs =================================================================== RCS file: /cvsroot/mvp-xml/Design/v1/src/CustomTools/XGen/XGenTool.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- XGenTool.cs 18 Dec 2004 05:11:45 -0000 1.2 +++ XGenTool.cs 9 Jan 2005 20:20:14 -0000 1.3 @@ -27,7 +27,7 @@ /// by the XmlSerializer class. /// </remarks> [Guid("B8F154A6-E234-4c6f-9AD4-675713F4AABD")] - [CustomTool("Mvp.Xml.XGen", "MVP XML XmlSerializer Generation Tool", true)] + [CustomTool("Mvp.Xml.XGen", "Mvp.Xml Project XmlSerializer Generation Tool", true)] [ComVisible(true)] [VersionSupport("7.1")] [CategorySupport(CategorySupportAttribute.CSharpCategory)] @@ -75,77 +75,91 @@ item.DTE.Solution.SolutionBuild.ActiveConfiguration.Name, item.ContainingProject.UniqueName, true); + if (item.FileCodeModel == null || item.FileCodeModel.CodeElements == null) + { + return System.Text.Encoding.ASCII.GetBytes( + SR.XGenTool_NoClassFound); + } + // Copy Design assembly to output for the isolated AppDomain. - string output = item.ContainingProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString(); - output = Path.Combine(item.ContainingProject.Properties.Item("FullPath").Value.ToString(), output); + string outputpath = item.ContainingProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString(); + outputpath = Path.Combine(item.ContainingProject.Properties.Item("FullPath").Value.ToString(), outputpath); string asmfile = ReflectionHelper.GetAssemblyPath(Assembly.GetExecutingAssembly()); - File.Copy(asmfile, Path.Combine(output, Path.GetFileName(asmfile)), true); + try + { + File.Copy(asmfile, Path.Combine(outputpath, Path.GetFileName(asmfile)), true); + } + catch (Exception ex) + { + // May already exist, be locked, etc. + System.Diagnostics.Debug.WriteLine(ex.ToString()); + } - CodeClass targetclass = null; - foreach (CodeElement element in item.FileCodeModel.CodeElements) + string key = "Mvp.Xml.XGen:" + item.get_FileNames(1); + string serialized = String.Empty; + if (item.ContainingProject.Globals.get_VariableExists(key)) { - if (element is CodeNamespace) - { - foreach (CodeElement codec in ((CodeNamespace)element).Members) - { - if (codec is CodeClass) - { - targetclass = (CodeClass) codec; - break; - } - } - } - else if (element is CodeClass) - { - targetclass = (CodeClass) element; - } - if (targetclass != null) - { - break; - } + serialized = (string)item.ContainingProject.Globals[key]; } - if (targetclass == null) + SelectionCollection selections = null; + + if (serialized.Length > 0) { - return System.Text.Encoding.ASCII.GetBytes( - SR.XGenTool_NoClassFound); + selections = SelectionCollection.FromString(serialized); + } + + ClassPicker picker = new ClassPicker(item.FileCodeModel.CodeElements, base.FileNameSpace, selections); + if (picker.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + selections = picker.Selections; } - string codefile = Path.GetTempFileName(); - AppDomainSetup setup = new AppDomainSetup(); - setup.ApplicationName = "Mvp.Xml Design Domain"; - setup.ApplicationBase = output; - setup.ConfigurationFile = ConfigFile; - domain = AppDomain.CreateDomain("Mvp.Xml Design Domain", null, setup); - // Runner ctor will dump the output to the file we pass. - domain.CreateInstance( - Assembly.GetExecutingAssembly().FullName, - typeof(XGenRunner).FullName, false, 0, null, - new object[] { - codefile, - targetclass.FullName + ", " + - item.ContainingProject.Properties.Item("AssemblyName").Value.ToString(), - base.FileNameSpace }, - null, null, null); + if (selections == null || selections.Count == 0) + { + return new byte[0]; + } - string code; - using (StreamReader reader = new StreamReader(codefile)) + StringBuilder output = new StringBuilder(); + + foreach (Selection selection in selections) { - code = reader.ReadToEnd(); + string codefile = Path.GetTempFileName(); + AppDomainSetup setup = new AppDomainSetup(); + setup.ApplicationName = "Mvp.Xml Design Domain"; + setup.ApplicationBase = outputpath; + setup.ConfigurationFile = ConfigFile; + domain = AppDomain.CreateDomain("Mvp.Xml Design Domain", null, setup); + try + { + // Runner ctor will dump the output to the file we pass. + domain.CreateInstance( + Assembly.GetExecutingAssembly().FullName, + typeof(XGenRunner).FullName, false, 0, null, + new object[] { + codefile, + selection.ClassName + ", " + + item.ContainingProject.Properties.Item("AssemblyName").Value.ToString(), + selection.TargetNamespace }, + null, null, null); + + using (StreamReader reader = new StreamReader(codefile)) + { + output.Append(reader.ReadToEnd()).Append(Environment.NewLine); + } + } + finally + { + AppDomain.Unload(domain); + } } - return System.Text.Encoding.ASCII.GetBytes(code); + + return System.Text.Encoding.ASCII.GetBytes(output.ToString()); } catch (Exception e) { return System.Text.Encoding.ASCII.GetBytes(SR.CustomTool_GeneralError(e)); } - finally - { - if (domain != null) - { - AppDomain.Unload(domain); - } - } } #endregion GenerateCode |