Esempio n. 1
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonDocumentWriter class.
 /// </summary>
 /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param>
 /// <param name="settings">The settings.</param>
 public BsonDocumentWriter(BsonDocument topLevelDocument, BsonDocumentWriterSettings settings)
     : base(settings)
 {
     _topLevelDocument = topLevelDocument;
     _documentWriterSettings = settings; // already frozen by base class
     _context = null;
     State = BsonWriterState.Initial;
 }
 // public methods
 /// <summary>
 /// Closes the writer.
 /// </summary>
 public override void Close()
 {
     // Close can be called on Disposed objects
     if (State != BsonWriterState.Closed)
     {
         _context = null;
         State = BsonWriterState.Closed;
     }
 }
 internal BsonDocumentWriterContext(
     BsonDocumentWriterContext parentContext,
     ContextType contextType,
     string code)
 {
     _parentContext = parentContext;
     _contextType = contextType;
     _code = code;
 }
 internal BsonDocumentWriterContext(
     BsonDocumentWriterContext parentContext,
     ContextType contextType,
     BsonArray array)
 {
     _parentContext = parentContext;
     _contextType = contextType;
     _array = array;
 }
 // constructors
 internal BsonDocumentWriterContext(
     BsonDocumentWriterContext parentContext,
     ContextType contextType,
     BsonDocument document)
 {
     _parentContext = parentContext;
     _contextType = contextType;
     _document = document;
 }
        // constructors
        /// <summary>
        /// Initializes a new instance of the BsonDocumentWriter class.
        /// </summary>
        /// <param name="topLevelDocument">The document to write to (normally starts out as an empty document).</param>
        /// <param name="settings">The settings.</param>
        public BsonDocumentWriter(BsonDocument topLevelDocument, BsonDocumentWriterSettings settings)
            : base(settings)
        {
            if (topLevelDocument == null)
            {
                throw new ArgumentNullException("topLevelDocument");
            }

            _topLevelDocument = topLevelDocument;
            _documentWriterSettings = settings; // already frozen by base class
            _context = null;
            State = BsonWriterState.Initial;
        }
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Initial && State != BsonWriterState.Value && State != BsonWriterState.ScopeDocument && State != BsonWriterState.Done)
            {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Initial, BsonWriterState.Value, BsonWriterState.ScopeDocument, BsonWriterState.Done);
            }

            base.WriteStartDocument();
            switch (State)
            {
                case BsonWriterState.Initial:
                case BsonWriterState.Done:
                    _context = new BsonDocumentWriterContext(null, ContextType.Document, _topLevelDocument);
                    break;
                case BsonWriterState.Value:
                    _context = new BsonDocumentWriterContext(_context, ContextType.Document, new BsonDocument());
                    break;
                case BsonWriterState.ScopeDocument:
                    _context = new BsonDocumentWriterContext(_context, ContextType.ScopeDocument, new BsonDocument());
                    break;
                default:
                    throw new BsonInternalException("Unexpected state.");
            }

            State = BsonWriterState.Name;
        }
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteStartArray", BsonWriterState.Value);
            }

            base.WriteStartArray();
            _context = new BsonDocumentWriterContext(_context, ContextType.Array, new BsonArray());
            State = BsonWriterState.Value;
        }
        /// <summary>
        /// Writes a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope).
        /// </summary>
        /// <param name="code">The JavaScript code.</param>
        public override void WriteJavaScriptWithScope(string code)
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteJavaScriptWithScope", BsonWriterState.Value);
            }

            _context = new BsonDocumentWriterContext(_context, ContextType.JavaScriptWithScope, code);
            State = BsonWriterState.ScopeDocument;
        }
        /// <summary>
        /// Writes the end of a BSON document to the writer.
        /// </summary>
        public override void WriteEndDocument()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Name)
            {
                ThrowInvalidState("WriteEndDocument", BsonWriterState.Name);
            }
            if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
            {
                ThrowInvalidContextType("WriteEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }

            base.WriteEndDocument();
            if (_context.ContextType == ContextType.ScopeDocument)
            {
                var scope = _context.Document;
                _context = _context.ParentContext;
                var code = _context.Code;
                _context = _context.ParentContext;
                WriteValue(new BsonJavaScriptWithScope(code, scope));
            }
            else
            {
                var document = _context.Document;
                _context = _context.ParentContext;
                if (_context != null)
                {
                    WriteValue(document);
                }
            }

            if (_context == null)
            {
                State = BsonWriterState.Done;
            }
            else
            {
                State = GetNextState();
            }
        }
        /// <summary>
        /// Writes the end of a BSON array to the writer.
        /// </summary>
        public override void WriteEndArray()
        {
            if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteEndArray", BsonWriterState.Value);
            }
            if (_context.ContextType != ContextType.Array)
            {
                ThrowInvalidContextType("WriteEndArray", _context.ContextType, ContextType.Array);
            }

            base.WriteEndArray();
            var array = _context.Array;
            _context = _context.ParentContext;
            WriteValue(array);
            State = GetNextState();
        }