Exemplo n.º 1
0
 internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoadingInfo loadingInfo)
 {
     if (!et.isScriptBlock)
     {
         return new MshExpression(et.expressionValue);
     }
     if (this._expressionCache != null)
     {
         MshExpression expression;
         if (this._expressionCache.TryGetValue(et, out expression))
         {
             return expression;
         }
     }
     else
     {
         this._expressionCache = new Dictionary<ExpressionToken, MshExpression>();
     }
     ScriptBlock scriptBlock = ScriptBlock.Create(et.expressionValue);
     scriptBlock.DebuggerStepThrough = true;
     if ((loadingInfo != null) && loadingInfo.isFullyTrusted)
     {
         scriptBlock.LanguageMode = 0;
     }
     MshExpression expression2 = new MshExpression(scriptBlock);
     this._expressionCache.Add(et, expression2);
     return expression2;
 }
Exemplo n.º 2
0
 private bool EvaluateDisplayCondition(PSObject so, ExpressionToken conditionToken)
 {
     MshExpressionResult result;
     if (conditionToken == null)
     {
         return true;
     }
     MshExpression ex = this.expressionFactory.CreateFromExpressionToken(conditionToken, this.loadingInfo);
     bool flag = DisplayCondition.Evaluate(so, ex, out result);
     if ((result != null) && (result.Exception != null))
     {
         this.errorManager.LogMshExpressionFailedResult(result, so);
     }
     return flag;
 }
Exemplo n.º 3
0
 private ExpressionToken LoadExpressionFromObjectModel(DisplayEntry displayEntry, int viewIndex, string typeName)
 {
     ExpressionToken token = new ExpressionToken();
     if (displayEntry.ValueType.Equals(DisplayEntryValueType.Property))
     {
         token.expressionValue = displayEntry.Value;
         return token;
     }
     if (displayEntry.ValueType.Equals(DisplayEntryValueType.ScriptBlock))
     {
         token.isScriptBlock = true;
         token.expressionValue = displayEntry.Value;
         try
         {
             base.expressionFactory.VerifyScriptBlockText(token.expressionValue);
         }
         catch (ParseException exception)
         {
             base.ReportErrorForLoadingFromObjectModel(StringUtil.Format(FormatAndOutXmlLoadingStrings.InvalidScriptBlockInFormattingData, new object[] { typeName, viewIndex, exception.Message }), typeName);
             return null;
         }
         catch (Exception)
         {
             throw;
         }
         return token;
     }
     PSTraceSource.NewInvalidOperationException();
     return null;
 }
Exemplo n.º 4
0
 internal bool ProcessExpressionDirectives(System.Xml.XmlNode containerNode, List<System.Xml.XmlNode> unprocessedNodes)
 {
     if (containerNode == null)
     {
         throw PSTraceSource.NewArgumentNullException("containerNode");
     }
     string mandatoryInnerText = null;
     Microsoft.PowerShell.Commands.Internal.Format.TextToken token = null;
     TypeInfoDataBaseLoader.ExpressionNodeMatch match = new TypeInfoDataBaseLoader.ExpressionNodeMatch(this._loader);
     bool flag = false;
     bool flag2 = false;
     bool flag3 = false;
     foreach (System.Xml.XmlNode node in containerNode.ChildNodes)
     {
         if (match.MatchNode(node))
         {
             if (flag2)
             {
                 this._loader.ProcessDuplicateNode(node);
                 return false;
             }
             flag2 = true;
             if (!match.ProcessNode(node))
             {
                 return false;
             }
         }
         else if (this._loader.MatchNodeName(node, "FormatString"))
         {
             if (flag)
             {
                 this._loader.ProcessDuplicateNode(node);
                 return false;
             }
             flag = true;
             mandatoryInnerText = this._loader.GetMandatoryInnerText(node);
             if (mandatoryInnerText == null)
             {
                 this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NoFormatString, this._loader.ComputeCurrentXPath(), this._loader.FilePath));
                 return false;
             }
         }
         else if (this._loader.MatchNodeNameWithAttributes(node, "Text"))
         {
             if (flag3)
             {
                 this._loader.ProcessDuplicateNode(node);
                 return false;
             }
             flag3 = true;
             token = this._loader.LoadText(node);
             if (token == null)
             {
                 this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.InvalidNode, new object[] { this._loader.ComputeCurrentXPath(), this._loader.FilePath, "Text" }));
                 return false;
             }
         }
         else
         {
             unprocessedNodes.Add(node);
         }
     }
     if (flag2)
     {
         if (flag3)
         {
             this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NodeWithExpression, new object[] { this._loader.ComputeCurrentXPath(), this._loader.FilePath, "Text" }));
             return false;
         }
         ExpressionToken token2 = match.GenerateExpressionToken();
         if (token2 == null)
         {
             return false;
         }
         if (!string.IsNullOrEmpty(mandatoryInnerText))
         {
             this._formatString = mandatoryInnerText;
         }
         this._expression = token2;
     }
     else
     {
         if (flag)
         {
             this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NodeWithoutExpression, new object[] { this._loader.ComputeCurrentXPath(), this._loader.FilePath, "FormatString" }));
             return false;
         }
         if (flag3)
         {
             this._textToken = token;
         }
     }
     return true;
 }
Exemplo n.º 5
0
 internal bool ProcessNode(System.Xml.XmlNode n)
 {
     if (this._loader.MatchNodeName(n, "PropertyName"))
     {
         if (this._token != null)
         {
             if (this._token.isScriptBlock)
             {
                 this._loader.ProcessDuplicateAlternateNode(n, "PropertyName", "ScriptBlock");
             }
             else
             {
                 this._loader.ProcessDuplicateNode(n);
             }
             return false;
         }
         this._token = new ExpressionToken();
         this._token.expressionValue = this._loader.GetMandatoryInnerText(n);
         if (this._token.expressionValue == null)
         {
             this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NoProperty, this._loader.ComputeCurrentXPath(), this._loader.FilePath));
             this._fatalError = true;
             return false;
         }
         return true;
     }
     if (this._loader.MatchNodeName(n, "ScriptBlock"))
     {
         if (this._token != null)
         {
             if (!this._token.isScriptBlock)
             {
                 this._loader.ProcessDuplicateAlternateNode(n, "PropertyName", "ScriptBlock");
             }
             else
             {
                 this._loader.ProcessDuplicateNode(n);
             }
             return false;
         }
         this._token = new ExpressionToken();
         this._token.isScriptBlock = true;
         this._token.expressionValue = this._loader.GetMandatoryInnerText(n);
         if (this._token.expressionValue == null)
         {
             this._loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NoScriptBlockText, this._loader.ComputeCurrentXPath(), this._loader.FilePath));
             this._fatalError = true;
             return false;
         }
         if (!this._loader.suppressValidation && !this._loader.VerifyScriptBlock(this._token.expressionValue))
         {
             this._fatalError = true;
             return false;
         }
         return true;
     }
     PSTraceSource.NewInvalidOperationException();
     return false;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Load the expression information from DisplayEntry
        /// </summary>
        /// <param name="displayEntry"></param>
        /// <param name="viewIndex"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        private ExpressionToken LoadExpressionFromObjectModel(DisplayEntry displayEntry, int viewIndex, string typeName)
        {
            ExpressionToken token = new ExpressionToken();
            if (displayEntry.ValueType == DisplayEntryValueType.Property)
            {
                token.expressionValue = displayEntry.Value;
                return token;
            }
            else if (displayEntry.ValueType == DisplayEntryValueType.ScriptBlock)
            {
                token.isScriptBlock = true;
                token.expressionValue = displayEntry.Value;

                try
                {
                    // For faster startup, we don't validate any of the built-in formatting script blocks, where isFullyTrusted == built-in.
                    if (!LoadingInfo.isFullyTrusted)
                    {
                        this.expressionFactory.VerifyScriptBlockText(token.expressionValue);
                    }
                }
                catch (ParseException e)
                {
                    // Error at 
                    this.ReportErrorForLoadingFromObjectModel(
                        StringUtil.Format(FormatAndOutXmlLoadingStrings.InvalidScriptBlockInFormattingData, typeName, viewIndex, e.Message), typeName);
                    return null;
                }
                catch (Exception e) // will rethrow
                {
                    Diagnostics.Assert(false, "TypeInfoBaseLoader.VerifyScriptBlock unexpected exception " + e.GetType().FullName);
                    throw;
                }

                return token;
            }
            // this should never happen if the API is used correctly
            PSTraceSource.NewInvalidOperationException();
            return null;
        }
Exemplo n.º 7
0
            internal bool ProcessNode(XmlNode n)
            {
                if (_loader.MatchNodeName(n, XmlTags.PropertyNameNode))
                {
                    if (_token != null)
                    {
                        if (_token.isScriptBlock)
                            _loader.ProcessDuplicateAlternateNode(n, XmlTags.PropertyNameNode, XmlTags.ScriptBlockNode);
                        else
                            _loader.ProcessDuplicateNode(n);
                        return false; // fatal error
                    }

                    _token = new ExpressionToken();
                    _token.expressionValue = _loader.GetMandatoryInnerText(n);
                    if (_token.expressionValue == null)
                    {
                        //Error at XPath {0} in file {1}: Missing property.
                        _loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NoProperty, _loader.ComputeCurrentXPath(), _loader.FilePath));
                        _fatalError = true;
                        return false; // fatal error
                    }
                    return true;
                }
                else if (_loader.MatchNodeName(n, XmlTags.ScriptBlockNode))
                {
                    if (_token != null)
                    {
                        if (!_token.isScriptBlock)
                            _loader.ProcessDuplicateAlternateNode(n, XmlTags.PropertyNameNode, XmlTags.ScriptBlockNode);
                        else
                            _loader.ProcessDuplicateNode(n);
                        return false; // fatal error
                    }

                    _token = new ExpressionToken();
                    _token.isScriptBlock = true;
                    _token.expressionValue = _loader.GetMandatoryInnerText(n);
                    if (_token.expressionValue == null)
                    {
                        //Error at XPath {0} in file {1}: Missing script block text.
                        _loader.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NoScriptBlockText, _loader.ComputeCurrentXPath(), _loader.FilePath));
                        _fatalError = true;
                        return false; // fatal error
                    }

                    if ((!_loader._suppressValidation) && (!_loader.VerifyScriptBlock(_token.expressionValue)))
                    {
                        _fatalError = true;
                        return false; // fatal error
                    }
                    return true;
                }
                // this should never happen if the API is used correctly
                PSTraceSource.NewInvalidOperationException();
                return false;
            }
Exemplo n.º 8
0
        protected bool EvaluateDisplayCondition(PSObject so, ExpressionToken conditionToken)
        {
            if (conditionToken == null)
                return true;

            MshExpression ex = this.expressionFactory.CreateFromExpressionToken(conditionToken, this.dataBaseInfo.view.loadingInfo);
            MshExpressionResult expressionResult;
            bool retVal = DisplayCondition.Evaluate(so, ex, out expressionResult);

            if (expressionResult != null && expressionResult.Exception != null)
            {
                _errorManager.LogMshExpressionFailedResult(expressionResult, so);
            }
            return retVal;
        }
Exemplo n.º 9
0
 internal MshExpression CreateFromExpressionToken(ExpressionToken et)
 {
     return this.CreateFromExpressionToken(et, null);
 }
Exemplo n.º 10
0
        /// <summary>
        /// create an expression from an expression token
        /// </summary>
        /// <param name="et">expression token to use</param>
        /// <param name="loadingInfo">The context from which the file was loaded</param>
        /// <returns>constructed expression</returns>
        /// <exception cref="ParseException"></exception>
        internal MshExpression CreateFromExpressionToken(ExpressionToken et, DatabaseLoadingInfo loadingInfo)
        {
            if (et.isScriptBlock)
            {
                // we cache script blocks from expression tokens
                if (_expressionCache != null)
                {
                    MshExpression value;
                    if (_expressionCache.TryGetValue(et, out value))
                    {
                        // got a hit on the cache, just return
                        return value;
                    }
                }
                else
                {
                    _expressionCache = new Dictionary<ExpressionToken, MshExpression>();
                }

                bool isFullyTrusted = false;
                bool isProductCode = false;
                if (loadingInfo != null)
                {
                    isFullyTrusted = loadingInfo.isFullyTrusted;
                    isProductCode = loadingInfo.isProductCode;
                }

                // no hit, we build one and we cache
                ScriptBlock sb = ScriptBlock.CreateDelayParsedScriptBlock(et.expressionValue, isProductCode: isProductCode);
                sb.DebuggerStepThrough = true;

                if (isFullyTrusted)
                {
                    sb.LanguageMode = PSLanguageMode.FullLanguage;
                }

                MshExpression ex = new MshExpression(sb);

                _expressionCache.Add(et, ex);

                return ex;
            }

            // we do not cache if it is just a property name
            return new MshExpression(et.expressionValue);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Create an expression from an expression token.
 /// </summary>
 /// <param name="et">Expression token to use.</param>
 /// <returns>Constructed expression.</returns>
 /// <exception cref="ParseException"></exception>
 internal PSPropertyExpression CreateFromExpressionToken(ExpressionToken et)
 {
     return(CreateFromExpressionToken(et, null));
 }
Exemplo n.º 12
0
        private bool LoadPropertyBaseHelper(XmlNode propertyBaseNode, PropertyTokenBase ptb, List <XmlNode> unprocessedNodes)
        {
            ExpressionNodeMatch expressionMatch = new ExpressionNodeMatch(this);

            bool expressionNodeFound             = false; // cardinality 0..1
            bool collectionNodeFound             = false; // cardinality 0..1
            bool itemSelectionConditionNodeFound = false; // cardinality 0..1

            ExpressionToken condition = null;

            foreach (XmlNode n in propertyBaseNode.ChildNodes)
            {
                if (expressionMatch.MatchNode(n))
                {
                    if (expressionNodeFound)
                    {
                        this.ProcessDuplicateNode(n);
                        return(false); // fatal error
                    }

                    expressionNodeFound = true;
                    if (!expressionMatch.ProcessNode(n))
                    {
                        return(false); // fatal error
                    }
                }
                else if (MatchNodeName(n, XmlTags.EnumerateCollectionNode))
                {
                    if (collectionNodeFound)
                    {
                        this.ProcessDuplicateNode(n);
                        return(false);
                    }

                    collectionNodeFound = true;
                    if (!ReadBooleanNode(n, out ptb.enumerateCollection))
                    {
                        return(false);
                    }
                }
                else if (MatchNodeName(n, XmlTags.ItemSelectionConditionNode))
                {
                    if (itemSelectionConditionNodeFound)
                    {
                        this.ProcessDuplicateNode(n);
                        return(false);
                    }

                    itemSelectionConditionNodeFound = true;
                    condition = LoadItemSelectionCondition(n);
                    if (condition is null)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!IsFilteredOutNode(n))
                    {
                        unprocessedNodes.Add(n);
                    }
                }
            }

            if (expressionNodeFound)
            {
                // we add only if we encountered one, since it's not mandatory
                ExpressionToken expression = expressionMatch.GenerateExpressionToken();
                if (expression is null)
                {
                    return(false); // fatal error
                }

                ptb.expression     = expression;
                ptb.conditionToken = condition;
            }

            return(true);
        }
Exemplo n.º 13
0
        private ListControlItemDefinition LoadListControlItemDefinition(XmlNode propertyEntryNode)
        {
            using (this.StackFrame(propertyEntryNode))
            {
                // process Mshexpression, format string and text token
                ViewEntryNodeMatch match            = new ViewEntryNodeMatch(this);
                List <XmlNode>     unprocessedNodes = new List <XmlNode>();
                if (!match.ProcessExpressionDirectives(propertyEntryNode, unprocessedNodes))
                {
                    return(null); // fatal error
                }

                // process the remaining nodes
                TextToken       labelToken     = null;
                ExpressionToken condition      = null;
                bool            labelNodeFound = false;                  // cardinality 0..1
                bool            itemSelectionConditionNodeFound = false; // cardinality 0..1

                foreach (XmlNode n in unprocessedNodes)
                {
                    if (MatchNodeName(n, XmlTags.ItemSelectionConditionNode))
                    {
                        if (itemSelectionConditionNodeFound)
                        {
                            this.ProcessDuplicateNode(n);
                            return(null); // fatal error
                        }

                        itemSelectionConditionNodeFound = true;
                        condition = LoadItemSelectionCondition(n);
                        if (condition is null)
                        {
                            return(null); // fatal error
                        }
                    }
                    else if (MatchNodeNameWithAttributes(n, XmlTags.LabelNode))
                    {
                        if (labelNodeFound)
                        {
                            this.ProcessDuplicateNode(n);
                            return(null); // fatal error
                        }

                        labelNodeFound = true;
                        labelToken     = LoadLabel(n);
                        if (labelToken is null)
                        {
                            return(null); // fatal error
                        }
                    }
                    else
                    {
                        this.ProcessUnknownNode(n);
                    }
                }

                // finally build the item to return
                ListControlItemDefinition lvid = new ListControlItemDefinition();

                // add the label
                lvid.label = labelToken;

                // add condition
                lvid.conditionToken = condition;

                // add either the text token or the PSPropertyExpression with optional format string
                if (match.TextToken != null)
                {
                    lvid.formatTokenList.Add(match.TextToken);
                }
                else
                {
                    FieldPropertyToken fpt = new FieldPropertyToken();
                    fpt.expression = match.Expression;
                    fpt.fieldFormattingDirective.formatString = match.FormatString;
                    lvid.formatTokenList.Add(fpt);
                }

                return(lvid);
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// create an expression from an expression token
 /// </summary>
 /// <param name="et">expression token to use</param>
 /// <returns>constructed expression</returns>
 /// <exception cref="ParseException"></exception>
 internal MshExpression CreateFromExpressionToken(ExpressionToken et)
 {
     return(CreateFromExpressionToken(et, null));
 }