CSharp · Console
using System;
using System.Text;
using FluentValidator.Interface;
using FluentValidator.Validator;
// Interfaces used for building the fluent validator
namespace FluentValidator.Interface
{
/// <summary>
/// Prepare document for validation
/// </summary>
public interface IDocument
{
public IValidateSyntax Create();
}
/// <summary>
/// RuUn a syntax check on the document
/// </summary>
public interface IValidateSyntax
{
public IValidateStructure CheckSyntax();
}
/// <summary>
/// Validate the structure of the document using a schema
/// </summary>
public interface IValidateStructure
{
public IValidated CheckStructure();
}
/// <summary>
/// Document is structural and semantic valid
/// </summary>
public interface IValidated
{
public IValidationResult CheckContent();
public IValidationResult TransformContent();
}
/// <summary>
/// Return validation result
/// </summary>
public interface IValidationResult
{
public string GetMessage();
}
}
namespace FluentValidator.Validator
{
/// <summary>
/// Concrete implementation of the fluent validator
/// </summary>
/// <seealso cref="FluentValidator.Interface.IValidateSyntax" />
/// <seealso cref="FluentValidator.Interface.IValidateStructure" />
/// <seealso cref="FluentValidator.Interface.IValidated" />
/// <seealso cref="FluentValidator.Interface.IValidationResult" />
/// <seealso cref="FluentValidator.Interface.IDocument" />
public class ValidateXML : IValidateSyntax, IValidateStructure, IValidated, IValidationResult, IDocument
{
private StringBuilder message = new StringBuilder();
/// <summary>
/// Creates the instance.
/// </summary>
/// <returns></returns>
public IValidateSyntax Create()
{
return new ValidateXML();
}
/// <summary>
/// Checks the syntax.
/// </summary>
/// <returns>IValidateStructure</returns>
public IValidateStructure CheckSyntax()
{
message.Append("Syntax validated\r\n");
return this;
}
/// <summary>
/// Checks the structure.
/// </summary>
/// <returns>IValidated</returns>
public IValidated CheckStructure()
{
message.Append("Structure validated\r\n");
return this;
}
/// <summary>
/// Checks the content.
/// </summary>
/// <returns>IValidationResult</returns>
public IValidationResult CheckContent()
{
message.Append("Content validated\r\n");
return this;
}
/// <summary>
/// Transforms the content.
/// </summary>
/// <returns>IValidationResult</returns>
public IValidationResult TransformContent()
{
message.Append("Stylesheet applied\r\n");
return this;
}
/// <summary>
/// Gets the message.
/// </summary>
/// <returns>string</returns>
public string GetMessage()
{
return message.ToString();
}
}
}
namespace FluentValidator
{
internal class Program
{
static void Main(string[] args)
{
ValidateXML validate = new ValidateXML();
Console.WriteLine("Validate XML file:");
Console.WriteLine("------------------");
Console.WriteLine(
validate.Create()
.CheckSyntax()
.CheckStructure()
.CheckContent()
.GetMessage()
);
Console.WriteLine("");
Console.WriteLine("Validate and transform XML file:");
Console.WriteLine("--------------------------------");
Console.WriteLine(
validate.Create()
.CheckSyntax()
.CheckStructure()
.TransformContent()
.GetMessage()
);
Console.ReadKey();
}
}
}