Exemple #1
0
 internal static string FormatField(FieldFormattingDirective directive, object val, int enumerationLimit, StringFormatError formatErrorObject, MshExpressionFactory expressionFactory)
 {
     PSObject so = AsPSObject(val);
     if ((directive != null) && !string.IsNullOrEmpty(directive.formatString))
     {
         try
         {
             if (directive.formatString.Contains("{0") || directive.formatString.Contains("}"))
             {
                 return string.Format(CultureInfo.CurrentCulture, directive.formatString, new object[] { so });
             }
             return so.ToString(directive.formatString, null);
         }
         catch (Exception exception)
         {
             CommandProcessorBase.CheckForSevereException(exception);
             if (formatErrorObject != null)
             {
                 formatErrorObject.sourceObject = so;
                 formatErrorObject.exception = exception;
                 formatErrorObject.formatString = directive.formatString;
                 return "";
             }
         }
     }
     return SmartToString(so, expressionFactory, enumerationLimit, formatErrorObject);
 }
        protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex,
                                                   FieldFormattingDirective directive, out MshExpressionResult expressionResult)
        {
            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 retVal = PSObjectHelper.GetExpressionDisplayValue(so, enumerationLimit, ex,
                                                                     directive, formatErrorObject, expressionFactory, out expressionResult);

            if (expressionResult != null)
            {
                // we obtained a result, check if there is an error
                if (expressionResult.Exception != null)
                {
                    _errorManager.LogMshExpressionFailedResult(expressionResult, so);
                    if (_errorManager.DisplayErrorStrings)
                    {
                        retVal = _errorManager.ErrorString;
                    }
                }
                else 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.DisplayErrorStrings)
                    {
                        retVal = _errorManager.FormatErrorString;
                    }
                }
            }
            return(retVal);
        }
Exemple #3
0
        private WideViewEntry GenerateWideViewEntryFromProperties(PSObject so, int enumerationLimit)
        {
            if (base.activeAssociationList == null)
            {
                this.SetUpActiveProperty(so);
            }
            WideViewEntry       entry = new WideViewEntry();
            FormatPropertyField field = new FormatPropertyField();

            entry.formatPropertyField = field;
            if (base.activeAssociationList.Count > 0)
            {
                MshResolvedExpressionParameterAssociation association = base.activeAssociationList[0];
                FieldFormattingDirective directive = null;
                if (association.OriginatingParameter != null)
                {
                    directive = association.OriginatingParameter.GetEntry("formatString") as FieldFormattingDirective;
                }
                field.propertyValue = base.GetExpressionDisplayValue(so, enumerationLimit, association.ResolvedExpression, directive);
            }
            base.activeAssociationList = null;
            return(entry);
        }
        /// <summary>
        /// helper to retrieve the value of an PSPropertyExpression and to format it
        /// </summary>
        /// <param name="so">shell object to process</param>
        /// <param name="enumerationLimit">limit on IEnumerable enumeration</param>
        /// <param name="ex">expression to use for retrieval</param>
        /// <param name="directive">format directive to use for formatting</param>
        /// <param name="formatErrorObject"></param>
        /// <param name="expressionFactory">expression factory to create PSPropertyExpression</param>
        /// <param name="result"> not null if an error condition arose</param>
        /// <returns>formatted string</returns>
        internal static string GetExpressionDisplayValue(
            PSObject so,
            int enumerationLimit,
            PSPropertyExpression ex,
            FieldFormattingDirective directive,
            StringFormatError formatErrorObject,
            PSPropertyExpressionFactory expressionFactory,
            out PSPropertyExpressionResult result)
        {
            result = null;
            List <PSPropertyExpressionResult> resList = ex.GetValues(so);

            if (resList.Count == 0)
            {
                return(string.Empty);
            }

            result = resList[0];
            if (result.Exception != null)
            {
                return(string.Empty);
            }
            return(PSObjectHelper.FormatField(directive, result.Result, enumerationLimit, formatErrorObject, expressionFactory));
        }
        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));
        }
Exemple #6
0
        private void ExecuteFormatTokenList(TraversalInfo level,
                                            PSObject so, List <FormatToken> formatTokenList, List <FormatValue> formatValueList)
        {
            if (so == null)
            {
                throw PSTraceSource.NewArgumentNullException("so");
            }

            // guard against infinite loop
            if (level.Level == level.MaxDepth)
            {
                return;
            }

            FormatEntry fe = new FormatEntry();

            formatValueList.Add(fe);
            #region foreach loop
            foreach (FormatToken t in formatTokenList)
            {
                TextToken tt = t as TextToken;
                if (tt != null)
                {
                    FormatTextField ftf = new FormatTextField();
                    ftf.text = _db.displayResourceManagerCache.GetTextTokenString(tt);
                    fe.formatValueList.Add(ftf);
                    continue;
                }
                var newline = t as NewLineToken;
                if (newline != null)
                {
                    for (int i = 0; i < newline.count; i++)
                    {
                        fe.formatValueList.Add(new FormatNewLine());
                    }
                    continue;
                }
                FrameToken ft = t as FrameToken;
                if (ft != null)
                {
                    // instantiate a new entry and attach a frame info object
                    FormatEntry feFrame = new FormatEntry();
                    feFrame.frameInfo = new FrameInfo();

                    // add the frame info
                    feFrame.frameInfo.firstLine        = ft.frameInfoDefinition.firstLine;
                    feFrame.frameInfo.leftIndentation  = ft.frameInfoDefinition.leftIndentation;
                    feFrame.frameInfo.rightIndentation = ft.frameInfoDefinition.rightIndentation;

                    // execute the list inside the frame
                    ExecuteFormatTokenList(level, so, ft.itemDefinition.formatTokenList, feFrame.formatValueList);

                    // add the frame computation results to the current format entry
                    fe.formatValueList.Add(feFrame);
                    continue;
                }
                #region CompoundPropertyToken
                CompoundPropertyToken cpt = t as CompoundPropertyToken;
                if (cpt != null)
                {
                    if (!EvaluateDisplayCondition(so, cpt.conditionToken))
                    {
                        // token not active, skip it
                        continue;
                    }

                    // get the property from the object
                    object val = null;

                    // if no expression was specified, just use the
                    // object itself
                    if (cpt.expression == null || string.IsNullOrEmpty(cpt.expression.expressionValue))
                    {
                        val = so;
                    }
                    else
                    {
                        PSPropertyExpression ex = _expressionFactory.CreateFromExpressionToken(cpt.expression, _loadingInfo);
                        List <PSPropertyExpressionResult> resultList = ex.GetValues(so);
                        if (resultList.Count > 0)
                        {
                            val = resultList[0].Result;
                            if (resultList[0].Exception != null)
                            {
                                _errorManager.LogPSPropertyExpressionFailedResult(resultList[0], so);
                            }
                        }
                    }

                    // if the token is has a formatting string, it's a leaf node,
                    // do the formatting and we will be done
                    if (cpt.control == null || cpt.control is FieldControlBody)
                    {
                        // Since it is a leaf node we just consider it an empty string and go
                        // on with formatting
                        if (val == null)
                        {
                            val = string.Empty;
                        }
                        FieldFormattingDirective fieldFormattingDirective = null;
                        StringFormatError        formatErrorObject        = null;
                        if (cpt.control != null)
                        {
                            fieldFormattingDirective = ((FieldControlBody)cpt.control).fieldFormattingDirective;
                            if (fieldFormattingDirective != null && _errorManager.DisplayFormatErrorString)
                            {
                                formatErrorObject = new StringFormatError();
                            }
                        }

                        IEnumerable         e   = PSObjectHelper.GetEnumerable(val);
                        FormatPropertyField fpf = new FormatPropertyField();
                        if (cpt.enumerateCollection && e != null)
                        {
                            foreach (object x in e)
                            {
                                if (x == null)
                                {
                                    // nothing to process
                                    continue;
                                }
                                fpf = new FormatPropertyField();

                                fpf.propertyValue = PSObjectHelper.FormatField(fieldFormattingDirective, x, _enumerationLimit, formatErrorObject, _expressionFactory);
                                fe.formatValueList.Add(fpf);
                            }
                        }
                        else
                        {
                            fpf = new FormatPropertyField();

                            fpf.propertyValue = PSObjectHelper.FormatField(fieldFormattingDirective, val, _enumerationLimit, formatErrorObject, _expressionFactory);
                            fe.formatValueList.Add(fpf);
                        }
                        if (formatErrorObject != null && formatErrorObject.exception != null)
                        {
                            _errorManager.LogStringFormatError(formatErrorObject);
                            fpf.propertyValue = _errorManager.FormatErrorString;
                        }
                    }
                    else
                    {
                        // An empty result that is not a leaf node should not be expanded
                        if (val == null)
                        {
                            continue;
                        }
                        IEnumerable e = PSObjectHelper.GetEnumerable(val);
                        if (cpt.enumerateCollection && e != null)
                        {
                            foreach (object x in e)
                            {
                                if (x == null)
                                {
                                    // nothing to process
                                    continue;
                                }

                                // proceed with the recursion
                                ExecuteFormatControl(level.NextLevel, cpt.control, PSObject.AsPSObject(x), fe.formatValueList);
                            }
                        }
                        else
                        {
                            // proceed with the recursion
                            ExecuteFormatControl(level.NextLevel, cpt.control, PSObjectHelper.AsPSObject(val), fe.formatValueList);
                        }
                    }
                }
                #endregion CompoundPropertyToken
            }
            #endregion foreach loop
        }
        protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex,
                    FieldFormattingDirective directive, out MshExpressionResult expressionResult)
        {
            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 retVal = PSObjectHelper.GetExpressionDisplayValue(so, enumerationLimit, ex,
                                directive, formatErrorObject, expressionFactory, out expressionResult);

            if (expressionResult != null)
            {
                // we obtained a result, check if there is an error
                if (expressionResult.Exception != null)
                {
                    _errorManager.LogMshExpressionFailedResult(expressionResult, so);
                    if (_errorManager.DisplayErrorStrings)
                    {
                        retVal = _errorManager.ErrorString;
                    }
                }
                else 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.DisplayErrorStrings)
                    {
                        retVal = _errorManager.FormatErrorString;
                    }
                }
            }
            return retVal;
        }
 protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex,
             FieldFormattingDirective directive)
 {
     MshExpressionResult resolvedExpression;
     return GetExpressionDisplayValue(so, enumerationLimit, ex, directive, out resolvedExpression);
 }
        internal override object Verify(object val,
                                        TerminatingErrorContext invocationContext,
                                        bool originalParameterWasHashTable)
        {
            if (!originalParameterWasHashTable)
            {
                // this should never happen
                throw PSTraceSource.NewInvalidOperationException();
            }

            string s = val as string;
            if (string.IsNullOrEmpty(s))
            {
                string msg = StringUtil.Format(FormatAndOut_MshParameter.EmptyFormatStringValueError,
                    this.KeyName
                    );

                ParameterProcessor.ThrowParameterBindingException(invocationContext, "FormatStringEmpty", msg);
            }

            // we expect a string and we build a field formatting directive
            FieldFormattingDirective directive = new FieldFormattingDirective();
            directive.formatString = s;
            return directive;
        }
Exemple #10
0
        /// <summary>
        /// helper to retrieve the value of an MshExpression and to format it
        /// </summary>
        /// <param name="so">shell object to process</param>
        /// <param name="enumerationLimit">limit on IEnumerable enumeration</param>
        /// <param name="ex">expression to use for retrieval</param>
        /// <param name="directive">format directive to use for formatting</param>
        /// <param name="formatErrorObject"></param>
        /// <param name="expressionFactory">expression factory to create MshExpression</param>
        /// <param name="result"> not null if an error condition arose</param>
        /// <returns>formatted string</returns>
        internal static string GetExpressionDisplayValue(
            PSObject so,
            int enumerationLimit,
            MshExpression ex,
            FieldFormattingDirective directive,
            StringFormatError formatErrorObject,
            MshExpressionFactory expressionFactory,
            out MshExpressionResult result)
        {
            result = null;
            List<MshExpressionResult> resList = ex.GetValues(so);

            if (resList.Count == 0)
            {
                return "";
            }

            result = resList[0];
            if (result.Exception != null)
            {
                return "";
            }
            return PSObjectHelper.FormatField(directive, result.Result, enumerationLimit, formatErrorObject, expressionFactory);
        }
Exemple #11
0
 /// <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 MshExpression</param>
 /// <returns>string representation</returns>
 internal static string FormatField(FieldFormattingDirective directive, object val, int enumerationLimit,
     StringFormatError formatErrorObject, MshExpressionFactory expressionFactory)
 {
     PSObject so = PSObjectHelper.AsPSObject(val);
     if (directive != null && !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, null);
         }
         catch (Exception e) // 2004/11/17-JonN This covers exceptions thrown in
                             // String.Format and PSObject.ToString().
                             // I think we can swallow these.
         {
             CommandProcessorBase.CheckForSevereException(e);
             // NOTE: we catch all the exceptions, since we do not know
             // what the underlying object access would throw
             if (formatErrorObject != null)
             {
                 formatErrorObject.sourceObject = so;
                 formatErrorObject.exception = e;
                 formatErrorObject.formatString = directive.formatString;
                 return "";
             }
         }
     }
     // 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);
 }
Exemple #12
0
 protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, FieldFormattingDirective directive, out MshExpressionResult expressionResult)
 {
     StringFormatError formatErrorObject = null;
     if (this.errorManager.DisplayFormatErrorString)
     {
         formatErrorObject = new StringFormatError();
     }
     string errorString = PSObjectHelper.GetExpressionDisplayValue(so, enumerationLimit, ex, directive, formatErrorObject, this.expressionFactory, out expressionResult);
     if (expressionResult != null)
     {
         if (expressionResult.Exception != null)
         {
             this.errorManager.LogMshExpressionFailedResult(expressionResult, so);
             if (this.errorManager.DisplayErrorStrings)
             {
                 errorString = this.errorManager.ErrorString;
             }
             return errorString;
         }
         if ((formatErrorObject != null) && (formatErrorObject.exception != null))
         {
             this.errorManager.LogStringFormatError(formatErrorObject);
             if (this.errorManager.DisplayErrorStrings)
             {
                 errorString = this.errorManager.FormatErrorString;
             }
         }
     }
     return errorString;
 }
 private void ExecuteFormatTokenList(TraversalInfo level, PSObject so, List <FormatToken> formatTokenList, List <FormatValue> formatValueList)
 {
     if (so == null)
     {
         throw PSTraceSource.NewArgumentNullException("so");
     }
     if (level.Level != level.MaxDepth)
     {
         FormatEntry item = new FormatEntry();
         formatValueList.Add(item);
         foreach (FormatToken token in formatTokenList)
         {
             TextToken tt = token as TextToken;
             if (tt != null)
             {
                 FormatTextField field = new FormatTextField {
                     text = this.db.displayResourceManagerCache.GetTextTokenString(tt)
                 };
                 item.formatValueList.Add(field);
             }
             else if (token is NewLineToken)
             {
                 item.formatValueList.Add(new FormatNewLine());
             }
             else
             {
                 FrameToken token3 = token as FrameToken;
                 if (token3 != null)
                 {
                     FormatEntry entry2 = new FormatEntry {
                         frameInfo = new FrameInfo()
                     };
                     entry2.frameInfo.firstLine        = token3.frameInfoDefinition.firstLine;
                     entry2.frameInfo.leftIndentation  = token3.frameInfoDefinition.leftIndentation;
                     entry2.frameInfo.rightIndentation = token3.frameInfoDefinition.rightIndentation;
                     this.ExecuteFormatTokenList(level, so, token3.itemDefinition.formatTokenList, entry2.formatValueList);
                     item.formatValueList.Add(entry2);
                 }
                 else
                 {
                     CompoundPropertyToken token4 = token as CompoundPropertyToken;
                     if ((token4 != null) && this.EvaluateDisplayCondition(so, token4.conditionToken))
                     {
                         object result = null;
                         if ((token4.expression == null) || string.IsNullOrEmpty(token4.expression.expressionValue))
                         {
                             result = so;
                         }
                         else
                         {
                             List <MshExpressionResult> values = this.expressionFactory.CreateFromExpressionToken(token4.expression, this.loadingInfo).GetValues(so);
                             if (values.Count > 0)
                             {
                                 result = values[0].Result;
                                 if (values[0].Exception != null)
                                 {
                                     this.errorManager.LogMshExpressionFailedResult(values[0], so);
                                 }
                             }
                         }
                         if ((token4.control == null) || (token4.control is FieldControlBody))
                         {
                             if (result == null)
                             {
                                 result = "";
                             }
                             FieldFormattingDirective fieldFormattingDirective = null;
                             StringFormatError        formatErrorObject        = null;
                             if (token4.control != null)
                             {
                                 fieldFormattingDirective = ((FieldControlBody)token4.control).fieldFormattingDirective;
                                 if ((fieldFormattingDirective != null) && this.errorManager.DisplayFormatErrorString)
                                 {
                                     formatErrorObject = new StringFormatError();
                                 }
                             }
                             IEnumerable         enumerable = PSObjectHelper.GetEnumerable(result);
                             FormatPropertyField field2     = new FormatPropertyField();
                             if (token4.enumerateCollection && (enumerable != null))
                             {
                                 foreach (object obj3 in enumerable)
                                 {
                                     if (obj3 != null)
                                     {
                                         field2 = new FormatPropertyField {
                                             propertyValue = PSObjectHelper.FormatField(fieldFormattingDirective, obj3, this.enumerationLimit, formatErrorObject, this.expressionFactory)
                                         };
                                         item.formatValueList.Add(field2);
                                     }
                                 }
                             }
                             else
                             {
                                 field2 = new FormatPropertyField {
                                     propertyValue = PSObjectHelper.FormatField(fieldFormattingDirective, result, this.enumerationLimit, formatErrorObject, this.expressionFactory)
                                 };
                                 item.formatValueList.Add(field2);
                             }
                             if ((formatErrorObject != null) && (formatErrorObject.exception != null))
                             {
                                 this.errorManager.LogStringFormatError(formatErrorObject);
                                 field2.propertyValue = this.errorManager.FormatErrorString;
                             }
                         }
                         else if (result != null)
                         {
                             IEnumerable enumerable2 = PSObjectHelper.GetEnumerable(result);
                             if (token4.enumerateCollection && (enumerable2 != null))
                             {
                                 foreach (object obj4 in enumerable2)
                                 {
                                     if (obj4 != null)
                                     {
                                         this.ExecuteFormatControl(level.NextLevel, token4.control, PSObject.AsPSObject(obj4), item.formatValueList);
                                     }
                                 }
                             }
                             else
                             {
                                 this.ExecuteFormatControl(level.NextLevel, token4.control, PSObjectHelper.AsPSObject(result), item.formatValueList);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #14
0
        protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, FieldFormattingDirective directive, out MshExpressionResult expressionResult)
        {
            StringFormatError formatErrorObject = null;

            if (this.errorManager.DisplayFormatErrorString)
            {
                formatErrorObject = new StringFormatError();
            }
            string errorString = PSObjectHelper.GetExpressionDisplayValue(so, enumerationLimit, ex, directive, formatErrorObject, this.expressionFactory, out expressionResult);

            if (expressionResult != null)
            {
                if (expressionResult.Exception != null)
                {
                    this.errorManager.LogMshExpressionFailedResult(expressionResult, so);
                    if (this.errorManager.DisplayErrorStrings)
                    {
                        errorString = this.errorManager.ErrorString;
                    }
                    return(errorString);
                }
                if ((formatErrorObject != null) && (formatErrorObject.exception != null))
                {
                    this.errorManager.LogStringFormatError(formatErrorObject);
                    if (this.errorManager.DisplayErrorStrings)
                    {
                        errorString = this.errorManager.FormatErrorString;
                    }
                }
            }
            return(errorString);
        }
Exemple #15
0
        protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, FieldFormattingDirective directive)
        {
            MshExpressionResult result;

            return(this.GetExpressionDisplayValue(so, enumerationLimit, ex, directive, out result));
        }
Exemple #16
0
        internal static string GetExpressionDisplayValue(PSObject so, int enumerationLimit, MshExpression ex, FieldFormattingDirective directive, StringFormatError formatErrorObject, MshExpressionFactory expressionFactory, out MshExpressionResult result)
        {
            result = null;
            List <MshExpressionResult> values = ex.GetValues(so);

            if (values.Count == 0)
            {
                return("");
            }
            result = values[0];
            if (result.Exception != null)
            {
                return("");
            }
            return(FormatField(directive, result.Result, enumerationLimit, formatErrorObject, expressionFactory));
        }