Click here to Skip to main content
15,891,136 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.5M   40.4K   632  
An article on how to write a custom RTF parser and converter.
// -- FILE ------------------------------------------------------------------
// name       : RtfDocumentProperty.cs
// project    : RTF Framelet
// created    : Leon Poyyayil - 2008.05.23
// language   : c#
// environment: .NET 2.0
// copyright  : (c) 2004-2010 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Text;
using Itenso.Sys;

namespace Itenso.Rtf.Model
{

	// ------------------------------------------------------------------------
	public sealed class RtfDocumentProperty : IRtfDocumentProperty
	{

		// ----------------------------------------------------------------------
		public RtfDocumentProperty( int propertyKindCode, string name, string staticValue ) :
			this( propertyKindCode, name, staticValue, null )
		{
		} // RtfDocumentProperty

		// ----------------------------------------------------------------------
		public RtfDocumentProperty( int propertyKindCode, string name, string staticValue, string linkValue )
		{
			if ( name == null )
			{
				throw new ArgumentNullException( "name" );
			}
			if ( staticValue == null )
			{
				throw new ArgumentNullException( "staticValue" );
			}
			this.propertyKindCode = propertyKindCode;
			switch ( propertyKindCode )
			{
				case RtfSpec.PropertyTypeInteger:
					propertyKind = RtfPropertyKind.IntegerNumber;
					break;
				case RtfSpec.PropertyTypeRealNumber:
					propertyKind = RtfPropertyKind.RealNumber;
					break;
				case RtfSpec.PropertyTypeDate:
					propertyKind = RtfPropertyKind.Date;
					break;
				case RtfSpec.PropertyTypeBoolean:
					propertyKind = RtfPropertyKind.Boolean;
					break;
				case RtfSpec.PropertyTypeText:
					propertyKind = RtfPropertyKind.Text;
					break;
				default:
					propertyKind = RtfPropertyKind.Unknown;
					break;
			}
			this.name = name;
			this.staticValue = staticValue;
			this.linkValue = linkValue;
		} // RtfDocumentProperty

		// ----------------------------------------------------------------------
		public int PropertyKindCode
		{
			get { return propertyKindCode; }
		} // PropertyKindCode

		// ----------------------------------------------------------------------
		public RtfPropertyKind PropertyKind
		{
			get { return propertyKind; }
		} // PropertyKind

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

		// ----------------------------------------------------------------------
		public string StaticValue
		{
			get { return staticValue; }
		} // StaticValue

		// ----------------------------------------------------------------------
		public string LinkValue
		{
			get { return linkValue; }
		} // LinkValue

		// ----------------------------------------------------------------------
		public override bool Equals( object obj )
		{
			if ( obj == this )
			{
				return true;
			}
			
			if ( obj == null || GetType() != obj.GetType() )
			{
				return false;
			}

			return IsEqual( obj );
		} // Equals

		// ----------------------------------------------------------------------
		private bool IsEqual( object obj )
		{
			RtfDocumentProperty compare = obj as RtfDocumentProperty; // guaranteed to be non-null
			return
				compare != null &&
				propertyKindCode == compare.propertyKindCode &&
				propertyKind == compare.propertyKind &&
				name.Equals( compare.name ) &&
				CompareTool.AreEqual( staticValue, compare.staticValue ) &&
				CompareTool.AreEqual( linkValue, compare.linkValue );
		} // IsEqual

		// ----------------------------------------------------------------------
		public override int GetHashCode()
		{
			return HashTool.AddHashCode( GetType().GetHashCode(), ComputeHashCode() );
		} // GetHashCode

		// ----------------------------------------------------------------------
		private int ComputeHashCode()
		{
			int hash = propertyKindCode;
			hash = HashTool.AddHashCode( hash, propertyKind );
			hash = HashTool.AddHashCode( hash, name );
			hash = HashTool.AddHashCode( hash, staticValue );
			hash = HashTool.AddHashCode( hash, linkValue );
			return hash;
		} // ComputeHashCode

		// ----------------------------------------------------------------------
		public override string ToString()
		{
			StringBuilder buf = new StringBuilder( name );
			if ( staticValue != null )
			{
				buf.Append( "=" );
				buf.Append( staticValue );
			}
			if ( linkValue != null )
			{
				buf.Append( "@" );
				buf.Append( linkValue );
			}
			return buf.ToString();
		} // ToString

		// ----------------------------------------------------------------------
		// members
		private readonly int propertyKindCode;
		private readonly RtfPropertyKind propertyKind;
		private readonly string name;
		private readonly string staticValue;
		private readonly string linkValue;

	} // class RtfDocumentProperty

} // namespace Itenso.Rtf.Model
// -- 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