Click here to Skip to main content
15,893,381 members
Articles / Desktop Programming / Win32

Routing Manager for WCF4

Rate me:
Please Sign up or sign in to vote.
5.00/5 (38 votes)
29 Apr 2010CPOL18 min read 108.3K   2.5K   92  
This article describes a design, implementation and usage of the Custom Routing Manager for managing messages via Routing Service built-in .Net 4 Technology.
//*****************************************************************************
//    Description.....Application Model - UserControls
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2005 ATZ Consulting Inc. (see included license.rtf file)  
//       
//    Note: This is a wrapper usercontrol around the Microsoft XmlNotepad 2007               
//    Date Created:    07/07/09
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    07/07/09    Roman Kiss     Initial Revision
//
//*****************************************************************************
//

#region Namespaces
using System;
using System.Globalization;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.Xml.Schema;
using System.Windows.Forms;
using System.ServiceModel.Dispatcher;

#endregion

namespace LocalRepository.UserControls
{
    public partial class XmlNotepadPanelControl : Panel
    {
        XmlNotepadForm _form = null;

        public XmlNotepadPanelControl()
        {
            try
            {
                XmlNotepadForm form = new XmlNotepadForm();
                if (form != null)
                {
                    // form
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.MaximizeBox = false;
                    form.MinimizeBox = false;
                    form.ControlBox = false;
                    form.IsMdiContainer = false;
                    form.TopLevel = false;
                    form.Location = this.Location;
                    form.Dock = DockStyle.Fill;
                    form.BringToFront();
                    form.Show();
                    this.Controls.Add(form);
                    _form = form;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
            }
        }


        public void Close()
        {
            if (_form != null)
            {
                _form.ClearXmlDocument();

                if (_form.IsDisposed == false)
                {
                    _form.Dispose();

                }
                _form.Close();
            }
        }
        public XmlNotepadForm XmlNotepadForm { get { return _form; } }
    }

    public class XmlNotepadForm : XmlNotepad.FormMain
    {
        public EventHandler RaiseEventOnModelChanged = null;

        #region Constructor
        public XmlNotepadForm()
        {
            this.Controls.Remove(this.Controls["menuStrip1"]);
            this.Controls.Remove(this.Controls["comboBoxLocation"]);
            this.Controls.Remove(this.Controls["resizer"]);
            this.Controls.Remove(this.Controls["tabControlLists"]);
            this.Controls.Remove(this.Controls["statusBar1"]);
            this.Controls.Remove(this.Controls["toolStrip1"]);
            this.Controls[0].Controls[0].Controls[1].Text = "Xml";
            TableLayoutPanel tlp = this.Controls[0].Controls[2].Controls[0].Controls[0].Controls[0] as TableLayoutPanel;
            tlp.Controls.Clear();

            this.Controls["tabControlViews"].Dock = DockStyle.Fill;
        }
        #endregion

        #region customize XmlNotepad for input/output xml text
        protected override void InitializeHelp(HelpProvider hp)
        {
            if (hp != null)
                hp.HelpNamespace = string.Empty;
        }
        protected override object GetService(Type service)
        {
            if (service == typeof(XmlNotepad.XmlCache))
            {
                if (this.Model == null)
                {
                    return new XmlNotepad.XmlCache(this, this); ;
                }
                else
                {
                    if (this.Model.Document.OuterXml == "")
                    {
                        this.Model.Document.LoadXml("<root/>");
                    }
                    return this.Model;
                }
            }
            return base.GetService(service);
        }
        public override bool SaveIfDirty(bool prompt)
        {
            // don't show dialog box
            return false;
        }
        public override bool Save()
        {
            // don't show dialog box
            return false;
        }
        protected override void OnModelChanged()
        {
            //Trace.WriteLine("The Document Model has been changed");
            base.OnModelChanged();

            if (this.RaiseEventOnModelChanged != null)
                this.RaiseEventOnModelChanged(this, new EventArgs());
        }
        protected override void OnFileChanged()
        {
            base.OnFileChanged();
        }
        public void LoadXmlDocument(string xml)
        {
            if (string.IsNullOrEmpty(xml) == false)
            {
                this.Model.Clear();
                this.Model.Document.LoadXml(xml);
                this.XmlTreeView.SetSite(this);
            }
        }
        public string GetCurrentXmlDocument
        {
            get { return this.Model.Document.OuterXml; }
        }
        public void ClearXmlDocument()
        {
            if (this.Model != null)
                this.Model.Clear();
            this.XmlTreeView.SetSite(this);
        }
        public void ExpandAll()
        {
            this.XmlTreeView.ExpandAll();
        }
        public void CollapseAll()
        {
            this.XmlTreeView.CollapseAll();
        }
        public int ResizerPosition
        {
            get { return this.XmlTreeView.ResizerPosition; }
            set { this.XmlTreeView.ResizerPosition = value; }
        }
        #endregion

        #region XPath Extension
        public string XPathSelectedNode
        {
            get
            {
                if (base.XmlTreeView.SelectedNode != null)
                {
                    Microsoft.Xml.XPathGenerator xpathGen = new Microsoft.Xml.XPathGenerator();
                    return xpathGen.GetXPath(base.XmlTreeView.SelectedNode.Node, new System.Xml.XmlNamespaceManager(base.XmlTreeView.SelectedNode.Node.OwnerDocument.NameTable));
                }
                return null;
            }
        }

        public bool XPathSelectNode(string xpath, XPathMessageContext context)
        {
            // cleanup selected node
            base.XmlTreeView.SelectedNode = null;

            bool retVal = false;
            if (string.IsNullOrEmpty(xpath) == false)
            {
                // create instance of the finder
                Type type = typeof(XmlNotepad.FindResult).Assembly.GetTypes().FirstOrDefault(e => e.FullName == "XmlNotepad.XmlTreeViewFindTarget");
                object finder = Activator.CreateInstance(type, new object[] { this.XmlTreeView });

                // namespaces from document
                XDocument doc = XDocument.Parse(this.GetCurrentXmlDocument);
                XPathNavigator nav = doc.CreateNavigator();
                nav.MoveToFollowing(XPathNodeType.Element);

                // set namespaces
                XmlNamespaceManager nsmgr = finder.GetType().GetProperty("Namespaces").GetValue(finder, null) as XmlNamespaceManager;
                foreach (var item in nav.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml))
                {
                    nsmgr.AddNamespace(item.Key, item.Value);
                    Trace.WriteLine(string.Format("{0}={1}", item.Key == null ? " " : item.Key, item.Value == null ? " " : item.Value));
                }
                if (context != null)
                {
                    foreach (var item in context.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml))
                    {
                        nsmgr.AddNamespace(item.Key, item.Value);
                        Trace.WriteLine(string.Format("{0}={1}", item.Key == null ? " " : item.Key, item.Value == null ? " " : item.Value));
                    }
                }

                // call FindNext
                object[] args = new object[] { xpath, XmlNotepad.FindFlags.XPath, XmlNotepad.SearchFilter.Everything };
                XmlNotepad.FindResult result = (XmlNotepad.FindResult)finder.GetType().GetMethod("FindNext").Invoke(finder, args);
                if (result != XmlNotepad.FindResult.Found)
                {
                    throw new Exception("Not match");
                }
                if (!base.XmlTreeView.SelectedNode.IsExpanded)
                {
                    base.XmlTreeView.SelectedNode.Expand();
                }
                retVal = true;
            }
            return retVal;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions