public void testGetInstance_valueT()
        {
            var value = new LiteralPolicyExpression <int>(1234);

            value.Should().NotBeNull();
            value.GetPolicyValue().GetPolicyValue().Should().Be(1234);
            value.GetExpressionType().Should().Be(PolicyExpressionType.LITERAL);
        }
        public void testGetInstance_valueT()
        {
            var value = new LiteralPolicyExpression<int>(1234);

            value.Should().NotBeNull();
            value.GetPolicyValue().GetPolicyValue().Should().Be(1234);
            value.GetExpressionType().Should().Be(PolicyExpressionType.LITERAL);
        }
        public void TestSerialize_SimpleExpression_ValidateExpression()
        {
            LiteralPolicyExpression<int> expr =
                new LiteralPolicyExpression<int>(new PolicyValue<int>((int) KeyUsageBit.DataEncipherment));
            XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser(expr);

            KeyUsageExtensionField extensionField = new KeyUsageExtensionField(true);
            List<IPolicyExpression> operands = new List<IPolicyExpression>();
            operands.Add(expr);
            operands.Add(extensionField);

            IOperationPolicyExpression oper = new OperationPolicyExpression(
                PolicyOperator<int, int, bool>.EQUALS, operands);

           
            string xml = parser.Serialize(oper);
            Console.WriteLine(xml);
            
        }
        public void TestSerialize_SimpleExpression_ValidateExpression()
        {
            LiteralPolicyExpression <int> expr =
                new LiteralPolicyExpression <int>(new PolicyValue <int>((int)KeyUsageBit.DataEncipherment));
            XMLLexiconPolicyParser parser = new XMLLexiconPolicyParser(expr);

            KeyUsageExtensionField   extensionField = new KeyUsageExtensionField(true);
            List <IPolicyExpression> operands       = new List <IPolicyExpression>();

            operands.Add(expr);
            operands.Add(extensionField);

            IOperationPolicyExpression oper = new OperationPolicyExpression(
                PolicyOperator <int, int, bool> .EQUALS, operands);


            string xml = parser.Serialize(oper);

            Console.WriteLine(xml);
        }
        /**
         * Builds an aggregated {@link PolicyExpression} from a parsed list of tokens.
         * @param tokens Parsed list of tokens used to build the {@link PolicyExpression}.
         * @param level Used for keeping track of depth of operations.
         * @return A {@link PolicyExpression} built from the parsed list of tokens.
         * @throws PolicyParseException
         */
        protected IPolicyExpression BuildExpression(IEnumerator <TokenTypeAssociation> tokens, bool operandFrame)
        {
            if (!tokens.MoveNext())
            {
                return(null);
            }

            IList <IPolicyExpression> builtOperandExpressions = new List <IPolicyExpression>();

            do
            {
                TokenTypeAssociation assos = tokens.Current;
                switch (assos.GetTokenType())
                {
                case TokenType.START_LEVEL:
                {
                    IncrementLevel();
                    IPolicyExpression expression = BuildExpression(tokens);
                    if (operandFrame)
                    {
                        return(expression);
                    }

                    builtOperandExpressions.Add(expression);
                    break;
                }

                case TokenType.END_LEVEL:
                    if (GetLevel() == 0)
                    {
                        throw new PolicyGrammarException("To many \")\" tokens.  Delete this token");
                    }

                    if (builtOperandExpressions.Count == 0)
                    {
                        throw new PolicyGrammarException("Group must contain at least one expression.");
                    }

                    DecrementLevel();
                    return(builtOperandExpressions[0]);

                case TokenType.OPERATOR_BINARY_EXPRESSION:
                case TokenType.OPERATOR_UNARY_EXPRESSION:
                {
                    // regardless if this is a unary or binary expression, then next set of tokens should consist
                    // of a parameter to this operator
                    IPolicyExpression subExpression = BuildExpression(tokens, true);

                    int tokenHashCode = 0;

                    if (subExpression != null)
                    {
                        //TODO Refactor
                        if (assos.GetTokenType() == TokenType.OPERATOR_UNARY_EXPRESSION)
                        {
                            tokenHashCode = (assos.GetToken() + "_" + GetOperandType(subExpression)).GetHashCode();
                        }
                        if (assos.GetTokenType() == TokenType.OPERATOR_BINARY_EXPRESSION)
                        {
                            string leftOperandType  = GetOperandType(builtOperandExpressions.First());
                            string rightOperandType = GetOperandType(subExpression);
                            tokenHashCode = (assos.GetToken() + "_" + leftOperandType + "_" + rightOperandType).GetHashCode();
                        }
                    }
                    else     //(subExpression == null)
                    {
                        throw new PolicyGrammarException("Missing parameter.  Operator must be followed by an expression.");
                    }

                    builtOperandExpressions.Add(subExpression);



                    // get the operator for this token
                    OperatorBase operatorBase = PolicyOperator.FromToken(tokenHashCode);

                    // now add the parameters to the operator
                    if (builtOperandExpressions.Count == 1 && operatorBase is BinaryOperator)
                    {
                        throw new PolicyGrammarException("Missing parameter.  Binary operators require two parameters.");
                    }

                    IPolicyExpression operatorExpression = new OperationPolicyExpression(operatorBase, builtOperandExpressions);

                    if (operandFrame)
                    {
                        return(operatorExpression);
                    }

                    builtOperandExpressions = new List <IPolicyExpression>();
                    builtOperandExpressions.Add(operatorExpression);

                    break;
                }

                case TokenType.LITERAL_EXPRESSION:
                {
                    IPolicyExpression expression = new LiteralPolicyExpression <string>(new PolicyValue <string>(assos.GetToken()));
                    if (operandFrame)
                    {
                        return(expression);                             // exit this operand frame
                    }
                    builtOperandExpressions.Add(expression);
                    break;
                }

                case TokenType.CERTIFICATE_REFERENCE_EXPRESSION:
                {
                    IPolicyExpression expression = BuildCertificateReferenceField(assos.GetToken());

                    if (operandFrame)
                    {
                        return(expression);                             // exit this operand frame
                    }
                    builtOperandExpressions.Add(expression);
                    break;
                }
                }
            } while(tokens.MoveNext());

            if (builtOperandExpressions.Count > 1)
            {
                throw new PolicyGrammarException("Erroneous expression.");
            }

            return(builtOperandExpressions[0]);
        }
        /**
	     * Builds an aggregated {@link PolicyExpression} from a parsed list of tokens.
	     * @param tokens Parsed list of tokens used to build the {@link PolicyExpression}.
	     * @param level Used for keeping track of depth of operations.
	     * @return A {@link PolicyExpression} built from the parsed list of tokens.
	     * @throws PolicyParseException
	     */
	    protected IPolicyExpression BuildExpression(IEnumerator<TokenTypeAssociation> tokens, bool operandFrame)
	    {
	        if (!tokens.MoveNext())
	        {
	            return null;
	        }

		    IList<IPolicyExpression> builtOperandExpressions = new List<IPolicyExpression>();

	        do
	        {
			    TokenTypeAssociation assos = tokens.Current;
			    switch (assos.GetTokenType())
			    {
				    case TokenType.START_LEVEL:
				    {
					    IncrementLevel();
					    IPolicyExpression expression = BuildExpression(tokens);
					    if (operandFrame)
						    return expression;
					
					    builtOperandExpressions.Add(expression);
					    break;
				    }
				    case TokenType.END_LEVEL:
					    if (GetLevel() == 0)
						    throw new PolicyGrammarException("To many \")\" tokens.  Delete this token");
					
					    if (builtOperandExpressions.Count == 0)
						    throw new PolicyGrammarException("Group must contain at least one expression.");
					
					    DecrementLevel();
					    return builtOperandExpressions[0];
				    case TokenType.OPERATOR_BINARY_EXPRESSION:
                    case TokenType.OPERATOR_UNARY_EXPRESSION:
				    {
					    // regardless if this is a unary or binary expression, then next set of tokens should consist 
					    // of a parameter to this operator
					    IPolicyExpression subExpression = BuildExpression(tokens, true);

                        int tokenHashCode = 0;

				        if (subExpression != null)
				        {
                            //TODO Refactor
				            if (assos.GetTokenType() == TokenType.OPERATOR_UNARY_EXPRESSION)
				            {
                                tokenHashCode = (assos.GetToken() + "_" + GetOperandType(subExpression)).GetHashCode();
				            }
                            if (assos.GetTokenType() == TokenType.OPERATOR_BINARY_EXPRESSION)
                            {
                                string leftOperandType = GetOperandType(builtOperandExpressions.First());
                                string rightOperandType = GetOperandType(subExpression);
                                tokenHashCode = (assos.GetToken() + "_" + leftOperandType + "_" + rightOperandType).GetHashCode();
                            }

				        }
                        else //(subExpression == null)
				        {
                            throw new PolicyGrammarException("Missing parameter.  Operator must be followed by an expression.");
				        }

					    builtOperandExpressions.Add(subExpression);

				        

                        // get the operator for this token
                        OperatorBase operatorBase = PolicyOperator.FromToken(tokenHashCode);

					    // now add the parameters to the operator
                        if (builtOperandExpressions.Count == 1 && operatorBase is BinaryOperator)
						    throw new PolicyGrammarException("Missing parameter.  Binary operators require two parameters.");

                        IPolicyExpression operatorExpression = new OperationPolicyExpression(operatorBase, builtOperandExpressions);
					
					    if (operandFrame)
						    return operatorExpression;
					
					    builtOperandExpressions = new List<IPolicyExpression>();
					    builtOperandExpressions.Add(operatorExpression);
					
					    break;
				    }
				    case TokenType.LITERAL_EXPRESSION:
				    {
					    IPolicyExpression expression = new LiteralPolicyExpression<string>(new PolicyValue<string>(assos.GetToken()));
					    if (operandFrame)
						    return expression;  // exit this operand frame
					
					    builtOperandExpressions.Add(expression);
					    break;
				    }
				    case TokenType.CERTIFICATE_REFERENCE_EXPRESSION:
				    {
                        IPolicyExpression expression = BuildCertificateReferenceField(assos.GetToken());
					
					    if (operandFrame)
						    return expression;  // exit this operand frame

                        builtOperandExpressions.Add(expression);
					    break;
				    }
			    }
		    } while(tokens.MoveNext());
	
		    if (builtOperandExpressions.Count > 1)
			    throw new PolicyGrammarException("Erroneous expression.");
			
		    return builtOperandExpressions[0];
	    }