/// <summary> /// Creates a new collection element path segment, with a given element index entity. /// </summary> /// <param name="elementIndex">The entity representing the index of the element in collection to access.</param> public StonCollectionElementPathSegment(IStonEntity elementIndex) { if (elementIndex == null) { throw new ArgumentNullException("elementIndex"); } ElementIndex = elementIndex; }
/// <summary> /// Returns a string representation of a STON entity, using specific STON writer. /// </summary> /// <param name="entity">The entity 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 IStonEntity entity, IStonWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } var stringWriter = new StringWriter(); writer.WriteEntity(stringWriter, entity); return(stringWriter.ToString()); }
/// <summary> /// Writes a STON entity to a given text writer. /// </summary> /// <param name="writer">The writer to write the entity to.</param> /// <param name="entity">The entity to write.</param> public void WriteEntity(TextWriter writer, IStonEntity entity) { if (writer == null) { throw new ArgumentNullException("writer"); } if (entity == null) { throw new ArgumentNullException("entity"); } WriteEntity(new StonTokenWriter(writer), entity); }
public static void Save(this IStonEntity entity, 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.WriteEntity(streamWriter, entity); } }
// 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)); } }
/// <summary> /// Writes a string representation of a STON entity to a stream, using specific STON writer, leaving the stream open. /// </summary> /// <param name="entity">The entity 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 IStonEntity entity, 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.WriteEntity(streamWriter, entity); } }
// checks that a construction loop appears in a given STON document // and that cycle loops between globally identified entities NODE0_p0, NODE1_p1, etc. up to LASTi_pi // where pn in NODEn_pn is the position of the next entity in the cycle private void ExpectCircularConstruction(string ston) { try { RegularStonReader.Default.ParseDocument(ston, null, null, s => true, s => true); Assert.Fail("The document is valid. This should *not* have happened."); } catch (StonCircularConstructionException ex) { Assert.AreEqual(ex.ConstructedValue, ex.ConstructionCycle.First().Key); string globalIdentifier = ex.ConstructedValue.GlobalIdentifier; int expectIdx = int.Parse(globalIdentifier.Substring(4, globalIdentifier.IndexOf('_') - 4)); int expectPosition; IStonEntity nextNode = null; foreach (var node in ex.ConstructionCycle) { if (nextNode != null) { Assert.AreEqual(nextNode, node.Key); } globalIdentifier = node.Key.GlobalIdentifier; Assert.AreEqual(expectIdx, int.Parse(globalIdentifier.Substring(4, globalIdentifier.IndexOf('_') - 4))); expectPosition = int.Parse(globalIdentifier.Substring(globalIdentifier.IndexOf('_') + 1)); nextNode = node.Key.Construction.PositionalParameters.Concat(node.Key.Construction.NamedParameters.Select(p => p.Value)).ElementAt(expectPosition); if (nextNode is IStonReferenceEntity) { nextNode = ex.Document.GetReferencedValue(nextNode as IStonReferenceEntity); } if (globalIdentifier.StartsWith("LAST")) { expectIdx = 0; } else { expectIdx++; } } globalIdentifier = ex.ConstructedValue.GlobalIdentifier; Assert.AreEqual(expectIdx, int.Parse(globalIdentifier.Substring(4, globalIdentifier.IndexOf('_') - 4))); Assert.AreEqual(nextNode, ex.ConstructedValue); } }
/// <summary> /// Creates a structurally equivalent entity from a given entity. /// </summary> /// <param name="entity">The entity to copy the structure of.</param> /// <returns>A structurally equivalent copy of the given entity.</returns> public static IStonEntity Copy(IStonEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity is IStonValuedEntity) { return(StonValuedEntity.Copy(entity as IStonValuedEntity)); } if (entity is IStonReferenceEntity) { return(StonReferenceEntity.Copy(entity as IStonReferenceEntity)); } throw new StonImplementationException(entity.GetType(), typeof(IStonEntity), typeof(IStonValuedEntity), typeof(IStonReferenceEntity)); }
/// <summary> /// Checks a validity of a given STON entity. /// </summary> /// <param name="entity">The entity to check the validity of.</param> public static void ValidateEntity(IStonEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } else if (entity is IStonValuedEntity) { ValidateEntity(entity as IStonValuedEntity); } else if (entity is IStonReferenceEntity) { ValidateEntity(entity as IStonReferenceEntity); } else { throw new StonImplementationException(entity.GetType(), typeof(IStonEntity), typeof(IStonValuedEntity), typeof(IStonReferenceEntity)); } }
// writes any entity private void WriteEntity(StonTokenWriter writer, IStonEntity entity) { if (entity == null) { throw new StonException("A non-existing entity has been found in the structure to be written."); } else if (entity is IStonValuedEntity) { WriteEntity(writer, entity as IStonValuedEntity); } else if (entity is IStonReferenceEntity) { WriteEntity(writer, entity as IStonReferenceEntity); } else { throw new StonImplementationException(entity.GetType(), typeof(IStonEntity), typeof(IStonValuedEntity), typeof(IStonReferenceEntity)); } }
/// <summary> /// Returns a hash code for a given entity, applied to entities semantic equivalence. /// </summary> /// <param name="obj">The entity to get a hash code of.</param> /// <returns>The hash code for the entity.</returns> public int GetHashCode(IStonEntity obj) { if (obj == null) { return(0); } else if (obj is IStonReferenceEntity) { return(GetHashCode(obj as IStonReferenceEntity)); } else if (obj is IStonValuedEntity) { return(GetHashCode(obj as IStonValuedEntity)); } else { return(0); } }
/// <summary> /// Determines whether two entities are semantically equivalent. /// </summary> /// <param name="x">The first entity to compare.</param> /// <param name="y">The second entity to compare.</param> /// <returns>True when entities are semantically equivalent, false otherwise.</returns> public bool Equals(IStonEntity x, IStonEntity y) { if (x == y) { return(true); } else if (x == null || y == null) { return(false); } IStonValuedEntity xval, yval; if (x is IStonReferenceEntity) { xval = Document.GetReferencedValue(x as IStonReferenceEntity); } else { xval = x as IStonValuedEntity; } if (y is IStonReferenceEntity) { yval = Document.GetReferencedValue(y as IStonReferenceEntity); } else { yval = y as IStonValuedEntity; } if (xval == null || yval == null) { return(false); } return(Equals(xval, yval)); }
/// <summary> /// Writes a canonical string representation of a STON entity to a stream. /// </summary> /// <param name="entity">The entity to write.</param> /// <param name="stream">The stream to write to.</param> public static void SaveCanonicalForm(this IStonEntity entity, Stream stream) => Save(entity, stream, CanonicalStonWriter.Instance);
public AnElementSegment(IStonEntity elementIndex) { ElementIndex = elementIndex; }
/// <summary> /// Returns a canonical string representation of a STON entity. /// </summary> /// <param name="entity">The entity to represent in its canonical form.</param> /// <returns>The canonical representation of the entity.</returns> public static string ToCanonicalForm(this IStonEntity entity) => ToString(entity, CanonicalStonWriter.Instance);
/// <summary> /// Creates a new collection element path segment, with a given element index. /// </summary> /// <param name="elementIndex">The entity representing the index of the element in collection to access.</param> /// <returns>The new STON path segment.</returns> public IStonCollectionElementPathSegment CreateCollectionElementPathSegment(IStonEntity elementIndex) => new StonCollectionElementPathSegment(elementIndex);
public static void SaveCanonicalForm(this IStonEntity entity, string path) => Save(entity, path, CanonicalStonWriter.Instance);
// general function to create a proper key-value pair of STON properties private static KeyValuePair <IStonBindingKey, IStonEntity> MakeStonProperty(string name, IStonEntity entity) { return(new KeyValuePair <IStonBindingKey, IStonEntity>(new StonBindingName(name), entity)); }