private void DisplayRawObject(PSObject so, List <FormatValue> formatValueList) { FormatPropertyField fpf = new FormatPropertyField(); StringFormatError formatErrorObject = null; if (_errorManager.DisplayFormatErrorString) { // we send a format error object down to the formatting calls // only if we want to show the formatting error strings formatErrorObject = new StringFormatError(); } fpf.propertyValue = PSObjectHelper.SmartToString(so, _expressionFactory, _enumerationLimit, formatErrorObject); if (formatErrorObject != null && formatErrorObject.exception != null) { // if we did no thave any errors in the expression evaluation // we might have errors in the formatting, if present _errorManager.LogStringFormatError(formatErrorObject); if (_errorManager.DisplayFormatErrorString) { fpf.propertyValue = _errorManager.FormatErrorString; } } formatValueList.Add(fpf); formatValueList.Add(new FormatNewLine()); }
internal GroupStartData GenerateGroupStartData(PSObject firstObjectInGroup, int enumerationLimit) { GroupStartData data = new GroupStartData(); if (this.groupingManager != null) { object currentGroupingKeyPropertyValue = this.groupingManager.CurrentGroupingKeyPropertyValue; if (currentGroupingKeyPropertyValue == AutomationNull.Value) { return(data); } PSObject so = PSObjectHelper.AsPSObject(currentGroupingKeyPropertyValue); ControlBase control = null; TextToken tt = null; if (((this.dataBaseInfo.view != null) && (this.dataBaseInfo.view.groupBy != null)) && (this.dataBaseInfo.view.groupBy.startGroup != null)) { control = this.dataBaseInfo.view.groupBy.startGroup.control; tt = this.dataBaseInfo.view.groupBy.startGroup.labelTextToken; } data.groupingEntry = new GroupingEntry(); if (control == null) { string textTokenString; StringFormatError formatErrorObject = null; if (this.errorManager.DisplayFormatErrorString) { formatErrorObject = new StringFormatError(); } string formatErrorString = PSObjectHelper.SmartToString(so, this.expressionFactory, enumerationLimit, formatErrorObject); if ((formatErrorObject != null) && (formatErrorObject.exception != null)) { this.errorManager.LogStringFormatError(formatErrorObject); if (this.errorManager.DisplayFormatErrorString) { formatErrorString = this.errorManager.FormatErrorString; } } FormatEntry item = new FormatEntry(); data.groupingEntry.formatValueList.Add(item); FormatTextField field = new FormatTextField(); if (tt != null) { textTokenString = this.dataBaseInfo.db.displayResourceManagerCache.GetTextTokenString(tt); } else { textTokenString = this.groupingManager.GroupingKeyDisplayName; } field.text = StringUtil.Format(FormatAndOut_format_xxx.GroupStartDataIndentedAutoGeneratedLabel, textTokenString); item.formatValueList.Add(field); FormatPropertyField field2 = new FormatPropertyField { propertyValue = formatErrorString }; item.formatValueList.Add(field2); return(data); } new ComplexControlGenerator(this.dataBaseInfo.db, this.dataBaseInfo.view.loadingInfo, this.expressionFactory, this.dataBaseInfo.view.formatControlDefinitionHolder.controlDefinitionList, this.ErrorManager, enumerationLimit, this.errorContext).GenerateFormatEntries(50, control, firstObjectInGroup, data.groupingEntry.formatValueList); } return(data); }
/// <summary> /// Format an object using a provided format string directive. /// </summary> /// <param name="directive">Format directive object to use.</param> /// <param name="val">Object to format.</param> /// <param name="enumerationLimit">Limit on IEnumerable enumeration.</param> /// <param name="formatErrorObject">Formatting error object, if present.</param> /// <param name="expressionFactory">Expression factory to create PSPropertyExpression.</param> /// <returns>String representation.</returns> internal static string FormatField(FieldFormattingDirective directive, object val, int enumerationLimit, StringFormatError formatErrorObject, PSPropertyExpressionFactory expressionFactory) { PSObject so = PSObjectHelper.AsPSObject(val); bool isTable = false; if (directive is not null) { isTable = directive.isTable; if (!string.IsNullOrEmpty(directive.formatString)) { // we have a formatting directive, apply it // NOTE: with a format directive, we do not make any attempt // to deal with IEnumerable try { // use some heuristics to determine if we have "composite formatting" // 2004/11/16-JonN This is heuristic but should be safe enough if (directive.formatString.Contains("{0") || directive.formatString.Contains('}')) { // we do have it, just use it return(string.Format(CultureInfo.CurrentCulture, directive.formatString, so)); } // we fall back to the PSObject's IFormattable.ToString() // pass a null IFormatProvider return(so.ToString(directive.formatString, formatProvider: null)); } catch (Exception e) // 2004/11/17-JonN This covers exceptions thrown in // string.Format and PSObject.ToString(). // I think we can swallow these. { // NOTE: we catch all the exceptions, since we do not know // what the underlying object access would throw if (formatErrorObject is not null) { formatErrorObject.sourceObject = so; formatErrorObject.exception = e; formatErrorObject.formatString = directive.formatString; return(string.Empty); } } } } // we do not have a formatting directive or we failed the formatting (fallback) // but we did not report as an error; // this call would deal with IEnumerable if the object implements it return(PSObjectHelper.SmartToString(so, expressionFactory, enumerationLimit, formatErrorObject, isTable)); }
private void DisplayRawObject(PSObject so, List <FormatValue> formatValueList) { FormatPropertyField item = new FormatPropertyField(); StringFormatError formatErrorObject = null; if (this.errorManager.DisplayFormatErrorString) { formatErrorObject = new StringFormatError(); } item.propertyValue = PSObjectHelper.SmartToString(so, this.expressionFactory, this.enumerationLimit, formatErrorObject); if ((formatErrorObject != null) && (formatErrorObject.exception != null)) { this.errorManager.LogStringFormatError(formatErrorObject); if (this.errorManager.DisplayFormatErrorString) { item.propertyValue = this.errorManager.FormatErrorString; } } formatValueList.Add(item); formatValueList.Add(new FormatNewLine()); }
internal static string FormatField( FieldFormattingDirective directive, object val, int enumerationLimit, StringFormatError formatErrorObject, MshExpressionFactory expressionFactory) { PSObject so = PSObjectHelper.AsPSObject(val); if (directive != null) { if (!string.IsNullOrEmpty(directive.formatString)) { try { if (!directive.formatString.Contains("{0") && !directive.formatString.Contains("}")) { return(so.ToString(directive.formatString, (IFormatProvider)null)); } return(string.Format((IFormatProvider)CultureInfo.CurrentCulture, directive.formatString, (object)so)); } catch (Exception ex) { CommandProcessorBase.CheckForSevereException(ex); if (formatErrorObject != null) { formatErrorObject.sourceObject = (object)so; formatErrorObject.exception = ex; formatErrorObject.formatString = directive.formatString; return(""); } } } } return(PSObjectHelper.SmartToString(so, expressionFactory, enumerationLimit, formatErrorObject)); }
internal GroupStartData GenerateGroupStartData(PSObject firstObjectInGroup, int enumerationLimit) { GroupStartData startGroup = new GroupStartData(); if (_groupingManager is null) { return(startGroup); } object currentGroupingValue = _groupingManager.CurrentGroupingKeyPropertyValue; if (currentGroupingValue == AutomationNull.Value) { return(startGroup); } PSObject so = PSObjectHelper.AsPSObject(currentGroupingValue); // we need to determine how to display the group header ControlBase control = null; TextToken labelTextToken = null; if (this.dataBaseInfo.view != null && this.dataBaseInfo.view.groupBy != null) { if (this.dataBaseInfo.view.groupBy.startGroup != null) { // NOTE: from the database constraints, only one of the // two will be non null control = this.dataBaseInfo.view.groupBy.startGroup.control; labelTextToken = this.dataBaseInfo.view.groupBy.startGroup.labelTextToken; } } startGroup.groupingEntry = new GroupingEntry(); if (control is null) { // we do not have a control, we auto generate a // snippet of complex display using a label StringFormatError formatErrorObject = null; if (_errorManager.DisplayFormatErrorString) { // we send a format error object down to the formatting calls // only if we want to show the formatting error strings formatErrorObject = new StringFormatError(); } string currentGroupingValueDisplay = PSObjectHelper.SmartToString(so, this.expressionFactory, enumerationLimit, formatErrorObject); if (formatErrorObject != null && formatErrorObject.exception != null) { // if we did no thave any errors in the expression evaluation // we might have errors in the formatting, if present _errorManager.LogStringFormatError(formatErrorObject); if (_errorManager.DisplayFormatErrorString) { currentGroupingValueDisplay = _errorManager.FormatErrorString; } } FormatEntry fe = new FormatEntry(); startGroup.groupingEntry.formatValueList.Add(fe); FormatTextField ftf = new FormatTextField(); // determine what the label should be. If we have a label from the // database, let's use it, else fall back to the string provided // by the grouping manager string label; if (labelTextToken != null) { label = this.dataBaseInfo.db.displayResourceManagerCache.GetTextTokenString(labelTextToken); } else { label = _groupingManager.GroupingKeyDisplayName; } ftf.text = StringUtil.Format(FormatAndOut_format_xxx.GroupStartDataIndentedAutoGeneratedLabel, label); fe.formatValueList.Add(ftf); FormatPropertyField fpf = new FormatPropertyField(); fpf.propertyValue = currentGroupingValueDisplay; fe.formatValueList.Add(fpf); } else { // NOTE: we set a max depth to protect ourselves from infinite loops const int maxTreeDepth = 50; ComplexControlGenerator controlGenerator = new ComplexControlGenerator(this.dataBaseInfo.db, this.dataBaseInfo.view.loadingInfo, this.expressionFactory, this.dataBaseInfo.view.formatControlDefinitionHolder.controlDefinitionList, this.ErrorManager, enumerationLimit, this.errorContext); controlGenerator.GenerateFormatEntries(maxTreeDepth, control, firstObjectInGroup, startGroup.groupingEntry.formatValueList); } return(startGroup); }