// This file is part of YamlDotNet - A .NET library for YAML. // Copyright (c) Antoine Aubry and contributors // // 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 NONINFRINGEMENT. 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.Collections.Generic; using System.IO; using YamlDotNet.Core; using YamlDotNet.Core.Events; namespace YamlDotNet.RepresentationModel { /// /// Represents an YAML stream. /// public class YamlStream : IEnumerable { private readonly IList documents = new List(); /// /// Gets the documents inside the stream. /// /// The documents. public IList Documents { get { return documents; } } /// /// Initializes a new instance of the class. /// public YamlStream() { } /// /// Initializes a new instance of the class. /// public YamlStream(params YamlDocument[] documents) : this((IEnumerable)documents) { } /// /// Initializes a new instance of the class. /// public YamlStream(IEnumerable documents) { foreach (var document in documents) { this.documents.Add(document); } } /// /// Adds the specified document to the collection. /// /// The document. public void Add(YamlDocument document) { documents.Add(document); } /// /// Loads the stream from the specified input. /// /// The input. public void Load(TextReader input) { Load(new Parser(input)); } /// /// Loads the stream from the specified . /// public void Load(IParser parser) { documents.Clear(); parser.Consume(); while (!parser.TryConsume(out var _)) { var document = new YamlDocument(parser); documents.Add(document); } } /// /// Saves the stream to the specified output. /// /// The output. public void Save(TextWriter output) { Save(output, true); } /// /// Saves the stream to the specified output. /// /// The output. /// Indicates whether or not to assign node anchors. public void Save(TextWriter output, bool assignAnchors) { Save(new Emitter(output), assignAnchors); } /// /// Saves the stream to the specified emitter. /// /// The emitter. /// Indicates whether or not to assign node anchors. public void Save(IEmitter emitter, bool assignAnchors) { emitter.Emit(new StreamStart()); foreach (var document in documents) { document.Save(emitter, assignAnchors); } emitter.Emit(new StreamEnd()); } /// /// Accepts the specified visitor by calling the appropriate Visit method on it. /// /// /// A . /// public void Accept(IYamlVisitor visitor) { visitor.Visit(this); } #region IEnumerable Members /// public IEnumerator GetEnumerator() { return documents.GetEnumerator(); } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion } }