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()); } }
private object CopyCore(EmberWriter writer, int inner) { switch (inner) { case Ember.InnerNumber.Boolean: var boolean = this.ReadContentsAsBoolean(); writer.WriteValue(this.outer.GetValueOrDefault(), boolean); return(boolean); case Ember.InnerNumber.Integer: var int64 = this.ReadContentsAsInt64(); writer.WriteValue(this.outer.GetValueOrDefault(), int64); return(int64); case Ember.InnerNumber.Octetstring: var byteArray = this.ReadContentsAsByteArray(); writer.WriteValue(this.outer.GetValueOrDefault(), byteArray); return(byteArray); case Ember.InnerNumber.Real: var dbl = this.ReadContentsAsDouble(); writer.WriteValue(this.outer.GetValueOrDefault(), dbl); return(dbl); case Ember.InnerNumber.Utf8String: var str = this.ReadContentsAsString(); writer.WriteValue(this.outer.GetValueOrDefault(), str); return(str); case Ember.InnerNumber.RelativeObjectIdentifier: var int32Array = this.ReadContentsAsInt32Array(); writer.WriteValue(this.outer.GetValueOrDefault(), int32Array); return(int32Array); case Ember.InnerNumber.Sequence: writer.WriteStartSequence(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return(null); case Ember.InnerNumber.Set: writer.WriteStartSet(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return(null); default: writer.WriteStartApplicationDefinedType(this.outer.GetValueOrDefault(), inner); this.CopyToEndContainer(writer, null); return(null); } }
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()); } }
private object CopyCore(EmberWriter writer, int inner) { switch (inner) { case Ember.InnerNumber.Boolean: var boolean = this.ReadContentsAsBoolean(); writer.WriteValue(this.outer.GetValueOrDefault(), boolean); return boolean; case Ember.InnerNumber.Integer: var int64 = this.ReadContentsAsInt64(); writer.WriteValue(this.outer.GetValueOrDefault(), int64); return int64; case Ember.InnerNumber.Octetstring: var byteArray = this.ReadContentsAsByteArray(); writer.WriteValue(this.outer.GetValueOrDefault(), byteArray); return byteArray; case Ember.InnerNumber.Real: var dbl = this.ReadContentsAsDouble(); writer.WriteValue(this.outer.GetValueOrDefault(), dbl); return dbl; case Ember.InnerNumber.Utf8String: var str = this.ReadContentsAsString(); writer.WriteValue(this.outer.GetValueOrDefault(), str); return str; case Ember.InnerNumber.RelativeObjectIdentifier: var int32Array = this.ReadContentsAsInt32Array(); writer.WriteValue(this.outer.GetValueOrDefault(), int32Array); return int32Array; case Ember.InnerNumber.Sequence: writer.WriteStartSequence(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; case Ember.InnerNumber.Set: writer.WriteStartSet(this.outer.GetValueOrDefault()); this.CopyToEndContainer(writer, null); return null; default: writer.WriteStartApplicationDefinedType(this.outer.GetValueOrDefault(), inner); this.CopyToEndContainer(writer, null); return null; } }
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; } } }