Ejemplo n.º 1
0
        private static int ComputeDefaultAlignment(PSObject so, MshExpression ex)
        {
            List <MshExpressionResult> rList = ex.GetValues(so);

            if ((rList.Count == 0) || (rList[0].Exception != null))
            {
                return(TextAlignment.Left);
            }

            object val = rList[0].Result;

            if (val == null)
            {
                return(TextAlignment.Left);
            }

            PSObject soVal     = PSObject.AsPSObject(val);
            var      typeNames = soVal.InternalTypeNames;

            if (string.Equals(PSObjectHelper.PSObjectIsOfExactType(typeNames),
                              "System.String", StringComparison.OrdinalIgnoreCase))
            {
                return(TextAlignment.Left);
            }

            if (DefaultScalarTypes.IsTypeInList(typeNames))
            {
                return(TextAlignment.Right);
            }

            return(TextAlignment.Left);
        }
Ejemplo n.º 2
0
 internal static bool Evaluate(PSObject obj, MshExpression ex, out MshExpressionResult expressionResult)
 {
     expressionResult = null;
     List<MshExpressionResult> res = ex.GetValues(obj);
     if (res.Count == 0)
         return false;
     if (res[0].Exception != null)
     {
         expressionResult = res[0];
         return false;
     }
     return LanguagePrimitives.IsTrue(res[0].Result);
 }
Ejemplo n.º 3
0
        internal static MshExpressionResult GetDisplayName(
            PSObject target,
            MshExpressionFactory expressionFactory)
        {
            MshExpression displayNameExpression = PSObjectHelper.GetDisplayNameExpression(target, expressionFactory);

            if (displayNameExpression == null)
            {
                return((MshExpressionResult)null);
            }
            List <MshExpressionResult> values = displayNameExpression.GetValues(target);

            return(values.Count == 0 || values[0].Exception != null ? (MshExpressionResult)null : values[0]);
        }
Ejemplo n.º 4
0
        internal static MshExpressionResult GetDisplayName(PSObject target, MshExpressionFactory expressionFactory)
        {
            MshExpression displayNameExpression = GetDisplayNameExpression(target, expressionFactory);

            if (displayNameExpression != null)
            {
                List <MshExpressionResult> values = displayNameExpression.GetValues(target);
                if ((values.Count != 0) && (values[0].Exception == null))
                {
                    return(values[0]);
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
        internal static bool Evaluate(PSObject obj, MshExpression ex, out MshExpressionResult expressionResult)
        {
            expressionResult = null;
            List <MshExpressionResult> res = ex.GetValues(obj);

            if (res.Count == 0)
            {
                return(false);
            }
            if (res[0].Exception != null)
            {
                expressionResult = res[0];
                return(false);
            }
            return(LanguagePrimitives.IsTrue(res[0].Result));
        }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// it gets the display name value
        /// </summary>
        /// <param name="target">shell object to process</param>
        /// <param name="expressionFactory">expression factory to create MshExpression</param>
        /// <returns>MshExpressionResult if successful; null otherwise</returns>
        internal static MshExpressionResult GetDisplayName(PSObject target, MshExpressionFactory expressionFactory)
        {
            // get the expression to evaluate
            MshExpression ex = GetDisplayNameExpression(target, expressionFactory);

            if (ex == null)
            {
                return(null);
            }

            // evaluate the expression
            List <MshExpressionResult> resList = ex.GetValues(target);

            if (resList.Count == 0 || resList[0].Exception != null)
            {
                // no results or the retrieval on the first one failed
                return(null);
            }
            // return something only if the first match was successful
            return(resList[0]);
        }
Ejemplo n.º 8
0
 private static int ComputeDefaultAlignment(PSObject so, MshExpression ex)
 {
     List<MshExpressionResult> values = ex.GetValues(so);
     if ((values.Count != 0) && (values[0].Exception == null))
     {
         object result = values[0].Result;
         if (result != null)
         {
             ConsolidatedString internalTypeNames = PSObject.AsPSObject(result).InternalTypeNames;
             if (string.Equals(PSObjectHelper.PSObjectIsOfExactType(internalTypeNames), "System.String", StringComparison.OrdinalIgnoreCase))
             {
                 return 1;
             }
             if (DefaultScalarTypes.IsTypeInList(internalTypeNames))
             {
                 return 3;
             }
         }
     }
     return 1;
 }
Ejemplo n.º 9
0
        private static int ComputeDefaultAlignment(PSObject so, MshExpression ex)
        {
            List <MshExpressionResult> values = ex.GetValues(so);

            if ((values.Count != 0) && (values[0].Exception == null))
            {
                object result = values[0].Result;
                if (result != null)
                {
                    ConsolidatedString internalTypeNames = PSObject.AsPSObject(result).InternalTypeNames;
                    if (string.Equals(PSObjectHelper.PSObjectIsOfExactType(internalTypeNames), "System.String", StringComparison.OrdinalIgnoreCase))
                    {
                        return(1);
                    }
                    if (DefaultScalarTypes.IsTypeInList(internalTypeNames))
                    {
                        return(3);
                    }
                }
            }
            return(1);
        }
        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
                    {
                        MshExpression ex = _expressionFactory.CreateFromExpressionToken(cpt.expression, _loadingInfo);
                        List <MshExpressionResult> resultList = ex.GetValues(so);
                        if (resultList.Count > 0)
                        {
                            val = resultList[0].Result;
                            if (resultList[0].Exception != null)
                            {
                                _errorManager.LogMshExpressionFailedResult(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
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        private static int ComputeDefaultAlignment(PSObject so, MshExpression ex)
        {
            List<MshExpressionResult> rList = ex.GetValues(so);

            if ((rList.Count == 0) || (rList[0].Exception != null))
                return TextAlignment.Left;

            object val = rList[0].Result;
            if (val == null)
                return TextAlignment.Left;

            PSObject soVal = PSObject.AsPSObject(val);
            var typeNames = soVal.InternalTypeNames;
            if (string.Equals(PSObjectHelper.PSObjectIsOfExactType(typeNames),
                                "System.String", StringComparison.OrdinalIgnoreCase))
                return TextAlignment.Left;

            if (DefaultScalarTypes.IsTypeInList(typeNames))
                return TextAlignment.Right;

            return TextAlignment.Left;
        }