// // Node.cs // // Author: Kees van Spelde // // Copyright (c) 2013-2018 Magic-Sessions. (www.magic-sessions.com) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // namespace MsgReader.Rtf { /// /// RTF parser node /// internal class Node { #region Fields protected RawDocument InternalOwnerDocument = null; protected NodeGroup InternalParent = null; protected string InternalKeyword = null; #endregion #region Properties /// /// parent node /// public virtual NodeGroup Parent { get { return InternalParent; } set { InternalParent = value; } } /// /// raw document which owner this node /// public virtual RawDocument OwnerDocument { get { return InternalOwnerDocument; } set { InternalOwnerDocument = value; if (Nodes != null) { foreach (Node node in Nodes) { node.OwnerDocument = value; } } } } /// /// Keyword /// public virtual string Keyword { get { return InternalKeyword; } set { InternalKeyword = value; } } /// /// Whether this node has parameters /// public virtual bool HasParameter { get; set; } /// /// Paramter value /// public virtual int Parameter { get; protected set; } /// /// Child nodes /// public virtual NodeList Nodes => null; /// /// Index /// public int Index => InternalParent == null ? 0 : InternalParent.Nodes.IndexOf(this); /// /// node type /// public RtfNodeType Type { get; protected set; } /// /// Previous node in parent nodes list /// public Node PreviousNode { get { if (InternalParent != null) { var index = InternalParent.Nodes.IndexOf(this); if (index > 0) { return InternalParent.Nodes[index - 1]; } } return null; } } /// /// Next node in parent nodes list /// public Node NextNode { get { if (InternalParent != null) { var index = InternalParent.Nodes.IndexOf(this); if (index >= 0 && index < InternalParent.Nodes.Count - 1) return InternalParent.Nodes[index + 1]; } return null; } } #endregion #region Constructors /// /// initialize instance /// public Node() { Type = RtfNodeType.None; } public Node(RtfNodeType type, string key) { Type = type; InternalKeyword = key; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Node(Token token) { InternalKeyword = token.Key; // ReSharper disable DoNotCallOverridableMethodsInConstructor HasParameter = token.HasParam; Parameter = token.Param; // ReSharper restore DoNotCallOverridableMethodsInConstructor switch (token.Type) { case RtfTokenType.Control: Type = RtfNodeType.Control; break; case RtfTokenType.Keyword: Type = RtfNodeType.Keyword; break; case RtfTokenType.ExtKeyword: Type = RtfNodeType.ExtKeyword; break; case RtfTokenType.Text: Type = RtfNodeType.Text; break; default: Type = RtfNodeType.Text; break; } } #endregion #region Write /// /// Write to rtf document /// /// RTF text writer public virtual void Write(Writer writer) { switch (Type) { case RtfNodeType.ExtKeyword: case RtfNodeType.Keyword: case RtfNodeType.Control: if (HasParameter) writer.WriteKeyword(InternalKeyword + Parameter, Type == RtfNodeType.ExtKeyword); else writer.WriteKeyword(InternalKeyword, Type == RtfNodeType.ExtKeyword); break; case RtfNodeType.Text: writer.WriteText(InternalKeyword); break; } } #endregion } }