// // NodeList.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. // using System; using System.Collections; using System.Text; namespace MsgReader.Rtf { /// /// RTF node list /// internal class NodeList : CollectionBase { #region Properties /// /// Get node by index /// /// /// public Node this[int index] => (Node) List[index]; /// /// Get node by keyword /// /// /// public Node this[string keyWord] { get { foreach (Node node in this) if (node.Keyword == keyWord) return node; return null; } } /// /// Get node by type /// /// /// public Node this[Type type] { get { foreach (Node node in this) { if (type == node.GetType()) return node; } return null; } } /// /// Get the node text /// public string Text { get { var stringBuilder = new StringBuilder(); foreach (Node node in this) { if (node.Type == RtfNodeType.Text) stringBuilder.Append(node.Keyword); else if (node is NodeGroup) { var txt = node.Nodes.Text; if (txt != null) stringBuilder.Append(txt); } } return stringBuilder.ToString(); } } /// /// Get node's parameter value special keyword, if it is not found , return default value. /// /// Keyword /// Default value /// Parameter value public int GetParameter(string key, int defaultValue) { foreach (Node node in this) if (node.Keyword == key && node.HasParameter) return node.Parameter; return defaultValue; } /// /// Detect whether the node exist in this list /// /// keyword /// True or false public bool ContainsKey(string key) { return this[key] != null; } /// /// Get index of node in this list /// /// node /// index , if node does no in this list , return -1 public int IndexOf(Node node) { return List.IndexOf(node); } /// /// Add node list range /// /// A node list internal void AddRange(NodeList nodeList) { InnerList.AddRange(nodeList); } /// /// Add node /// /// Node internal void Add(Node node) { //node.OwnerList = this ; List.Add(node); } /// /// Remove node /// /// Node internal void Remove(Node node) { List.Remove(node); } /// /// Insert node /// /// Index /// Node internal void Insert(int index, Node node) { List.Insert(index, node); } #endregion } }