//*****************************************************************************
// Description.....Library of the activities
//
// Author..........Roman Kiss, rkiss@pathcom.com
// Copyright © 2005 ATZ Consulting Inc. (see included license.rtf file)
//
// Date Created: 04/04/09
//
// Date Modified By Description
//-----------------------------------------------------------------------------
// 04/04/09 Roman Kiss Initial Revision
//
//*****************************************************************************
//
#region Namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Windows.Forms;
using System.Xml.Linq;
//
using RKiss.MessageMediationActivityLibrary.Design.Dialogs.UserControls;
#endregion
namespace RKiss.MessageMediationActivityLibrary.Design.Dialogs.Forms
{
public partial class XPathForm: Form
{
public string XPath { get; set; }
public Dictionary<string, string> Namespaces { get; set; }
public string SourceUri { get; set; }
#region Constructor(s)
public XPathForm(string xpath, Dictionary<string, string> namespaces, string sourceUri) : this(xpath, namespaces, sourceUri, false) { }
public XPathForm(string xpath, Dictionary<string, string> namespaces, string sourceUri, bool bLock)
{
InitializeComponent();
this.XPath = xpath.Trim();
this.Namespaces = namespaces;
this.SourceUri = sourceUri;
this.xpathUserControl1.XPath = xpath;
this.xpathUserControl1.Namespaces = this.Namespaces;
this.messageUserControl1.MessageDirection = MessageDirection.Output;
this.messageUserControl1.InitState(sourceUri, null);
// handlers
this.xpathUserControl1.RaiseEventOnStatus += delegate(object source, StatusEventArgs args)
{
this.statusStrip1.Items[0].Text = args.Status;
this.buttonReload.Enabled = true;
this.buttonOK.Enabled = true;
};
this.messageUserControl1.RaiseEventSelectDone += delegate(object source, SelectEventArgs args)
{
if (args.Topic == "Select")
{
this.xpathUserControl1.XmlDocument = this.CreateMessage((string)args.Tag, args.Name, args.Message);
this.xpathUserControl1.SelectXmlNode();
}
else if (args.Topic == "Clear")
{
this.xpathUserControl1.ClearXmlDocument();
this.xpathUserControl1.Namespaces = null;
}
this.buttonReload.Enabled = true;
this.buttonOK.Enabled = true;
};
this.buttonReload.Enabled = false;
this.buttonOK.Enabled = false;
this.messageUserControl1.LockState(bLock);
}
#endregion
#region Events
protected override void OnClosed(EventArgs e)
{
this.xpathUserControl1.Cleanup();
base.OnClosed(e);
}
#endregion
#region Clicks
private void buttonOK_Click(object sender, EventArgs e)
{
this.XPath = this.xpathUserControl1.XPath;
this.Namespaces = this.xpathUserControl1.Namespaces;
this.SourceUri = this.messageUserControl1.GetState();
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void buttonReload_Click(object sender, EventArgs e)
{
this.xpathUserControl1.ClearXmlDocument();
this.xpathUserControl1.XPath = this.XPath;
this.xpathUserControl1.Namespaces = this.Namespaces;
this.messageUserControl1.InitState(this.SourceUri, null);
this.buttonReload.Enabled = false;
this.buttonOK.Enabled = false;
}
#endregion
private string CreateMessage(string version, string action, string xbody)
{
string outxml = string.Empty;
System.ServiceModel.Channels.Message message = null;
try
{
MessageVersion mv = LibHelper.MessageVersion(version);
if (string.IsNullOrEmpty(xbody))
{
if (mv != MessageVersion.None)
message = System.ServiceModel.Channels.Message.CreateMessage(mv, string.Empty);
}
else
{
var body = XElement.Parse(xbody);
if (this.xpathUserControl1.Namespaces == null || this.xpathUserControl1.Namespaces.Count() == 0)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string ns = body.Name.NamespaceName;
if (string.IsNullOrEmpty(ns) == false)
{
string prefix = body.GetPrefixOfNamespace(ns);
dic.Add(string.IsNullOrEmpty(prefix) ? "_p" : prefix, ns);
this.xpathUserControl1.Namespaces = dic;
}
}
if (mv == MessageVersion.None)
outxml = body.ToString();
else
message = System.ServiceModel.Channels.Message.CreateMessage(mv, action, body.CreateReader());
}
if(message != null)
outxml = LibHelper.ToString(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException == null ? ex.Message : ex.InnerException.Message);
}
finally
{
if(message != null)
message.Close();
}
return outxml;
}
//private void comboBoxMessageVersion_SelectedIndexChanged(object sender, EventArgs e)
//{
// this.xpathUserControl1.ClearXmlDocument();
// MessageVersionItem item = this.comboBoxMessageVersion.SelectedItem as MessageVersionItem;
// using (System.ServiceModel.Channels.Message message = System.ServiceModel.Channels.Message.CreateMessage(item.version, string.Empty))
// using (StringWriter sw = new StringWriter())
// using (XmlTextWriter xtw = new XmlTextWriter(sw))
// {
// message.WriteMessage(xtw);
// xtw.Flush();
// this.xpathUserControl1.XmlDocument = sw.ToString();
// }
//}
//private void comboBoxSchema_DragEnter(object sender, DragEventArgs e)
//{
// if (e.Data.GetDataPresent(DataFormats.FileDrop))
// {
// e.Effect = DragDropEffects.Copy;
// }
//}
//private void comboBoxSchema_DragDrop(object sender, DragEventArgs e)
//{
// //e.Effect = DragDropEffects.None;
// //this.comboBoxSchema.Items.Clear();
// string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop, false);
// sr = new StreamReader(filepath[0]);
//}
}
}