A converter is defined by declaring a class which implements Ant.ContentEntry.Converter
- (page 2 of 7)
The first step is to define the basic converter, with minimal validation which converts to and from an integer. This is done with the code below.
using System;
using Ant.ContentEntry;
namespace HowTos
{
/// <summary>
/// Converter which checks that the value is an integer and is divisible by 3, it also checks that the Int is between 0 and 600
/// </summary>
public class IntegerDivisibleConverter : Converter
{
ContentDefinitionField def;
/// <summary>
/// Init method is called by the content entry framework immediately after construction
/// </summary>
/// <param name="def">Field which is represented by this converter</param>
public void Init(ContentDefinitionField def)
{
this.def = def;
}
/// <summary>
/// Gets the regular expression that represents the validation of this converter. In this case it is not relevant
/// </summary>
public string GetRegEx()
{
return null;
}
/// <summary>
/// Returns the .net type which this converter converts to
/// </summary>
/// <returns>Int32 in this case</returns>
public Type GetSupportedType()
{
return typeof(Int32);
}
/// <summary>
/// Allows for the XmlSchema that represents the entire content to be modified to represent specific validation.
/// </summary>
/// <param name="user">Logged on user</param>
/// <param name="type">Simple type to modify</param>
public void ModifyXmlSchemaType(Ant.Security.AuthenticatedUser user, System.Xml.Schema.XmlSchemaSimpleType type)
{
// TODO: Add IntegerDivisibleConverter.ModifyXmlSchemaType implementation
}
/// <summary>
/// Gets the integer which represents the string
/// </summary>
/// <param name="str">string which represents an integer</param>
/// <param name="context">Context in which we are editing</param>
/// <returns>Integer</returns>
public object GetObject(string str, System.Collections.IDictionary context)
{
try
{
if (str == null || str.Length == 0)
{
if (this.def.IsRequired)
{
throw new ConverterValidationException("Field "+def.FieldName+" is not specified but is marked as required");
}
else
return null;
}
// TODO: validation
Int32 val = Int32.Parse(str);
return val;
}
catch (Exception e)
{
throw new ConverterValidationException("Can't convert value "+str+" using converter IntegerDivisibleConverter", e);
}
}
/// <summary>
/// Gets the string which represents this value
/// </summary>
/// <param name="obj">Object which is the value</param>
/// <param name="context">Context in which we are editing</param>
/// <returns>string which represents the integer</returns>
public string GetString(object obj, System.Collections.IDictionary context)
{
if (obj is Int32)
{
Int32 i = (Int32)obj;
// TODO: validation
return i.ToString();
}
else if (obj == null)
{
if (this.def.IsRequired)
{
throw new ConverterValidationException("Field "+def.FieldName+" is not specified but is marked as required");
}
else
return null;
}
else
throw new ConverterValidationException("IntegerDivisibleConverter converter can not convert "+obj.GetType().ToString());
}
}
}