Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

RTF Document Constructor Library

, 16 Aug 2010 CPOL
Create Rich Text Format documents programatically.
using System;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using System.IO;

namespace ESCommon.Rtf
{
    internal class RtfAttributeInfo
    {
        private MemberInfo _memberInfo;

        private RtfControlWordAttribute controlWordAttribute;
        private RtfControlGroupAttribute controlGroupAttribute;

        private RtfControlWordDenotingEndAttribute controlWordDenotingEndAttribute;
        private RtfEnclosingBracesAttribute enclosingBracesAttribute;

        private RtfIgnoreAttribute ignoreAttribute;
        private RtfIncludeAttribute includeAttribute;

        private RtfIndexAttribute indexAttribute;
        private RtfEnumAsControlWordAttribute enumAsControlWordAttribute;

        private RtfTextDataAttribute textDataAttribute;
        
        private bool hasAttributes = false;

        internal MemberInfo MemberInfo
        {
            get { return _memberInfo; }
        }

        internal RtfControlWordAttribute ControlWordAttribute
        {
            get { return controlWordAttribute; }
        }
        internal RtfControlGroupAttribute ControlGroupAttribute
        {
            get { return controlGroupAttribute; }
        }

        internal RtfControlWordDenotingEndAttribute ControlWordDenotingEndAttribute
        {
            get { return controlWordDenotingEndAttribute; }
        }
        internal RtfEnclosingBracesAttribute EnclosingBracesAttribute
        {
            get { return enclosingBracesAttribute; }
        }

        internal RtfIgnoreAttribute IgnoreAttribute
        {
            get { return ignoreAttribute; }
        }
        internal RtfIncludeAttribute IncludeAttribute
        {
            get { return includeAttribute; }
        }

        internal RtfIndexAttribute IndexAttribute
        {
            get { return indexAttribute; }
        }

        internal RtfEnumAsControlWordAttribute EnumAsControlWordAttribute
        {
            get { return enumAsControlWordAttribute; }
        }

        internal RtfTextDataAttribute TextDataAttribute
        {
            get { return textDataAttribute; }
        }

        internal bool HasAttributes
        {
            get { return hasAttributes; }
        }

        internal RtfAttributeInfo(MemberInfo memberInfo)
        {
            _memberInfo = memberInfo;

            object[] attributes = MemberInfo.GetCustomAttributes(false);

            hasAttributes = attributes.Length > 0;

            if (!hasAttributes)
                return;

            controlWordAttribute = GetAttribute(attributes, typeof(RtfControlWordAttribute)) as RtfControlWordAttribute;
            controlGroupAttribute = GetAttribute(attributes, typeof(RtfControlGroupAttribute)) as RtfControlGroupAttribute;

            controlWordDenotingEndAttribute = GetAttribute(attributes, typeof(RtfControlWordDenotingEndAttribute)) as RtfControlWordDenotingEndAttribute;
            enclosingBracesAttribute = GetAttribute(attributes, typeof(RtfEnclosingBracesAttribute)) as RtfEnclosingBracesAttribute;

            ignoreAttribute = GetAttribute(attributes, typeof(RtfIgnoreAttribute)) as RtfIgnoreAttribute;
            includeAttribute = GetAttribute(attributes, typeof(RtfIncludeAttribute)) as RtfIncludeAttribute;

            indexAttribute = GetAttribute(attributes, typeof(RtfIndexAttribute)) as RtfIndexAttribute;

            enumAsControlWordAttribute = GetAttribute(attributes, typeof(RtfEnumAsControlWordAttribute)) as RtfEnumAsControlWordAttribute;

            textDataAttribute = GetAttribute(attributes, typeof(RtfTextDataAttribute)) as RtfTextDataAttribute;

            object[] neededAttributes = new object[] {
                    controlWordAttribute,
                    controlGroupAttribute,
                    controlWordDenotingEndAttribute,
                    enclosingBracesAttribute,
                    ignoreAttribute,
                    includeAttribute,
                    indexAttribute,
                    enumAsControlWordAttribute,
                    textDataAttribute,
                };

            hasAttributes = false;

            for (int i = 0; !hasAttributes && i < neededAttributes.Length; i++)
                hasAttributes |= neededAttributes != null;
        }

        private object GetAttribute(object[] attributes, Type type)
        {
            foreach (object attribute in attributes)
                if (attribute.GetType() == type)
                    return attribute;

            return null;
        }
    }
}

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)

Share

About the Author

Dima Popov
Software Developer Energoservice
Russian Federation Russian Federation
Dmitry lives in Arkhangelsk, Russia. He has developed C# applications since 2007.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 16 Aug 2010
Article Copyright 2010 by Dima Popov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid