// initializes a playlist item using a corresponding STON value private static void InitializeFromSton(IPlaylistItem item, IStonDocument document, IStonComplexEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { item.Name = FromSton <string>(document, GetValue(document, document.GetMember(entity, new StonBindingName("Name"))), builtObjects) ?? item.GetType().Name; // playlist never declares its path explicitly // in general, it should be based on the playlist location instead if (!(item is Playlist)) { item.Path = FromSton <string>(document, GetValue(document, document.GetMember(entity, new StonBindingName("Path"))), builtObjects) ?? ""; } // loading subitems of a container if (item is IPlaylistContainer) { var container = item as IPlaylistContainer; foreach (var subitemData in entity.CollectionInit.Elements) { var subitem = FromSton <IPlaylistItem>(document, GetValue(document, subitemData), builtObjects); container.Add(subitem); } } // loading the loop provider of a playlist if (item is Track) { (item as Track).StreamProvider = FromSton <IStreamProvider>(document, GetValue(document, document.GetMember(entity, new StonBindingName("Stream"))), builtObjects); } }
// checks whether a given globally identified entities from a given document are equal or not private bool AreEqual <TEntity>(IStonDocument document, IStonEntityEquivalenceComparer comparer, string id1, string id2) where TEntity : class, IStonEntity { var e1 = id1 != null?document.GetGlobalEntity(id1) : null; var e2 = id2 != null?document.GetGlobalEntity(id2) : null; if (typeof(TEntity) == typeof(IStonValuedEntity)) { return(comparer.Equals(e1 as IStonValuedEntity, e2 as IStonValuedEntity)); } else if (typeof(TEntity) == typeof(IStonSimpleEntity)) { return(comparer.Equals(e1 as IStonSimpleEntity, e2 as IStonSimpleEntity)); } else if (typeof(TEntity) == typeof(IStonComplexEntity)) { return(comparer.Equals(e1 as IStonComplexEntity, e2 as IStonComplexEntity)); } else if (typeof(TEntity) == typeof(IStonReferenceEntity)) { return(comparer.Equals(e1 as IStonReferenceEntity, e2 as IStonReferenceEntity)); } else { return(comparer.Equals(e1, e2)); } }
/// <summary> /// Creates a new STON semantic entity equivalence comparer for a given STON document. /// </summary> /// <param name="document">The STON document whose entities are compared.</param> public StonSemanticEntityEquivalenceComparer(IStonDocument document) { if (document == null) { throw new ArgumentNullException("document"); } Document = document; TypeComparer = StonTypeEquivalenceComparer.Instance; }
/// <summary> /// Returns a string representation of a STON document, using specific STON writer. /// </summary> /// <param name="document">The document to represent as a string.</param> /// <param name="writer">The writer used to represent the entity.</param> /// <returns>The string representation of the entity.</returns> public static string ToString(this IStonDocument document, IStonWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } var stringWriter = new StringWriter(); writer.WriteDocument(stringWriter, document); return(stringWriter.ToString()); }
/// <summary> /// Writes a STON document to a given text writer. /// </summary> /// <param name="writer">The writer to write the document to.</param> /// <param name="document">The document to write.</param> public void WriteDocument(TextWriter writer, IStonDocument document) { if (writer == null) { throw new ArgumentNullException("writer"); } if (document == null) { throw new ArgumentNullException("document"); } WriteEntity(writer, document.Core); }
// initializes a stream provider using a corresponding STON value private static void InitializeFromSton(IStreamProvider provider, IStonDocument document, IStonComplexEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { if (provider is LoopStreamProvider) { var loop = provider as LoopStreamProvider; loop.TrackStart = FromSton <long?>(document, GetValue(document, document.GetMember(entity, new StonBindingName("TrackStart"))), builtObjects) ?? -1; loop.StreamLoopStart = FromSton <long?>(document, GetValue(document, document.GetMember(entity, new StonBindingName("LoopStart"))), builtObjects) ?? -1; loop.StreamLoopEnd = FromSton <long?>(document, GetValue(document, document.GetMember(entity, new StonBindingName("LoopEnd"))), builtObjects) ?? -1; loop.TrackEnd = FromSton <long?>(document, GetValue(document, document.GetMember(entity, new StonBindingName("TrackEnd"))), builtObjects) ?? -1; loop.Loops = FromSton <int?>(document, GetValue(document, document.GetMember(entity, new StonBindingName("Loops"))), builtObjects) ?? -1; } }
// initializes an object using a corresponding STON value private static void InitializeFromSton(object obj, IStonDocument document, IStonComplexEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { switch (obj) { case IPlaylistItem item: InitializeFromSton(item, document, entity, builtObjects); break; case IStreamProvider provider: InitializeFromSton(provider, document, entity, builtObjects); break; } }
/// <summary> /// Writes a string representation of a STON document to a stream, using specific STON writer, leaving the stream open. /// </summary> /// <param name="document">The document to write.</param> /// <param name="stream">The stream to write to.</param> /// <param name="writer">The writer used to write the entity.</param> public static void Save(this IStonDocument document, Stream stream, IStonWriter writer) { if (stream == null) { throw new ArgumentNullException("stream"); } if (writer == null) { throw new ArgumentNullException("writer"); } using (var streamWriter = new StreamWriter(stream, new UTF8Encoding(false, true), 1024, true)) { writer.WriteDocument(streamWriter, document); } }
public static void Save(this IStonDocument document, string path, IStonWriter writer) { if (path == null) { throw new ArgumentNullException("path"); } if (writer == null) { throw new ArgumentNullException("writer"); } using (var streamWriter = new StreamWriter(path)) { writer.WriteDocument(streamWriter, document); } }
// general function to retrieve a valued entity from a STON document // it really should appear in the original STON library... private static IStonValuedEntity GetValue(IStonDocument document, IStonEntity entity) { if (entity == null) { return(null); } else if (entity is IStonValuedEntity) { return(entity as IStonValuedEntity); } else { return(document.GetReferencedValue(entity as IStonReferenceEntity)); } }
// reads a usable object from a STON value private static TObject FromSton <TObject>(IStonDocument document, IStonValuedEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { if (entity == null) { return(default(TObject)); } if (!builtObjects.ContainsKey(entity)) { var obj = CreateFromSton <TObject>(document, entity, builtObjects); builtObjects.Add(entity, obj); if (entity is IStonComplexEntity) { InitializeFromSton(obj, document, entity as IStonComplexEntity, builtObjects); } } return((TObject)builtObjects[entity]); }
/// <summary> /// Writes a canonical string representation of a STON document to a stream. /// </summary> /// <param name="document">The document to write.</param> /// <param name="stream">The stream to write to.</param> public static void SaveCanonicalForm(this IStonDocument document, Stream stream) => Save(document, stream, CanonicalStonWriter.Instance);
public static void SaveCanonicalForm(this IStonDocument document, string path) => Save(document, path, CanonicalStonWriter.Instance);
/// <summary> /// Returns a canonical string representation of a STON document. /// </summary> /// <param name="document">The document to represent in its canonical form.</param> /// <returns>The canonical representation of the entity.</returns> public static string ToCanonicalForm(this IStonDocument document) => ToString(document, CanonicalStonWriter.Instance);
// creates an object from a STON value private static object CreateFromSton <TObject>(IStonDocument document, IStonValuedEntity entity, IDictionary <IStonValuedEntity, object> builtObjects) { var clrType = typeof(TObject); if (clrType == typeof(string)) { return((entity as IStonSimpleEntity).Value.Content); } else if (clrType == typeof(int) || clrType == typeof(int?) || clrType == typeof(long) || clrType == typeof(long?)) { // miiiight wanna make this a built-in part of STON library at some point IStonSimpleValue value = (entity as IStonSimpleEntity).Value; long result = 0; if (value.Content != "0") { int eidx = value.Content.IndexOf('e'); string sstr = value.Content.Remove(eidx); string estr = value.Content.Substring(eidx + 1); result = long.Parse(sstr); var exponent = int.Parse(estr); while (exponent-- > 0) { result *= 10; } } if (clrType == typeof(int) || clrType == typeof(int?)) { return((int)result); } else { return(result); } } else if (clrType == typeof(IPlaylistItem)) { switch ((entity.Type as IStonNamedType).Name) { case "Playlist": return(new Playlist()); case "Track": return(new Track()); default: throw new NotSupportedException(); } } else if (clrType == typeof(IStreamProvider)) { switch ((entity.Type as IStonNamedType).Name) { case "Loop": return(new LoopStreamProvider(builtObjects[document.GetParentContext(entity)] as Track)); default: throw new NotSupportedException(); } } else { throw new NotSupportedException(); } }