|
//*****************************************************************************
// Description.....WF4 Message Mediation Library
//
// Author..........Roman Kiss, rkiss@pathcom.com
// Copyright © 2009 ATZ Consulting Inc. (see included license.rtf file)
//
// Date Created: 07/07/09
//
// Date Modified By Description
//-----------------------------------------------------------------------------
// 07/07/09 Roman Kiss Initial Revision
// 10/10/12 Roman Kiss Upadate 4.5
//*****************************************************************************
//
#region Namespaces
using System;
using System.Activities;
using System.Activities.Presentation.PropertyEditing;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Markup;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
#endregion
namespace RKiss.MessageMediationActivityLibrary
{
[ContentProperty("Parameters")]
public sealed class Transform : CodeActivity
{
private IDictionary<string, Argument> parameters;
private IDictionary<string, InArgument> functions;
#region Properties
[Category("Output")]
[RequiredArgument]
public OutArgument<XElement> Output { get; set; }
[Category("Input")]
[RequiredArgument]
public InArgument<XElement> Input { get; set; }
[Category("Input")]
[DefaultValue(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool EnableScript { get; set; }
[Category("Input")]
[DefaultValue(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool EnableDebug { get; set; }
[Category("Input")]
[RequiredArgument]
[EditorBrowsable]
[EditorReuse(false)]
//[Editor(typeof(XmlValueExpressionEditor), typeof(PropertyValueEditor))]
public InArgument<XElement> Mediator { get; set; }
[Category("Input")]
[Browsable(true)]
[Editor(typeof(RKiss.MessageMediationActivityLibrary.Editors.DynamicArgumentDictionaryEditor), typeof(PropertyValueEditor))]
public IDictionary<string, Argument> Parameters
{
get
{
if (this.parameters == null)
{
this.parameters = new Dictionary<string, Argument>();
}
return this.parameters;
}
}
[Category("Input")]
[Browsable(true)]
[Editor(typeof(RKiss.MessageMediationActivityLibrary.Editors.DynamicArgumentDictionaryEditor), typeof(PropertyValueEditor))]
public IDictionary<string, InArgument> Functions
{
get
{
if (this.functions == null)
{
this.functions = new Dictionary<string, InArgument>();
}
return this.functions;
}
}
#endregion
protected override void Execute(CodeActivityContext context)
{
XslCompiledTransform xsltprocessor = new XslCompiledTransform(this.EnableDebug);
XsltArgumentList xsltArgList = new XsltArgumentList();
#region Step 1: Add functions to the XsltArgumentList
if (this.Functions != null && this.Functions.Count > 0)
{
foreach (var param in this.Functions)
{
// tbd
}
}
#endregion
#region Step 2: Add parameters to the XsltArgumentList
if (this.Parameters != null && this.Parameters.Count > 0)
{
foreach (var param in this.Parameters)
{
if (string.IsNullOrEmpty(param.Key) || param.Value == null || param.Value.Expression == null) continue;
XName xname = XName.Get(param.Key);
xsltArgList.AddParam(xname.LocalName, xname.NamespaceName, param.Value.Get(context));
}
}
#endregion
#region Step 3: Load metadata
try
{
using (MemoryStream msMetadata = new MemoryStream(UTF8Encoding.UTF8.GetBytes(this.Mediator.Get(context).ToString())))
{
XmlDictionaryReader rdMetadata = XmlDictionaryReader.CreateTextReader(msMetadata, XmlDictionaryReaderQuotas.Max);
xsltprocessor.Load(rdMetadata, new XsltSettings { EnableScript = this.EnableScript }, new XmlUrlResolver());
}
}
catch (Exception ex)
{
throw new Exception(string.Format("XslCompiledTransform loading in the activity '{0}' failed", this.DisplayName), ex);
}
#endregion
#region Step 4: Transform Input to the Output
try
{
using (MemoryStream msInput = new MemoryStream())
{
using (XmlDictionaryWriter wrInput = XmlDictionaryWriter.CreateTextWriter(msInput, Encoding.UTF8, false))
{
wrInput.WriteStartDocument();
this.Input.Get(context).WriteTo(wrInput);
wrInput.WriteEndDocument();
}
msInput.Position = 0;
using (MemoryStream msOutput = new MemoryStream())
using (XmlDictionaryWriter wrOutput = XmlDictionaryWriter.CreateTextWriter(msOutput, Encoding.UTF8, true))
using (XmlDictionaryReader rdInput = XmlDictionaryReader.CreateTextReader(msInput, XmlDictionaryReaderQuotas.Max))
{
xsltprocessor.Transform(rdInput, xsltArgList, wrOutput);
msOutput.Position = 0;
this.Output.Set(context, XElement.Load(msOutput));
}
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Xslt transformation in the activity '{0}' failed.", this.DisplayName), ex);
}
#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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.