Beispiel #1
0
        ///<summary>
        /// Returns all the objects defined in the file, serialized to type T.
        ///</summary>

        /*public IEnumerable<T> GetObjects<T>() where T : new()
         * {
         *  if( _docNode == null )
         *      throw new Exception("TydFile has no document node: " + FileName);
         *
         *  foreach (var n in TydHelper.TydToObject.GetObjects<T>(_docNode,_filePath))
         *  {
         *      yield return n;
         *  }
         * }*/

        /// <summary>
        /// Returns the single object defined by the file, deserialized as type T.
        /// </summary>

        /*public T GetObject<T>( bool resolveCrossRefs = false ) where T : new()
         * {
         *  if( _docNode == null )
         *      throw new Exception("TydFile has no document node: " + FileName);
         *
         *  if( _docNode.Count == 0 )
         *      throw new Exception("TydFile contains a document node but no data: " + FileName );
         *
         *  return TydHelper.TydToObject.GetObject<T>(_docNode, resolveCrossRefs, _filePath );
         * }*/

        ///<summary>
        /// Write to a file, overwriting any file present.
        /// If a path is provided, the file's path is changed to that new path before saving. Otherwise, the current path is used.
        ///</summary>
        public void Save(string path = null)
        {
            if (path != null)
            {
                _filePath = path;
            }
            else if (_filePath == null)
            {
                throw new InvalidOperationException("Saved TydFile which had null path");
            }

            //Build the text we're going to write
            var tydText = new StringBuilder();

            foreach (var node in _docNode)
            {
                tydText.AppendLine(TydToText.Write(node, true));
            }

            //Write to the file
            File.WriteAllText(_filePath, tydText.ToString().TrimEnd());
        }
Beispiel #2
0
        ///<summary>
        /// Copies all child nodes from source into heir, recursively.
        /// -If a node appears only in source or only in heir, it is included.
        /// -If a list appears in both source and heir, source's entries are appended to heir's entries.
        /// -If a non-list node appears in both source and heir, heir's node is overwritten.
        ///</summary>
        private static void ApplyInheritance(TydNode source, TydNode heir)
        {
            try
            {
                //They're either strings or nulls: We just keep the existing heir's value
                if (source is TydString)
                {
                    return;
                }

                //Heir has noinherit attribute: Skip this inheritance
                {
                    TydCollection heirCol = heir as TydCollection;
                    if (heirCol != null && heirCol.AttributeNoInherit)
                    {
                        return;
                    }
                }

                //They're tables: Combine all children of source and heir. Unique-name source nodes are prepended
                {
                    TydTable sourceObj = source as TydTable;
                    if (sourceObj != null)
                    {
                        TydTable heirTable = (TydTable)heir;
                        for (int i = 0; i < sourceObj.Count; i++)
                        {
                            var sourceChild       = sourceObj[i];
                            var heirMatchingChild = heirTable[sourceChild.Name];

                            if (heirMatchingChild != null)
                            {
                                ApplyInheritance(sourceChild, heirMatchingChild);
                            }
                            else
                            {
                                heirTable.InsertChild(sourceChild, 0); //Does this need to be DeepClone?
                            }
                        }
                        return;
                    }
                }

                //They're lists: Prepend source's children before heir's children
                {
                    TydList sourceList = source as TydList;
                    if (sourceList != null)
                    {
                        TydList heirList = (TydList)heir;
                        for (int i = 0; i < sourceList.Count; i++)
                        {
                            //Insert at i so the nodes stay in the same order from source to heir
                            heirList.InsertChild(sourceList[i], i); //Does this need to be DeepClone?
                        }
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("ApplyInheritance exception: " + e + ".\nsource: (" + source + ")\n" + TydToText.Write(source) + "\ntarget: (" + heir + ")\n" + TydToText.Write(heir));
            }
        }
Beispiel #3
0
        ///<summary>
        /// Copies all child nodes from source into heir, recursively.
        /// -If a node appears only in source or only in heir, it is included.
        /// -If a list appears in both source and heir, source's entries are appended to heir's entries.
        /// -If a non-list node appears in both source and heir, heir's node is overwritten.
        ///</summary>
        private static void ApplyInheritance(TydNode source, TydNode heir)
        {
            try
            {
                //They're strings: Copy source over heir
                {
                    TydString sourceStr = source as TydString;
                    if (sourceStr != null)
                    {
                        TydString heirStr = heir as TydString;
                        heirStr.Value = sourceStr.Value;
                        return;
                    }
                }

                //They're tables: Combine all children of source and heir, with source having priority in case of duplicates
                {
                    TydTable sourceObj = source as TydTable;
                    if (sourceObj != null)
                    {
                        TydTable heirObj = (TydTable)heir;
                        for (int i = 0; i < sourceObj.Count; i++)
                        {
                            var sourceChild       = sourceObj[i];
                            var heirMatchingChild = heirObj[sourceChild.Name];
                            if (heirMatchingChild != null)
                            {
                                ApplyInheritance(sourceChild, heirMatchingChild);
                            }
                            else
                            {
                                heirObj.AddChild(sourceChild); //Does this need to be DeepClone?
                            }
                        }
                        return;
                    }
                }

                //They're lists: Append source's entries to heir's entries
                {
                    TydList sourceList = source as TydList;
                    if (sourceList != null)
                    {
                        TydList heirList = (TydList)heir;
                        for (int i = 0; i < sourceList.Count; i++)
                        {
                            heirList.AddChild(sourceList[i]); //Does this need to be DeepClone?
                        }
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("ApplyInheritance exception: " + e + ".\nsource: (" + source + ")\n" + TydToText.Write(source) + "\ntarget: (" + heir + ")\n" + TydToText.Write(heir));
            }
        }