public static IDictionary<string, ExprNamedParameterNode> GetNamedExpressionsHandleDups( IList<ExprNode> parameters) { IDictionary<string, ExprNamedParameterNode> nameds = null; foreach (ExprNode node in parameters) { if (node is ExprNamedParameterNode) { ExprNamedParameterNode named = (ExprNamedParameterNode) node; if (nameds == null) { nameds = new Dictionary<string, ExprNamedParameterNode>(); } string lowerCaseName = named.ParameterName.ToLowerInvariant(); if (nameds.ContainsKey(lowerCaseName)) { throw new ExprValidationException("Duplicate parameter '" + lowerCaseName + "'"); } nameds.Put(lowerCaseName, named); } } if (nameds == null) { return Collections.GetEmptyMap<string, ExprNamedParameterNode>(); } return nameds; }
public static bool ValidateNamedExpectType( ExprNamedParameterNode namedParameterNode, Type[] expectedTypes) { if (namedParameterNode.ChildNodes.Length != 1) { throw GetNamedValidationException(namedParameterNode.ParameterName, expectedTypes); } ExprNode childNode = namedParameterNode.ChildNodes[0]; Type returnType = childNode.Forge.EvaluationType.GetBoxedType(); bool found = false; foreach (Type expectedType in expectedTypes) { if (expectedType == typeof(TimePeriod) && childNode is ExprTimePeriod) { found = true; break; } if ((returnType == expectedType.GetBoxedType()) || (expectedType.IsAssignableFrom(returnType))) { found = true; break; } } if (found) { return namedParameterNode.ChildNodes[0].Forge.ForgeConstantType.IsCompileTimeConstant; } throw GetNamedValidationException(namedParameterNode.ParameterName, expectedTypes); }
private ExprCastNodeDateDesc ValidateDateFormat( ExprNamedParameterNode dateFormatParameter, ExprValidationContext validationContext) { var iso8601Format = false; var formatExpr = dateFormatParameter.ChildNodes[0]; var formatForge = formatExpr.Forge; var formatReturnType = formatExpr.Forge.EvaluationType; string staticFormatString = null; if (formatReturnType == typeof(string)) { if (formatExpr.Forge.ForgeConstantType.IsCompileTimeConstant) { staticFormatString = (string) formatForge.ExprEvaluator.Evaluate(null, true, null); if (staticFormatString.ToLowerInvariant().Trim().Equals("iso")) { iso8601Format = true; } else { try { DateTimeFormat.For(staticFormatString); } catch (EPException) { throw; } catch (Exception ex) { throw new ExprValidationException( "Invalid date format '" + staticFormatString + "' (as obtained from DateTimeFormatter.For): " + ex.Message, ex); } } } } else { if ((typeof(DateFormat) != formatReturnType) && (!typeof(DateFormat).IsAssignableFrom(formatReturnType)) && (typeof(DateTimeFormat) != formatReturnType) && (!typeof(DateTimeFormat).IsAssignableFrom(formatReturnType))) { throw GetFailedExpected(typeof(DateFormat), formatReturnType); } } return new ExprCastNodeDateDesc( iso8601Format, formatForge, staticFormatString, formatForge.ForgeConstantType.IsConstant); }
/// <summary> /// Validates the date format. /// </summary> /// <param name="dateFormatParameter">The date format parameter.</param> /// <param name="validationContext">The validation context.</param> /// <returns></returns> private ExprCastNodeDateDesc ValidateDateFormat( ExprNamedParameterNode dateFormatParameter, ExprValidationContext validationContext) { string staticDateFormat = null; ExprEvaluator dynamicDateFormat = null; var iso8601Format = false; if (!dateFormatParameter.ChildNodes[0].IsConstantResult) { dynamicDateFormat = dateFormatParameter.ChildNodes[0].ExprEvaluator; } else { staticDateFormat = (string)dateFormatParameter.ChildNodes[0].ExprEvaluator.Evaluate( new EvaluateParams(null, true, validationContext.ExprEvaluatorContext)); if (staticDateFormat.ToLowerInvariant().Trim().Equals("iso")) { iso8601Format = true; } else { try { DateTime dateTimeTemp; DateTime.TryParseExact("", staticDateFormat, null, DateTimeStyles.None, out dateTimeTemp); //new SimpleDateFormat(staticDateFormat); } catch (Exception ex) { throw new ExprValidationException( "Invalid date format '" + staticDateFormat + "': " + ex.Message, ex); } } } return(new ExprCastNodeDateDesc(staticDateFormat, dynamicDateFormat, iso8601Format)); }
public void SetObserverParameters(IList<ExprNode> parameters, MatchedEventConvertor convertor, ExprValidationContext validationContext) { Convertor = convertor; // obtains name parameters IDictionary<string, ExprNamedParameterNode> namedExpressions; try { namedExpressions = ExprNodeUtility.GetNamedExpressionsHandleDups(parameters); ExprNodeUtility.ValidateNamed(namedExpressions, NAMED_PARAMETERS); } catch (ExprValidationException e) { throw new ObserverParameterException(e.Message, e); } bool allConstantResult; ExprNamedParameterNode isoStringExpr = namedExpressions.Get(ISO_NAME); if (namedExpressions.Count == 1 && isoStringExpr != null) { try { allConstantResult = ExprNodeUtility.ValidateNamedExpectType( isoStringExpr, new Type[] { typeof (string) }); } catch (ExprValidationException ex) { throw new ObserverParameterException(ex.Message, ex); } ScheduleComputer = new TimerScheduleSpecComputeISOString(isoStringExpr.ChildNodes[0]); } else if (isoStringExpr != null) { throw new ObserverParameterException( "The '" + ISO_NAME + "' parameter is exclusive of other parameters"); } else if (namedExpressions.Count == 0) { throw new ObserverParameterException("No parameters provided"); } else { allConstantResult = true; ExprNamedParameterNode dateNamedNode = namedExpressions.Get(DATE_NAME); ExprNamedParameterNode repetitionsNamedNode = namedExpressions.Get(REPETITIONS_NAME); ExprNamedParameterNode periodNamedNode = namedExpressions.Get(PERIOD_NAME); if (dateNamedNode == null && periodNamedNode == null) { throw new ObserverParameterException("Either the date or period parameter is required"); } try { if (dateNamedNode != null) { allConstantResult = ExprNodeUtility.ValidateNamedExpectType( dateNamedNode, new Type[] { typeof (string), typeof (DateTime), typeof (DateTimeOffset), typeof (long?) }); } if (repetitionsNamedNode != null) { allConstantResult &= ExprNodeUtility.ValidateNamedExpectType( repetitionsNamedNode, new Type[] { typeof (int?), typeof (long?) }); } if (periodNamedNode != null) { allConstantResult &= ExprNodeUtility.ValidateNamedExpectType( periodNamedNode, new Type[] { typeof (TimePeriod) }); } } catch (ExprValidationException ex) { throw new ObserverParameterException(ex.Message, ex); } ExprNode dateNode = dateNamedNode == null ? null : dateNamedNode.ChildNodes[0]; ExprNode repetitionsNode = repetitionsNamedNode == null ? null : repetitionsNamedNode.ChildNodes[0]; ExprTimePeriod periodNode = periodNamedNode == null ? null : (ExprTimePeriod) periodNamedNode.ChildNodes[0]; ScheduleComputer = new TimerScheduleSpecComputeFromExpr(dateNode, repetitionsNode, periodNode); } if (allConstantResult) { try { Spec = ScheduleComputer.Compute( convertor, new MatchedEventMapImpl(convertor.MatchedEventMapMeta), null, validationContext.MethodResolutionService.EngineImportService.TimeZone); } catch (ScheduleParameterException ex) { throw new ObserverParameterException(ex.Message, ex); } } }
public override ExprNode Validate(ExprValidationContext validationContext) { if (ChildNodes.Length == 0 || ChildNodes.Length > 2) { throw new ExprValidationException("Cast function node must have one or two child expressions"); } var valueEvaluator = ChildNodes[0].ExprEvaluator; _sourceType = valueEvaluator.ReturnType; var typeIdentifier = _typeIdentifier.Trim(); // determine date format parameter var namedParams = ExprNodeUtility.GetNamedExpressionsHandleDups(ChildNodes); ExprNodeUtility.ValidateNamed(namedParams, new String[] { "dateformat" }); ExprNamedParameterNode dateFormatParameter = namedParams.Get("dateformat"); if (dateFormatParameter != null) { ExprNodeUtility.ValidateNamedExpectType(dateFormatParameter, typeof(string)); } // identify target type // try the primitive names including "string" _targetType = TypeHelper.GetPrimitiveTypeForName(typeIdentifier).GetBoxedType(); _isConstant = true; if (dateFormatParameter != null) { if (_sourceType != typeof(string)) { throw new ExprValidationException("Use of the '" + dateFormatParameter.ParameterName + "' named parameter requires a string-type input"); } if (_targetType == null) { try { _targetType = TypeHelper.GetTypeForSimpleName(typeIdentifier); } catch (TypeLoadException e) { // expected } } // dynamic or static date format String staticDateFormat = null; ExprEvaluator dynamicDateFormat = null; Boolean iso8601Format = false; if (!dateFormatParameter.ChildNodes[0].IsConstantResult) { dynamicDateFormat = dateFormatParameter.ChildNodes[0].ExprEvaluator; } else { staticDateFormat = (string)dateFormatParameter.ChildNodes[0].ExprEvaluator.Evaluate( new EvaluateParams(null, true, validationContext.ExprEvaluatorContext)); if (staticDateFormat.ToLower().Trim() == "iso") { iso8601Format = true; } else { try { DateTime dateTimeTemp; DateTime.TryParseExact("", staticDateFormat, null, DateTimeStyles.None, out dateTimeTemp); //new SimpleDateFormat(staticDateFormat); } catch (Exception ex) { throw new ExprValidationException( "Invalid date format '" + staticDateFormat + "': " + ex.Message, ex); } } } if (_targetType == typeof(DateTime?) || typeIdentifier.ToLower() == "date") { _targetType = typeof(DateTime?); if (staticDateFormat != null) { if (iso8601Format) { _typeCaster = StringToDateTimeWStaticISOFormatComputer(); } else { _typeCaster = StringToDateTimeWStaticFormatComputer(staticDateFormat, validationContext.EngineImportService.TimeZone); } } else { _typeCaster = StringToDateTimeWDynamicFormatComputer(dynamicDateFormat, validationContext.EngineImportService.TimeZone); _isConstant = false; } } else if (_targetType == typeof(long?)) { _targetType = typeof(long?); if (staticDateFormat != null) { if (iso8601Format) { _typeCaster = StringToLongWStaticISOFormatComputer(); } else { _typeCaster = StringToLongWStaticFormatComputer(staticDateFormat, validationContext.EngineImportService.TimeZone); } } else { _typeCaster = StringToLongWDynamicFormatComputer(dynamicDateFormat, validationContext.EngineImportService.TimeZone); _isConstant = false; } } else { throw new ExprValidationException("Use of the '" + dateFormatParameter.ParameterName + "' named parameter requires a target type of datetime or long"); } } else if (_targetType == null) { try { _targetType = TypeHelper.ResolveType(_typeIdentifier, true); } catch (Exception e) { throw new ExprValidationException( "Type as listed in cast function by name '" + _typeIdentifier + "' cannot be loaded", e); } } _sourceType = _sourceType.GetBoxedType(); _targetType = _targetType.GetBoxedType(); if (_typeCaster != null) { // no-op } else if (_sourceType == _targetType) { _typeCaster = (o, evaluateParams) => o; } else if (_targetType == typeof(string)) { _typeCaster = (o, evaluateParams) => Convert.ToString(o); } else if (_sourceType == typeof(string)) { var typeParser = SimpleTypeParserFactory.GetParser(_targetType); _typeCaster = (o, evaluateParams) => typeParser((string)o); } else { var typeCaster = CastHelper.GetTypeCaster(_sourceType, _targetType); _typeCaster = (o, evaluateParams) => typeCaster.Invoke(o); } // determine constant or not // - basically, if the terms are constant then the cast can be computed now and stored away // - for future use. if (_isConstant && ChildNodes[0].IsConstantResult) { var evaluateParams = new EvaluateParams(null, true, validationContext.ExprEvaluatorContext); var inputValue = valueEvaluator.Evaluate(evaluateParams); var constantValue = _typeCaster.Invoke(inputValue, evaluateParams); _exprEvaluator = new ExprCastNodeConstEval(this, constantValue); } else { _exprEvaluator = new ExprCastNodeNonConstEval(this, valueEvaluator, _typeCaster); } return(null); }