Ejemplo n.º 1
0
        /// <summary>
        /// execution entry point override
        /// we assume that a LineOutput interface instance already has been acquired
        ///
        /// IMPORTANT: it assumes the presence of a pre-processing formatting command
        /// </summary>
        internal override void ProcessRecord()
        {
            PSObject so = this.ReadObject();

            if (so == null || so == AutomationNull.Value)
            {
                return;
            }

            // try to process the object
            if (ProcessObject(so))
            {
                return;
            }

            // send to the formatter for preprocessing
            Array results = ApplyFormatting(so);

            if (results != null)
            {
                foreach (object r in results)
                {
                    PSObject obj = PSObjectHelper.AsPSObject(r);

                    obj.IsHelpObject = so.IsHelpObject;

                    ProcessObject(obj);
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        internal override void EndProcessing()
        {
            base.EndProcessing();
            if (_command != null)
            {
                // shut down the command processor, if we ever used it
                Array results = _command.ShutDown();

                if (results != null)
                {
                    foreach (object o in results)
                    {
                        ProcessObject(PSObjectHelper.AsPSObject(o));
                    }
                }
            }

            if (this.LineOutput.RequiresBuffering)
            {
                // we need to notify the interface implementor that
                // we are about to do the playback
                LineOutput.DoPlayBackCall playBackCall = new LineOutput.DoPlayBackCall(this.DrainCache);

                this.LineOutput.ExecuteBufferPlayBack(playBackCall);
            }
            else
            {
                // we drain the cache ourselves
                DrainCache();
            }
        }
Ejemplo n.º 4
0
        private static string GetSmartToStringDisplayName(
            object x,
            MshExpressionFactory expressionFactory)
        {
            MshExpressionResult displayName = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);

            return(displayName != null && displayName.Exception == null?PSObjectHelper.AsPSObject(displayName.Result).ToString() : PSObjectHelper.AsPSObject(x).ToString());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// display a leaf value
        /// </summary>
        /// <param name="val">object to display</param>
        /// <param name="formatValueList">list of format tokens to add to</param>
        private void DisplayLeaf(object val, List <FormatValue> formatValueList)
        {
            FormatPropertyField fpf = new FormatPropertyField();

            fpf.propertyValue = PSObjectHelper.FormatField(null, PSObjectHelper.AsPSObject(val), _enumerationLimit, null, _expressionFactory);
            formatValueList.Add(fpf);
            formatValueList.Add(new FormatNewLine());
        }
Ejemplo n.º 6
0
        private void DisplayLeaf(object val, List <FormatValue> formatValueList)
        {
            FormatPropertyField item = new FormatPropertyField {
                propertyValue = PSObjectHelper.FormatField(null, PSObjectHelper.AsPSObject(val), this.enumerationLimit, null, this.expressionFactory)
            };

            formatValueList.Add(item);
            formatValueList.Add(new FormatNewLine());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Execution entry point.
        /// </summary>
        internal override void ProcessRecord()
        {
            _typeInfoDataBase = this.OuterCmdlet().Context.FormatDBManager.GetTypeInfoDataBase();

            PSObject so = this.ReadObject();

            if (so is null || so == AutomationNull.Value)
            {
                return;
            }

            IEnumerable e = PSObjectHelper.GetEnumerable(so);

            if (e is null)
            {
                ProcessObject(so);
                return;
            }

            // we have an IEnumerable, we have to decide if to expand, if at all
            EnumerableExpansion expansionState = this.GetExpansionState(so);

            switch (expansionState)
            {
            case EnumerableExpansion.EnumOnly:
            {
                foreach (object obj in e)
                {
                    ProcessObject(PSObjectHelper.AsPSObject(obj));
                }
            }

            break;

            case EnumerableExpansion.Both:
            {
                var objs = e.Cast <object>().ToArray();

                ProcessCoreOutOfBand(so, objs.Length);
                foreach (object obj in objs)
                {
                    ProcessObject(PSObjectHelper.AsPSObject(obj));
                }
            }

            break;

            default:
            {
                // do not enumerate at all (CoreOnly)
                ProcessObject(so);
            }

            break;
            }
        }
Ejemplo n.º 8
0
 private static void ReadListHelper(IEnumerable en, List <T> lst, FormatObjectDeserializer deserializer)
 {
     deserializer.VerifyDataNotNull(en, "enumerable");
     foreach (object obj2 in en)
     {
         T local = deserializer.DeserializeObject(PSObjectHelper.AsPSObject(obj2)) as T;
         deserializer.VerifyDataNotNull(local, "entry");
         lst.Add(local);
     }
 }
Ejemplo n.º 9
0
 private void WriteErrorRecords(List <ErrorRecord> errorRecordList)
 {
     if (errorRecordList != null)
     {
         foreach (ErrorRecord record in errorRecordList)
         {
             this.ProcessOutOfBand(PSObjectHelper.AsPSObject(record), true);
         }
     }
 }
Ejemplo n.º 10
0
 private static void ReadListHelper(IEnumerable en, List <T> lst, FormatObjectDeserializer deserializer)
 {
     deserializer.VerifyDataNotNull(en, "enumerable");
     foreach (object obj in en)
     {
         FormatInfoData fid = deserializer.DeserializeObject(PSObjectHelper.AsPSObject(obj));
         T entry            = fid as T;
         deserializer.VerifyDataNotNull(entry, "entry");
         lst.Add(entry);
     }
 }
Ejemplo n.º 11
0
        private static string GetSmartToStringDisplayName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);

            if ((r != null) && (r.Exception == null))
            {
                return(PSObjectHelper.AsPSObject(r.Result).ToString());
            }
            else
            {
                return(PSObjectHelper.AsPSObject(x).ToString());
            }
        }
Ejemplo n.º 12
0
        private static string GetObjectName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            string objName;

            // check if the underlying object is of primitive type
            // if so just return its value
            if (x is PSObject &&
                (LanguagePrimitives.IsBoolOrSwitchParameterType((((PSObject)x).BaseObject).GetType()) ||
                 LanguagePrimitives.IsNumeric(((((PSObject)x).BaseObject).GetType()).GetTypeCode()) ||
                 LanguagePrimitives.IsNull(x)))
            {
                objName = x.ToString();
            }
            else if (x == null)
            {
                // use PowerShell's $null variable to indicate that the value is null...
                objName = "$null";
            }
            else
            {
                MethodInfo toStringMethod = x.GetType().GetMethod("ToString", PSTypeExtensions.EmptyTypes);
                // TODO:CORECLR double check with CORE CLR that x.GetType() == toStringMethod.ReflectedType
                // Check if the given object "x" implements "toString" method. Do that by comparing "DeclaringType" which 'Gets the class that declares this member' and the object type
                if (toStringMethod.DeclaringType == x.GetType())
                {
                    objName = PSObjectHelper.AsPSObject(x).ToString();
                }
                else
                {
                    PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);
                    if ((r != null) && (r.Exception == null))
                    {
                        objName = PSObjectHelper.AsPSObject(r.Result).ToString();;
                    }
                    else
                    {
                        objName = PSObjectHelper.AsPSObject(x).ToString();
                        if (objName == string.Empty)
                        {
                            var baseObj = PSObject.Base(x);
                            if (baseObj != null)
                            {
                                objName = baseObj.ToString();
                            }
                        }
                    }
                }
            }

            return(objName);
        }
Ejemplo n.º 13
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 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));
        }
Ejemplo n.º 14
0
        internal override void ProcessRecord()
        {
            this._typeInfoDataBase = this.OuterCmdlet().Context.FormatDBManager.GetTypeInfoDataBase();
            PSObject obj2 = this.ReadObject();

            if ((obj2 != null) && (obj2 != AutomationNull.Value))
            {
                IEnumerable enumerable = PSObjectHelper.GetEnumerable(obj2);
                if (enumerable == null)
                {
                    this.ProcessObject(obj2);
                }
                else
                {
                    switch (this.GetExpansionState(obj2))
                    {
                    case EnumerableExpansion.EnumOnly:
                        foreach (object obj3 in enumerable)
                        {
                            this.ProcessObject(PSObjectHelper.AsPSObject(obj3));
                        }
                        break;

                    case EnumerableExpansion.Both:
                    {
                        int         count       = 0;
                        IEnumerator enumerator2 = enumerable.GetEnumerator();
                        {
                            while (enumerator2.MoveNext())
                            {
                                object current = enumerator2.Current;
                                count++;
                            }
                        }
                        this.ProcessCoreOutOfBand(obj2, count);
                        foreach (object obj4 in enumerable)
                        {
                            this.ProcessObject(PSObjectHelper.AsPSObject(obj4));
                        }
                        break;
                    }

                    default:
                        this.ProcessObject(obj2);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 15
0
        private void WriteErrorRecords(List <ErrorRecord> errorRecordList)
        {
            if (errorRecordList == null)
            {
                return;
            }

            // NOTE: for the time being we directly process error records.
            // This is should change if we hook up error pipelines; for the
            // time being, this achieves partial results.
            //
            // see NTRAID#Windows OS Bug-932722-2004/10/21-kevinloo ("Output: SS: Swallowing exceptions")
            foreach (ErrorRecord errorRecord in errorRecordList)
            {
                // we are recursing on formatting errors: isProcessingError == true
                ProcessOutOfBand(PSObjectHelper.AsPSObject(errorRecord), true);
            }
        }
Ejemplo n.º 16
0
        internal override void ProcessRecord()
        {
            PSObject so = this.ReadObject();

            if (((so != null) && (so != AutomationNull.Value)) && !this.ProcessObject(so))
            {
                Array array = this.ApplyFormatting(so);
                if (array != null)
                {
                    foreach (object obj3 in array)
                    {
                        PSObject obj4 = PSObjectHelper.AsPSObject(obj3);
                        obj4.IsHelpObject = so.IsHelpObject;
                        this.ProcessObject(obj4);
                    }
                }
            }
        }
Ejemplo n.º 17
0
        private void DisplayEnumerationInner(IEnumerable e, TraversalInfo level, List <FormatValue> formatValueList)
        {
            int enumCount = 0;

            foreach (object x in e)
            {
                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                {
                    throw new PipelineStoppedException();
                }

                if (_enumerationLimit >= 0)
                {
                    if (_enumerationLimit == enumCount)
                    {
                        DisplayLeaf(PSObjectHelper.Ellipsis, formatValueList);
                        break;
                    }

                    enumCount++;
                }

                if (TreatAsLeafNode(x, level))
                {
                    DisplayLeaf(x, formatValueList);
                }
                else
                {
                    // check if the top level object is an enumeration
                    IEnumerable e1 = PSObjectHelper.GetEnumerable(x);

                    if (e1 != null)
                    {
                        formatValueList.Add(new FormatNewLine());
                        DisplayEnumeration(e1, level.NextLevel, AddIndentationLevel(formatValueList));
                    }
                    else
                    {
                        DisplayObject(PSObjectHelper.AsPSObject(x), level.NextLevel, null, formatValueList);
                    }
                }
            }
        }
Ejemplo n.º 18
0
        private void DisplayEnumerationInner(IEnumerable e, TraversalInfo level, List <FormatValue> formatValueList)
        {
            int num = 0;

            foreach (object obj2 in e)
            {
                if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                {
                    throw new PipelineStoppedException();
                }
                if (this.enumerationLimit >= 0)
                {
                    if (this.enumerationLimit == num)
                    {
                        this.DisplayLeaf("...", formatValueList);
                        break;
                    }
                    num++;
                }
                if (TreatAsLeafNode(obj2, level))
                {
                    this.DisplayLeaf(obj2, formatValueList);
                }
                else
                {
                    IEnumerable enumerable = PSObjectHelper.GetEnumerable(obj2);
                    if (enumerable != null)
                    {
                        formatValueList.Add(new FormatNewLine());
                        this.DisplayEnumeration(enumerable, level.NextLevel, this.AddIndentationLevel(formatValueList));
                    }
                    else
                    {
                        this.DisplayObject(PSObjectHelper.AsPSObject(obj2), level.NextLevel, null, formatValueList);
                    }
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// handler for processing each object coming through the pipeline
        /// it forwards the call to the pipeline manager object
        /// </summary>
        internal override void ProcessRecord()
        {
            PSObject so = this.ReadObject();

            if (so == null || so == AutomationNull.Value)
            {
                return;
            }

            // on demand initialization when the first pipeline
            // object is initialized
            if (_mgr == null)
            {
                _mgr = new SubPipelineManager();
                _mgr.Initialize(_lo, this.OuterCmdlet().Context);
            }

#if false
            // if the object supports IEnumerable,
            // unpack the object and process each member separately
            IEnumerable e = PSObjectHelper.GetEnumerable(so);

            if (e == null)
            {
                this.mgr.Process(so);
            }
            else
            {
                foreach (object obj in e)
                {
                    this.mgr.Process(PSObjectHelper.AsPSObject(obj));
                }
            }
#else
            _mgr.Process(so);
#endif
        }
Ejemplo n.º 20
0
        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));
        }
Ejemplo n.º 21
0
 internal override void EndProcessing()
 {
     base.EndProcessing();
     if (this.command != null)
     {
         Array array = this.command.ShutDown();
         if (array != null)
         {
             foreach (object obj2 in array)
             {
                 this.ProcessObject(PSObjectHelper.AsPSObject(obj2));
             }
         }
     }
     if (this.LineOutput.RequiresBuffering)
     {
         Microsoft.PowerShell.Commands.Internal.Format.LineOutput.DoPlayBackCall playback = new Microsoft.PowerShell.Commands.Internal.Format.LineOutput.DoPlayBackCall(this.DrainCache);
         this.LineOutput.ExecuteBufferPlayBack(playback);
     }
     else
     {
         this.DrainCache();
     }
 }
Ejemplo n.º 22
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
        }
Ejemplo n.º 23
0
 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);
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 24
0
        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);
        }
Ejemplo n.º 25
0
        private void SendCommentOutOfBand(string msg)
        {
            FormatEntryData fed = OutOfBandFormatViewManager.GenerateOutOfBandObjectAsToString(PSObjectHelper.AsPSObject(msg));

            if (fed != null)
            {
                this.WriteObject(fed);
            }
        }
Ejemplo n.º 26
0
 internal static string SmartToString(
     PSObject so,
     MshExpressionFactory expressionFactory,
     int enumerationLimit,
     StringFormatError formatErrorObject)
 {
     if (so == null)
     {
         return("");
     }
     try
     {
         IEnumerable enumerable = PSObjectHelper.GetEnumerable((object)so);
         if (enumerable == null)
         {
             return(so.ToString());
         }
         StringBuilder stringBuilder = new StringBuilder();
         stringBuilder.Append("{");
         bool flag = true;
         int  num  = 0;
         if (enumerable.GetEnumerator() != null)
         {
             foreach (object obj in enumerable)
             {
                 if (LocalPipeline.GetExecutionContextFromTLS().CurrentPipelineStopping)
                 {
                     throw new PipelineStoppedException();
                 }
                 if (enumerationLimit >= 0)
                 {
                     if (num == enumerationLimit)
                     {
                         stringBuilder.Append("...");
                         break;
                     }
                     ++num;
                 }
                 string str;
                 if (obj is PSObject && (LanguagePrimitives.IsBoolOrSwitchParameterType(((PSObject)obj).BaseObject.GetType()) || LanguagePrimitives.IsNumeric(Type.GetTypeCode(((PSObject)obj).BaseObject.GetType())) || LanguagePrimitives.IsNull(obj)))
                 {
                     str = obj.ToString();
                 }
                 else if (obj == null)
                 {
                     str = "$null";
                 }
                 else
                 {
                     MethodInfo method = obj.GetType().GetMethod("ToString", Type.EmptyTypes, (ParameterModifier[])null);
                     if (method.DeclaringType.Equals(method.ReflectedType))
                     {
                         str = PSObjectHelper.AsPSObject(obj).ToString();
                     }
                     else
                     {
                         MshExpressionResult displayName = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(obj), expressionFactory);
                         str = displayName == null || displayName.Exception != null?PSObjectHelper.AsPSObject(obj).ToString() : PSObjectHelper.AsPSObject(displayName.Result).ToString();
                     }
                 }
                 if (!flag)
                 {
                     stringBuilder.Append(", ");
                 }
                 stringBuilder.Append(str);
                 if (flag)
                 {
                     flag = false;
                 }
             }
         }
         stringBuilder.Append("}");
         return(stringBuilder.ToString());
     }
     catch (ExtendedTypeSystemException ex)
     {
         if (formatErrorObject != null)
         {
             formatErrorObject.sourceObject = (object)so;
             formatErrorObject.exception    = (Exception)ex;
         }
         return("");
     }
 }