/// <summary> /// Replaces the current scope with a new scope; checks that the transition is valid. /// </summary> /// <param name="newState">The new state to transition into.</param> /// <param name="item">The item associated with the new state.</param> private void ReplaceScope(CollectionWriterState newState, ODataItem item) { this.ValidateTransition(newState); this.scopes.Pop(); this.scopes.Push(new Scope(newState, item)); this.NotifyListener(newState); }
/// <summary> /// Verify that the transition from the current state into new state is valid . /// </summary> /// <param name="newState">The new writer state to transition into.</param> private void ValidateTransition(CollectionWriterState newState) { if (!IsErrorState(this.State) && IsErrorState(newState)) { // we can always transition into an error state if we are not already in an error state return; } switch (this.State) { case CollectionWriterState.Start: if (newState != CollectionWriterState.Collection && newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromStart(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Collection: if (newState != CollectionWriterState.Item && newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromCollection(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Item: if (newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromItem(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Completed: // we should never see a state transition when in state 'Completed' throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromCompleted(this.State.ToString(), newState.ToString())); case CollectionWriterState.Error: if (newState != CollectionWriterState.Error) { // No more state transitions once we are in error state throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromError(this.State.ToString(), newState.ToString())); } break; default: throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataCollectionWriterCore_ValidateTransition_UnreachableCodePath)); } }
/// <summary> /// Notifies the implementer of the <see cref="IODataReaderWriterListener"/> interface of relevant state changes in the writer. /// </summary> /// <param name="newState">The new writer state.</param> private void NotifyListener(CollectionWriterState newState) { if (this.listener != null) { if (IsErrorState(newState)) { this.listener.OnException(); } else if (newState == CollectionWriterState.Completed) { this.listener.OnCompleted(); } } }
private void ValidateTransition(CollectionWriterState newState) { if (IsErrorState(this.State) || !IsErrorState(newState)) { switch (this.State) { case CollectionWriterState.Start: if ((newState != CollectionWriterState.Collection) && (newState != CollectionWriterState.Completed)) { throw new ODataException(Microsoft.Data.OData.Strings.ODataCollectionWriterCore_InvalidTransitionFromStart(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Collection: if ((newState != CollectionWriterState.Item) && (newState != CollectionWriterState.Completed)) { throw new ODataException(Microsoft.Data.OData.Strings.ODataCollectionWriterCore_InvalidTransitionFromCollection(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Item: if (newState != CollectionWriterState.Completed) { throw new ODataException(Microsoft.Data.OData.Strings.ODataCollectionWriterCore_InvalidTransitionFromItem(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Completed: throw new ODataException(Microsoft.Data.OData.Strings.ODataWriterCore_InvalidTransitionFromCompleted(this.State.ToString(), newState.ToString())); case CollectionWriterState.Error: if (newState != CollectionWriterState.Error) { throw new ODataException(Microsoft.Data.OData.Strings.ODataWriterCore_InvalidTransitionFromError(this.State.ToString(), newState.ToString())); } break; default: throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodes.ODataCollectionWriterCore_ValidateTransition_UnreachableCodePath)); } } }
/// <summary> /// Enter a new writer scope; verifies that the transition from the current state into new state is valid /// and attaches the item to the new scope. /// </summary> /// <param name="newState">The writer state to transition into.</param> /// <param name="item">The item to associate with the new scope.</param> private void EnterScope(CollectionWriterState newState, object item) { this.InterceptException(() => this.ValidateTransition(newState)); this.scopes.Push(new Scope(newState, item)); this.NotifyListener(newState); }
/// <summary> /// Determines whether a given writer state is considered an error state. /// </summary> /// <param name="state">The writer state to check.</param> /// <returns>True if the writer state is an error state; otherwise false.</returns> protected static bool IsErrorState(CollectionWriterState state) { return(state == CollectionWriterState.Error); }
/// <summary> /// Constructor creating a new writer scope. /// </summary> /// <param name="state">The writer state of this scope.</param> /// <param name="item">The item attached to this scope.</param> public Scope(CollectionWriterState state, object item) { this.state = state; this.item = item; }
/// <summary> /// Verify that the transition from the current state into new state is valid . /// </summary> /// <param name="newState">The new writer state to transition into.</param> private void ValidateTransition(CollectionWriterState newState) { if (!IsErrorState(this.State) && IsErrorState(newState) && !(this.State == CollectionWriterState.Completed && newState == CollectionWriterState.Error)) { // we can always transition into an error state if we are not already in an error state // unless we're in a completed state trying to write an error (we can't write error after the payload // was finished as it might introduce another top-level element in XML) return; } switch (this.State) { case CollectionWriterState.Start: if (newState != CollectionWriterState.Collection && newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromStart(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Collection: if (newState != CollectionWriterState.Item && newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromCollection(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Item: if (newState != CollectionWriterState.Completed) { throw new ODataException(Strings.ODataCollectionWriterCore_InvalidTransitionFromItem(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Completed: // we should never see a state transition when in state 'Completed' throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromCompleted(this.State.ToString(), newState.ToString())); case CollectionWriterState.ODataExceptionThrown: if (!IsErrorState(newState)) { // once an exception has been thrown we only allow clients to write an error (or we detect a fatal exception) throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromODataExceptionThrown(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.Error: // No more state transitions once we are in error state except for the fatal error if (newState != CollectionWriterState.FatalExceptionThrown) { throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromError(this.State.ToString(), newState.ToString())); } break; case CollectionWriterState.FatalExceptionThrown: if (newState != CollectionWriterState.FatalExceptionThrown) { throw new ODataException(Strings.ODataWriterCore_InvalidTransitionFromFatalExceptionThrown(this.State.ToString(), newState.ToString())); } break; default: throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataCollectionWriterCore_ValidateTransition_UnreachableCodePath)); } }
/// <summary> /// Determines whether a given writer state is considered an error state. /// </summary> /// <param name="state">The writer state to check.</param> /// <returns>True if the writer state is an error state; otherwise false.</returns> protected static bool IsErrorState(CollectionWriterState state) { return state == CollectionWriterState.Error; }
/// <summary> /// Determines whether a given writer state is considered an error state. /// </summary> /// <param name="state">The writer state to check.</param> /// <returns>True if the writer state is an error state; otherwise false.</returns> protected static bool IsErrorState(CollectionWriterState state) { return state == CollectionWriterState.Error || state == CollectionWriterState.ODataExceptionThrown || state == CollectionWriterState.FatalExceptionThrown; }
/// <summary> /// Determines whether a given writer state is considered an error state. /// </summary> /// <param name="state">The writer state to check.</param> /// <returns>True if the writer state is an error state; otherwise false.</returns> protected static bool IsErrorState(CollectionWriterState state) { return(state == CollectionWriterState.Error || state == CollectionWriterState.ODataExceptionThrown || state == CollectionWriterState.FatalExceptionThrown); }