This example shows how to implement pattern-matching constraints on property values because XSD patterns are not supported.
Fields such as ClientIdentifier are always a three-character numeric string. A property value of 10 is not valid, whereas 010 is valid. The following configuration schema fragment defines a ClientIdentifier property. The ClientIdentifierConverter class, implemented in your adapter assembly, implements pattern matching. In this case, the custom type converter restricts values to a string of exactly three digits (000 to 999). In the configuration schema, make sure the baf:converter node has the assembly and type full name set correctly. If the user attempts to enter a value that is not valid, an exception message pops up when a validation error occurs in the property page.
You can use the following code in the adapter property page configuration schemas.
|
<xs:element name="ClientIdentifier" type="xs:string">
<xs:annotation>
<xs:appinfo>
<baf:designer>
<baf:displayname>Adapter Client</baf:displayname>
<baf:description>Enter the Adapter Client (3 digit string)</baf:description>
<baf:converter assembly="%BTSROOT%\\Developer Tools\\Microsoft.BizTalk.TestAdapter.dll">Microsoft.BizTalk.TestAdapter.ClientIdentifierConverter</baf:converter>
</baf:designer>
</xs:appinfo>
</xs:annotation>
</xs:element> |
You can place the following code in your adapter assembly.
|
using System;
using System.ComponentModel;
using System.Globalization;
using System.Text.RegularExpressions;
namespace Microsoft.BizTalk.TestAdapter
{
/// <summary>
/// Summary description for ClientIdentifierConverter.
/// </summary>
public class ClientIdentifierConverter : System.ComponentModel.StringConverter
{
private void Validate(string value)
{
Regex regex = new Regex(@"^\d{3}$"); // ^=begin, \d=digit, {3}=exactly 3 occurrences, $=end
Match match = regex.Match((string)value);
if (!match.Success)
{
throw new ApplicationException("Value does not match pattern \"" + regex.ToString() + "\".");
}
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
this.Validate((string)value);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (typeof(string) == destinationType && value is string)
{
this.Validate((string)value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
} |