/// <summary>Reads data and writes it to <paramref name="writer"/> until the end of the current container is /// reached.</summary> /// <returns>The contents of the of the data value with the outer id <paramref name="outerId"/>, if such a /// data value was found in the current container and its contents is primitive; otherwise, <c>null</c>. /// </returns> /// <exception cref="ObjectDisposedException"><see cref="Dispose"/> has been called.</exception> /// <remarks> /// <para>While <see cref="Read"/> returns <c>true</c> and <see cref="InnerNumber"/> is not equal to /// <see cref="Ember.InnerNumber.EndContainer"/>, calls <see cref="Copy"/>.</para> /// </remarks> public object CopyToEndContainer(EmberWriter writer, EmberId?outerId) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } object result = null; var inner = -1; while (this.Read() && ((inner = this.innerNumber.GetValueOrDefault()) != Ember.InnerNumber.EndContainer)) { var candidate = this.CopyCore(writer, inner); if (this.outer.HasValue && (this.outer.Value == outerId)) { result = candidate; } } if (inner == Ember.InnerNumber.EndContainer) { writer.WriteEndContainer(); } return(result); }
public void ExceptionTest() { AssertThrow <ArgumentNullException>(() => new EmberWriter(null, 1).Dispose()); AssertThrow <ArgumentException>(() => new EmberWriter(new MemoryStream(), 0).Dispose()); using (var writer = new EmberWriter(new MemoryStream(), 1)) { var outer = EmberId.CreateApplication(0); AssertThrow <ArgumentNullException>( () => writer.WriteValue(outer, (byte[])null), () => writer.WriteValue(outer, (int[])null), () => writer.WriteValue(outer, (string)null)); AssertThrow <ArgumentOutOfRangeException>( () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication - 1)); writer.Dispose(); AssertThrow <ObjectDisposedException>( () => writer.WriteValue(outer, true), () => writer.WriteValue(outer, 0), () => writer.WriteValue(outer, new byte[] { }), () => writer.WriteValue(outer, 0.0), () => writer.WriteValue(outer, string.Empty), () => writer.WriteValue(outer, new int[] { }), () => writer.WriteStartSequence(outer), () => writer.WriteStartSet(outer), () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication), () => writer.WriteEndContainer()); } }
public void ExceptionTest() { AssertThrow<ArgumentNullException>(() => new EmberWriter(null, 1).Dispose()); AssertThrow<ArgumentException>(() => new EmberWriter(new MemoryStream(), 0).Dispose()); using (var writer = new EmberWriter(new MemoryStream(), 1)) { var outer = EmberId.CreateApplication(0); AssertThrow<ArgumentNullException>( () => writer.WriteValue(outer, (byte[])null), () => writer.WriteValue(outer, (int[])null), () => writer.WriteValue(outer, (string)null)); AssertThrow<ArgumentOutOfRangeException>( () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication - 1)); writer.Dispose(); AssertThrow<ObjectDisposedException>( () => writer.WriteValue(outer, true), () => writer.WriteValue(outer, 0), () => writer.WriteValue(outer, new byte[] { }), () => writer.WriteValue(outer, 0.0), () => writer.WriteValue(outer, string.Empty), () => writer.WriteValue(outer, new int[] { }), () => writer.WriteStartSequence(outer), () => writer.WriteStartSet(outer), () => writer.WriteStartApplicationDefinedType(outer, InnerNumber.FirstApplication), () => writer.WriteEndContainer()); } }
/// <summary>Reads data and writes it to <paramref name="writer"/> until the end of the current container is /// reached.</summary> /// <returns>The contents of the of the data value with the outer id <paramref name="outerId"/>, if such a /// data value was found in the current container and its contents is primitive; otherwise, <c>null</c>. /// </returns> /// <exception cref="ObjectDisposedException"><see cref="Dispose"/> has been called.</exception> /// <remarks> /// <para>While <see cref="Read"/> returns <c>true</c> and <see cref="InnerNumber"/> is not equal to /// <see cref="Ember.InnerNumber.EndContainer"/>, calls <see cref="Copy"/>.</para> /// </remarks> public object CopyToEndContainer(EmberWriter writer, EmberId? outerId) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } object result = null; var inner = -1; while (this.Read() && ((inner = this.innerNumber.GetValueOrDefault()) != Ember.InnerNumber.EndContainer)) { var candidate = this.CopyCore(writer, inner); if (this.outer.HasValue && (this.outer.Value == outerId)) { result = candidate; } } if (inner == Ember.InnerNumber.EndContainer) { writer.WriteEndContainer(); } return result; }
private void FromXmlCore( XmlReader reader, EmberWriter writer, FieldPath <string, string> previousPath, string currentType) { if (reader.IsEmptyElement) { writer.WriteEndContainer(); return; } while (ReadNext(reader)) { if (reader.NodeType == XmlNodeType.EndElement) { writer.WriteEndContainer(); return; } if (reader.NodeType != XmlNodeType.Element) { const string Format = "Unexpected Node Type: Encountered {0} while looking for {1}."; throw new XmlException( string.Format(InvariantCulture, Format, reader.NodeType, XmlNodeType.Element)); } var currentPath = Combine(previousPath, new Field <string, string>(currentType, reader.Name)); var fieldId = this.GetFieldId(currentPath); if (reader.AttributeCount != 1) { throw new XmlException( "Unexpected Attribute Count: Each element must have exactly one type attribute."); } var nextType = reader.GetAttribute(0); switch (nextType) { case BerBoolean.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsBoolean())); break; case BerInteger.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsLong())); break; case BerOctetstring.Name: WriteOctetstring(reader, writer, fieldId); break; case BerReal.Name: writer.WriteValue(fieldId, ReadValue(reader, r => r.ReadContentAsDouble())); break; case BerUtf8String.Name: var value = ReadValue(reader, r => r.ReadContentAsString(), string.Empty); switch (currentPath.ToString()) { case "Parameter.contents.formula": case "Parameter.contents.enumeration": case "Parameter.contents.schemaIdentifiers": case "QualifiedParameter.contents.formula": case "QualifiedParameter.contents.enumeration": case "QualifiedParameter.contents.schemaIdentifiers": case "Node.contents.schemaIdentifiers": case "QualifiedNode.contents.schemaIdentifiers": case "Matrix.contents.schemaIdentifiers": case "QualifiedMatrix.contents.schemaIdentifiers": value = value.Replace(Environment.NewLine, "\n"); break; default: // Intentionally do nothing break; } writer.WriteValue(fieldId, value); break; case BerRelativeObjectIdentifier.Name: WriteRelativeObjectIdentifier(reader, writer, fieldId); break; case BerSequence.Name: writer.WriteStartSequence(fieldId); this.FromXmlCore(reader, writer, currentPath, nextType); break; case BerSet.Name: writer.WriteStartSet(fieldId); this.FromXmlCore(reader, writer, currentPath, nextType); break; default: writer.WriteStartApplicationDefinedType(fieldId, this.GetInnerNumber(nextType)); this.FromXmlCore(reader, writer, currentPath, nextType); break; } } }