private void ValidateCurrentFormattingState(FormattingState expectedFormattingState, object obj) { if (this.currentFormattingState != expectedFormattingState) { string str = "format-*"; StartData data = obj as StartData; if (data != null) { if (data.shapeInfo.GetType() == typeof(WideViewHeaderInfo)) { str = "format-wide"; } else if (data.shapeInfo.GetType() == typeof(TableHeaderInfo)) { str = "format-table"; } else if (data.shapeInfo.GetType() == typeof(ListViewHeaderInfo)) { str = "format-list"; } else if (data.shapeInfo.GetType() == typeof(ComplexViewHeaderInfo)) { str = "format-complex"; } } string message = StringUtil.Format(FormatAndOut_out_xxx.OutLineOutput_OutOfSequencePacket, obj.GetType().FullName, str); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(), "ConsoleLineOutputOutOfSequencePacket", ErrorCategory.InvalidData, null) { ErrorDetails = new ErrorDetails(message) }; base.TerminatingErrorContext.ThrowTerminatingError(errorRecord); } }
private void VerifyState(FormattingState allowedStates, FormatData currentData) { if (!allowedStates.HasFlag(_state)) { var msg = String.Format("The format data is invalid. Did you modify the output of an Format-* command?", currentData.GetType().Name, _state); throw new InvalidOperationException(msg); } }
/// <summary> /// test if an object coming from the pipeline needs to be /// preprocessed by the default formatter /// </summary> /// <param name="o">object to examine for formatting</param> /// <returns>whether the object needs to be shunted to preprocessing</returns> private bool NeedsPreprocessing(object o) { FormatEntryData fed = o as FormatEntryData; if (fed != null) { // we got an already pre-processed object if (!fed.outOfBand) { // we allow out of band data in any state ValidateCurrentFormattingState(FormattingState.InsideGroup, o); } return(false); } else if (o is FormatStartData) { // when encountering FormatStartDate out of sequence, // pretend that the previous formatting directives were properly closed if (_currentFormattingState == FormattingState.InsideGroup) { this.EndProcessing(); this.BeginProcessing(); } // we got a Fs message, we enter a sequence ValidateCurrentFormattingState(FormattingState.Reset, o); _currentFormattingState = FormattingState.Formatting; return(false); } else if (o is FormatEndData) { // we got a Fe message, we exit a sequence ValidateCurrentFormattingState(FormattingState.Formatting, o); _currentFormattingState = FormattingState.Reset; return(false); } else if (o is GroupStartData) { ValidateCurrentFormattingState(FormattingState.Formatting, o); _currentFormattingState = FormattingState.InsideGroup; return(false); } else if (o is GroupEndData) { ValidateCurrentFormattingState(FormattingState.InsideGroup, o); _currentFormattingState = FormattingState.Formatting; return(false); } // this is a raw object return(true); }
public void ProcessPayload(FormatData data) { if (data is FormatStartData) { VerifyState(FormattingState.FormatEnd, data); ProcessFormatStart((FormatStartData)data); _state = FormattingState.FormatStart; } else if (data is GroupStartData) { var groupData = (GroupStartData)data; VerifyState(FormattingState.FormatStart | FormattingState.GroupEnd, data); ProcessGroupStart(groupData); _state = FormattingState.GroupStart; } else if (data is SimpleFormatEntryData) { // may occur in any state ProcessFormatEntry((SimpleFormatEntryData)data); } else if (data is ErrorFormatEntryData) { // may occur in any state ProcessFormatEntry((ErrorFormatEntryData)data); } else if (data is FormatEntryData) { VerifyState(FormattingState.GroupStart, data); ProcessFormatEntry((FormatEntryData)data); } else if (data is GroupEndData) { VerifyState(FormattingState.GroupStart, data); ProcessGroupEnd((GroupEndData)data); _state = FormattingState.GroupEnd; } else if (data is FormatEndData) { VerifyState(FormattingState.GroupEnd, data); ProcessFormatEnd((FormatEndData)data); _state = FormattingState.GroupEnd; } else { var msg = String.Format("The format data of type {0} is unknown and cannot be processed", data.GetType().Name); throw new InvalidOperationException(msg); } }
public Collection <FormatData> Process(PSObject psobj) { var formatData = new Collection <FormatData>(); if (psobj.BaseObject is FormatData) { formatData.Add((FormatData)psobj.BaseObject); return(formatData); } if (_generator == null) { if (Shape.Equals(FormatShape.Undefined)) { Shape = FormatShapeHelper.SelectByData(psobj); } _generator = FormatGenerator.Get(_executionContext, Shape, Options); } // check if we have a simple type or an error type. if yes, then we don't need a document structure if (psobj.BaseObject == null || psobj.BaseObject.GetType().IsSimple()) { formatData.Add(_generator.GenerateSimpleFormatEntry(psobj)); return(formatData); } if (psobj.BaseObject is ErrorRecord || psobj.BaseObject is Exception) { formatData.Add(_generator.GenerateErrorFormatEntry(psobj)); return(formatData); } // object to be printed get a complete document structure if (_state.Equals(FormattingState.FormatEnd)) { formatData.Add(_generator.GenerateFormatStart()); _state = FormattingState.FormatStart; } if (_state.Equals(FormattingState.GroupStart) && ShouldChangeGroup(psobj)) { formatData.Add(_generator.GenerateGroupEnd()); _state = FormattingState.GroupEnd; } if (_state.Equals(FormattingState.GroupEnd) || _state.Equals(FormattingState.FormatStart)) { formatData.Add(_generator.GenerateGroupStart(psobj)); _state = FormattingState.GroupStart; } // we have to be in the state GroupStart where we can write the data itself formatData.Add(_generator.GenerateObjectFormatEntry(psobj)); return(formatData); }
public Collection<FormatData> End() { var formatData = new Collection<FormatData>(); if (_state.Equals(FormattingState.GroupStart)) { // generator cannot be null formatData.Add(_generator.GenerateGroupEnd()); _state = FormattingState.GroupEnd; } if (_state.Equals(FormattingState.GroupEnd) || _state.Equals(FormattingState.FormatStart)) { formatData.Add(_generator.GenerateFormatEnd()); _state = FormattingState.FormatEnd; } return formatData; }
public Collection <FormatData> End() { var formatData = new Collection <FormatData>(); if (_state.Equals(FormattingState.GroupStart)) { // generator cannot be null formatData.Add(_generator.GenerateGroupEnd()); _state = FormattingState.GroupEnd; } if (_state.Equals(FormattingState.GroupEnd) || _state.Equals(FormattingState.FormatStart)) { formatData.Add(_generator.GenerateFormatEnd()); _state = FormattingState.FormatEnd; } return(formatData); }
private bool NeedsPreprocessing(object o) { FormatEntryData data = o as FormatEntryData; if (data != null) { if (!data.outOfBand) { this.ValidateCurrentFormattingState(FormattingState.InsideGroup, o); } return(false); } if (o is FormatStartData) { if (this.currentFormattingState == FormattingState.InsideGroup) { this.EndProcessing(); this.BeginProcessing(); } this.ValidateCurrentFormattingState(FormattingState.Reset, o); this.currentFormattingState = FormattingState.Formatting; return(false); } if (o is FormatEndData) { this.ValidateCurrentFormattingState(FormattingState.Formatting, o); this.currentFormattingState = FormattingState.Reset; return(false); } if (o is GroupStartData) { this.ValidateCurrentFormattingState(FormattingState.Formatting, o); this.currentFormattingState = FormattingState.InsideGroup; return(false); } if (o is GroupEndData) { this.ValidateCurrentFormattingState(FormattingState.InsideGroup, o); this.currentFormattingState = FormattingState.Formatting; return(false); } return(true); }
private void ValidateCurrentFormattingState(FormattingState expectedFormattingState, object obj) { // check if we are in the expected formatting state if (_currentFormattingState != expectedFormattingState) { // we are not in the expected state, some message is out of sequence, // need to abort the command string violatingCommand = "format-*"; StartData sdObj = obj as StartData; if (sdObj != null) { if (sdObj.shapeInfo is WideViewHeaderInfo) { violatingCommand = "format-wide"; } else if (sdObj.shapeInfo is TableHeaderInfo) { violatingCommand = "format-table"; } else if (sdObj.shapeInfo is ListViewHeaderInfo) { violatingCommand = "format-list"; } else if (sdObj.shapeInfo is ComplexViewHeaderInfo) { violatingCommand = "format-complex"; } } string msg = StringUtil.Format(FormatAndOut_out_xxx.OutLineOutput_OutOfSequencePacket, obj.GetType().FullName, violatingCommand); ErrorRecord errorRecord = new ErrorRecord( new InvalidOperationException(), "ConsoleLineOutputOutOfSequencePacket", ErrorCategory.InvalidData, null); errorRecord.ErrorDetails = new ErrorDetails(msg); this.TerminatingErrorContext.ThrowTerminatingError(errorRecord); } }
protected FormatProcessor(FormatShape shape, OutputWriter outputWriter) { Shape = shape; OutputWriter = outputWriter; _state = FormattingState.FormatEnd; }
internal FormatManager(FormatShape shape, ExecutionContext context) { Shape = shape; Options = new FormatGeneratorOptions(); _state = FormattingState.FormatEnd; }
public Collection<FormatData> Process(PSObject psobj) { var formatData = new Collection<FormatData>(); if (psobj.BaseObject is FormatData) { formatData.Add((FormatData)psobj.BaseObject); return formatData; } if (_generator == null) { if (Shape.Equals(FormatShape.Undefined)) { Shape = FormatShapeHelper.SelectByData(psobj); } _generator = FormatGenerator.Get(_executionContext, Shape, Options); } // check if we have a simple type or an error type. if yes, then we don't need a document structure if (psobj.BaseObject == null || psobj.BaseObject.GetType().IsSimple()) { formatData.Add(_generator.GenerateSimpleFormatEntry(psobj)); return formatData; } if (psobj.BaseObject is ErrorRecord || psobj.BaseObject is Exception) { formatData.Add(_generator.GenerateErrorFormatEntry(psobj)); return formatData; } // object to be printed get a complete document structure if (_state.Equals(FormattingState.FormatEnd)) { formatData.Add(_generator.GenerateFormatStart()); _state = FormattingState.FormatStart; } if (_state.Equals(FormattingState.GroupStart) && ShouldChangeGroup(psobj)) { formatData.Add(_generator.GenerateGroupEnd()); _state = FormattingState.GroupEnd; } if (_state.Equals(FormattingState.GroupEnd) || _state.Equals(FormattingState.FormatStart)) { formatData.Add(_generator.GenerateGroupStart(psobj)); _state = FormattingState.GroupStart; } // we have to be in the state GroupStart where we can write the data itself formatData.Add(_generator.GenerateObjectFormatEntry(psobj)); return formatData; }
private bool NeedsPreprocessing(object o) { FormatEntryData data = o as FormatEntryData; if (data != null) { if (!data.outOfBand) { this.ValidateCurrentFormattingState(FormattingState.InsideGroup, o); } return false; } if (o is FormatStartData) { if (this.currentFormattingState == FormattingState.InsideGroup) { this.EndProcessing(); this.BeginProcessing(); } this.ValidateCurrentFormattingState(FormattingState.Reset, o); this.currentFormattingState = FormattingState.Formatting; return false; } if (o is FormatEndData) { this.ValidateCurrentFormattingState(FormattingState.Formatting, o); this.currentFormattingState = FormattingState.Reset; return false; } if (o is GroupStartData) { this.ValidateCurrentFormattingState(FormattingState.Formatting, o); this.currentFormattingState = FormattingState.InsideGroup; return false; } if (o is GroupEndData) { this.ValidateCurrentFormattingState(FormattingState.InsideGroup, o); this.currentFormattingState = FormattingState.Formatting; return false; } return true; }
/// <summary> /// test if an object coming from the pipeline needs to be /// preprocessed by the default formatter /// </summary> /// <param name="o">object to examine for formatting</param> /// <returns>whether the object needs to be shunted to preprocessing</returns> private bool NeedsPreprocessing(object o) { FormatEntryData fed = o as FormatEntryData; if (fed != null) { // we got an already pre-processed object if (!fed.outOfBand) { // we allow out of band data in any state ValidateCurrentFormattingState(FormattingState.InsideGroup, o); } return false; } else if (o is FormatStartData) { // when encountering FormatStartDate out of sequence, // pretend that the previous formatting directives were properly closed if (_currentFormattingState == FormattingState.InsideGroup) { this.EndProcessing(); this.BeginProcessing(); } // we got a Fs message, we enter a sequence ValidateCurrentFormattingState(FormattingState.Reset, o); _currentFormattingState = FormattingState.Formatting; return false; } else if (o is FormatEndData) { // we got a Fe message, we exit a sequence ValidateCurrentFormattingState(FormattingState.Formatting, o); _currentFormattingState = FormattingState.Reset; return false; } else if (o is GroupStartData) { ValidateCurrentFormattingState(FormattingState.Formatting, o); _currentFormattingState = FormattingState.InsideGroup; return false; } else if (o is GroupEndData) { ValidateCurrentFormattingState(FormattingState.InsideGroup, o); _currentFormattingState = FormattingState.Formatting; return false; } // this is a raw object return true; }