Click here to Skip to main content
15,880,651 members
Articles / Desktop Programming / WPF

Writing Your Own RTF Converter

Rate me:
Please Sign up or sign in to vote.
4.95/5 (234 votes)
1 Aug 2013CPOL14 min read 2.4M   40.4K   632  
An article on how to write a custom RTF parser and converter.
// -- FILE ------------------------------------------------------------------
// name       : ArgumentInfo.cs
// project    : System Framelet
// created    : Jani Giannoudis - 2008.06.03
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
namespace Itenso.Sys.Application
{

	// ------------------------------------------------------------------------
	public abstract class Argument : IArgument
	{

		// ----------------------------------------------------------------------
		protected Argument( ArgumentType argumentType, string name, object defaultValue )
		{
			this.name = name;
			this.argumentType = argumentType;
			this.defaultValue = defaultValue;
		} // Argument

		// ----------------------------------------------------------------------
		public string Name
		{
			get { return name; }
		} // Name

		// ----------------------------------------------------------------------
		public object Value
		{
			get 
			{
				if ( value == null )
				{
					return defaultValue;
				}
				return value; 
			}
			set { this.value = value; }
		} // Value

		// ----------------------------------------------------------------------
		public object DefaultValue
		{
			get { return defaultValue; }
		} // DefaultValue

		// ----------------------------------------------------------------------
		public ArgumentType ArgumentType
		{
			get { return argumentType; }
		} // ArgumentType

		// ----------------------------------------------------------------------
		public bool IsMandatory
		{
			get { return ( argumentType & ArgumentType.Mandatory ) == ArgumentType.Mandatory; }
		} // IsMandatory

		// ----------------------------------------------------------------------
		public bool HasName
		{
			get { return ( argumentType & ArgumentType.HasName ) == ArgumentType.HasName; }
		} // HasName

		// ----------------------------------------------------------------------
		public bool ContainsValue
		{
			get { return ( argumentType & ArgumentType.ContainsValue ) == ArgumentType.ContainsValue; }
		} // ContainsValue

		// ----------------------------------------------------------------------
		public virtual bool IsValid
		{
			get 
			{
				if ( IsMandatory && !isLoaded )
				{
					return false;
				}

				if ( IsMandatory && ContainsValue && ( Value == null && DefaultValue == null ) )
				{
					return false;
				}

				return true; 
			}
		} // IsValid

		// ----------------------------------------------------------------------
		public bool IsLoaded
		{
			get { return isLoaded; }
		} // IsLoaded

		// ----------------------------------------------------------------------
		public void Load( string commandLineArg )
		{
			bool isNamedArg = commandLineArg.StartsWith( "/" ) || commandLineArg.StartsWith( "-" );

			// argument with name
			if ( HasName )
			{
				if ( !isNamedArg )
				{
					return; // missing argument name
				}

				commandLineArg = commandLineArg.Substring( 1 );
				if ( string.IsNullOrEmpty( commandLineArg ) )
				{
					return;
				}
			}
			else if ( isNamedArg )
			{
				return; // name provided on argument without name
			}

			isLoaded = OnLoad( commandLineArg );
		} // Load

		// ----------------------------------------------------------------------
		protected abstract bool OnLoad( string commandLineArg );

		// ----------------------------------------------------------------------
		// members
		private readonly string name;
		private readonly ArgumentType argumentType;
		private readonly object defaultValue;
		private bool isLoaded;
		private object value;

	} // class ArgumentInfo

} // namespace Itenso.Sys.Application
// -- EOF -------------------------------------------------------------------

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions