コード例 #1
0
 /// <summary>
 ///     Ctor.
 /// </summary>
 /// <param name="domPropertyGetter">getter</param>
 /// <param name="returnType">desired result type</param>
 public DOMConvertingGetter(
     DOMPropertyGetter domPropertyGetter,
     Type returnType)
 {
     getter = domPropertyGetter;
     parser = SimpleTypeParserFactory.GetParser(returnType);
 }
コード例 #2
0
 /// <summary>
 ///     Ctor.
 /// </summary>
 /// <param name="domPropertyGetter">getter</param>
 /// <param name="returnType">component type</param>
 public DOMConvertingArrayGetter(
     DOMPropertyGetter domPropertyGetter,
     Type returnType)
 {
     getter = domPropertyGetter;
     componentType = returnType;
     parser = SimpleTypeParserFactory.GetParser(returnType);
 }
コード例 #3
0
 public CodegenExpression UnderlyingGetCodegen(
     CodegenExpression underlyingExpression,
     CodegenMethodScope codegenMethodScope,
     CodegenClassScope codegenClassScope)
 {
     var parserMember = codegenClassScope.AddDefaultFieldUnshared(
         true,
         typeof(SimpleTypeParser),
         SimpleTypeParserFactory.CodegenSimpleParser(
             parser,
             codegenClassScope.NamespaceScope.InitMethod,
             GetType(),
             codegenClassScope));
     var inner = getter.UnderlyingGetCodegen(underlyingExpression, codegenMethodScope, codegenClassScope);
     return StaticMethod(GetType(), "GetParseTextValue", inner, parserMember);
 }
コード例 #4
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="propertyName">is the name of the event property for which this getter gets values</param>
        /// <param name="expressionText">is the property expression itself</param>
        /// <param name="xPathExpression">is a compile XPath expression</param>
        /// <param name="resultType">is the resulting type</param>
        /// <param name="optionalCastToType">if non-null then the return value of the xpath expression is cast to this value</param>
        /// <param name="fragmentFactory">for creating fragments, or null in none to be created</param>
        public XPathPropertyGetter(String propertyName,
                                   String expressionText,
                                   XPathExpression xPathExpression,
                                   XPathResultType resultType,
                                   Type optionalCastToType,
                                   FragmentFactory fragmentFactory)
        {
            _expression      = xPathExpression;
            _expressionText  = expressionText;
            _property        = propertyName;
            _resultType      = resultType;
            _fragmentFactory = fragmentFactory;

            if ((optionalCastToType != null) && (optionalCastToType.IsArray))
            {
                _isCastToArray = true;

                if (resultType != XPathResultType.NodeSet)
                {
                    throw new ArgumentException("Array cast-to types require XPathResultType.NodeSet as the XPath result type");
                }
                optionalCastToType = optionalCastToType.GetElementType();
            }
            else
            {
                _isCastToArray = false;
            }

            if (optionalCastToType != null)
            {
                _simpleTypeParser = SimpleTypeParserFactory.GetParser(optionalCastToType);
            }
            else
            {
                _simpleTypeParser = null;
            }
            if (optionalCastToType == typeof(XmlNode))
            {
                _optionalCastToType = null;
            }
            else
            {
                _optionalCastToType = optionalCastToType;
            }
        }
コード例 #5
0
ファイル: ExprCastNode.cs プロジェクト: valmac/nesper
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            if (ChildNodes.Count == 0 || ChildNodes.Count > 2)
            {
                throw new ExprValidationException("Cast function node must have one or two child expressions");
            }

            var valueEvaluator = ChildNodes[0].ExprEvaluator;
            var fromType       = valueEvaluator.ReturnType;

            // determine date format parameter
            var namedParams =
                ExprNodeUtility.GetNamedExpressionsHandleDups(ChildNodes);

            ExprNodeUtility.ValidateNamed(namedParams, new string[] { "dateformat" });
            var dateFormatParameter = namedParams.Get("dateformat");

            if (dateFormatParameter != null)
            {
                ExprNodeUtility.ValidateNamedExpectType(
                    dateFormatParameter, new Type[] { typeof(string) });
            }

            // identify target type
            // try the primitive names including "string"
            _targetType = TypeHelper.GetPrimitiveTypeForName(_classIdentifier.Trim()).GetBoxedType();

            SimpleTypeCaster     caster;
            bool                 numeric;
            CasterParserComputer casterParserComputer = null;

            var classIdentifierInvariant = _classIdentifier.Trim().ToLowerInvariant();

            if (dateFormatParameter != null)
            {
                if (fromType != typeof(string))
                {
                    throw new ExprValidationException(
                              string.Format("Use of the '{0}' named parameter requires a string-type input", dateFormatParameter.ParameterName));
                }

                if (_targetType == null)
                {
                    try
                    {
                        _targetType = TypeHelper.GetClassForName(
                            _classIdentifier.Trim(), validationContext.EngineImportService.GetClassForNameProvider());
                    }
                    catch (TypeLoadException)
                    {
                        // expected
                    }
                }

                // dynamic or static date format
                numeric = false;
                caster  = null;


                StringToDateTimeBaseComputer dateTimeComputer;

                if (_targetType == typeof(DateTime) ||
                    _targetType == typeof(DateTime?) ||
                    classIdentifierInvariant.Equals("date"))
                {
                    _targetType = typeof(DateTime);

                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.StaticDateFormat != null)
                    {
                        if (desc.Iso8601Format)
                        {
                            dateTimeComputer = new StringToDateTimeWStaticISOFormatComputer(validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseISOException;
                        }
                        else
                        {
                            dateTimeComputer = new StringToDateTimeWStaticFormatComputer(desc.StaticDateFormat);
                            dateTimeComputer.HandleParseException += HandleParseException;
                        }
                    }
                    else
                    {
                        dateTimeComputer = new StringToDateTimeWDynamicFormatComputer(desc.DynamicDateFormat);
                        dateTimeComputer.HandleParseException += HandleParseException;
                    }
                }
                else if (_targetType == typeof(DateTimeOffset) ||
                         _targetType == typeof(DateTimeOffset?) ||
                         classIdentifierInvariant.Equals("dto") ||
                         classIdentifierInvariant.Equals("datetimeoffset"))
                {
                    _targetType = typeof(DateTimeOffset);

                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.StaticDateFormat != null)
                    {
                        if (desc.Iso8601Format)
                        {
                            dateTimeComputer = new StringToDtoWStaticISOFormatComputer(validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseISOException;
                        }
                        else
                        {
                            dateTimeComputer = new StringToDtoWStaticFormatComputer(desc.StaticDateFormat, validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseException;
                        }
                    }
                    else
                    {
                        dateTimeComputer = new StringToDtoWDynamicFormatComputer(desc.DynamicDateFormat, validationContext.EngineImportService.TimeZone);
                        dateTimeComputer.HandleParseException += HandleParseException;
                    }
                }
                else if (_targetType == typeof(DateTimeEx) ||
                         classIdentifierInvariant.Equals("dtx") ||
                         classIdentifierInvariant.Equals("datetimeex") ||
                         classIdentifierInvariant.Equals("calendar"))
                {
                    _targetType = typeof(DateTimeEx);

                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.StaticDateFormat != null)
                    {
                        if (desc.Iso8601Format)
                        {
                            dateTimeComputer = new StringToDtxWStaticISOFormatComputer(validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseISOException;
                        }
                        else
                        {
                            dateTimeComputer = new StringToDtxWStaticFormatComputer(desc.StaticDateFormat, validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseException;
                        }
                    }
                    else
                    {
                        dateTimeComputer = new StringToDtxWDynamicFormatComputer(desc.DynamicDateFormat, validationContext.EngineImportService.TimeZone);
                        dateTimeComputer.HandleParseException += HandleParseException;
                    }
                }
                else if (_targetType == typeof(long) || _targetType == typeof(long?))
                {
                    _targetType = typeof(long);

                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.StaticDateFormat != null)
                    {
                        if (desc.Iso8601Format)
                        {
                            dateTimeComputer = new StringToDateTimeLongWStaticISOFormatComputer(validationContext.EngineImportService.TimeZone);
                            dateTimeComputer.HandleParseException += HandleParseISOException;
                        }
                        else
                        {
                            dateTimeComputer = new StringToDateTimeLongWStaticFormatComputer(desc.StaticDateFormat);
                            dateTimeComputer.HandleParseException += HandleParseException;
                        }
                    }
                    else
                    {
                        dateTimeComputer = new StringToDateTimeLongWDynamicFormatComputer(desc.DynamicDateFormat);
                        dateTimeComputer.HandleParseException += HandleParseException;
                    }
                }
                else
                {
                    throw new ExprValidationException(
                              "Use of the '" + dateFormatParameter.ParameterName +
                              "' named parameter requires a target type of long or datetime");
                }

                casterParserComputer = dateTimeComputer;
            }
            else if (_targetType != null)
            {
                _targetType = _targetType.GetBoxedType();
                caster      = SimpleTypeCasterFactory.GetCaster(fromType, _targetType);
                numeric     = _targetType.IsNumeric();
            }
            else if (String.Equals(classIdentifierInvariant, "bigint", StringComparison.InvariantCultureIgnoreCase) ||
                     String.Equals(classIdentifierInvariant, "biginteger", StringComparison.InvariantCultureIgnoreCase))
            {
                _targetType = typeof(BigInteger);
                caster      = SimpleTypeCasterFactory.GetCaster(fromType, _targetType);
                numeric     = true;
            }
            else if (classIdentifierInvariant.Equals("decimal".ToLowerInvariant()))
            {
                _targetType = typeof(decimal);
                caster      = SimpleTypeCasterFactory.GetCaster(fromType, _targetType);
                numeric     = true;
            }
            else
            {
                try
                {
                    _targetType = TypeHelper.GetClassForName(
                        _classIdentifier.Trim(), validationContext.EngineImportService.GetClassForNameProvider());
                }
                catch (TypeLoadException e)
                {
                    throw new ExprValidationException(
                              "Type as listed in cast function by name '" + _classIdentifier + "' cannot be loaded", e);
                }
                numeric = _targetType.IsNumeric();
                caster  = numeric
                    ? SimpleTypeCasterFactory.GetCaster(fromType, _targetType)
                    : SimpleTypeCasterFactory.GetCaster(_targetType);
            }

            // assign a computer unless already assigned
            if (casterParserComputer == null)
            {
                // to-string
                if (_targetType == typeof(string))
                {
                    casterParserComputer = new StringXFormComputer();
                }
                else if (fromType == typeof(string))
                {
                    // parse
                    var parser = SimpleTypeParserFactory.GetParser(_targetType.GetBoxedType());
                    casterParserComputer = new StringParserComputer(parser);
                }
                else if (numeric)
                {
                    // numeric cast with check
                    casterParserComputer = new NumericCasterComputer(caster);
                }
                else
                {
                    // non-numeric cast
                    casterParserComputer = new NonnumericCasterComputer(caster);
                }
            }

            // determine constant or not
            Object theConstant = null;

            if (ChildNodes[0].IsConstantResult)
            {
                _isConstant = casterParserComputer.IsConstantForConstInput;
                if (_isConstant)
                {
                    var evaluateParams = new EvaluateParams(null, true, validationContext.ExprEvaluatorContext);
                    var @in            = valueEvaluator.Evaluate(evaluateParams);
                    theConstant = @in == null ? null : casterParserComputer.Compute(@in, evaluateParams);
                }
            }

            // determine evaluator
            if (_isConstant)
            {
                _exprEvaluator = new ExprCastNodeConstEval(this, theConstant);
            }
            else
            {
                _exprEvaluator = new ExprCastNodeNonConstEval(this, valueEvaluator, casterParserComputer);
            }
            return(null);
        }
コード例 #6
0
ファイル: ExprCastNode.cs プロジェクト: Moj00/nesper
        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);
        }
コード例 #7
0
ファイル: DOMConvertingGetter.cs プロジェクト: ikvm/nesper
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="propertyExpression">property name</param>
 /// <param name="domPropertyGetter">getter</param>
 /// <param name="returnType">desired result type</param>
 public DOMConvertingGetter(String propertyExpression, DOMPropertyGetter domPropertyGetter, Type returnType)
 {
     _getter = domPropertyGetter;
     _parser = SimpleTypeParserFactory.GetParser(returnType);
 }
コード例 #8
0
        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 fromType = ChildNodes[0].Forge.EvaluationType;
            var classIdentifier = ClassIdentifierWArray.ClassIdentifier;
            var classIdentifierInvariant = classIdentifier.Trim();
            var arrayDimensions = ClassIdentifierWArray.ArrayDimensions;

            // Local function to match a class identifier
            bool MatchesClassIdentifier(string identifier)
            {
                return string.Equals(
                    classIdentifierInvariant,
                    identifier,
                    StringComparison.InvariantCultureIgnoreCase);
            }
            
            // determine date format parameter
            var namedParams =
                ExprNodeUtilityValidate.GetNamedExpressionsHandleDups(ChildNodes);
            ExprNodeUtilityValidate.ValidateNamed(namedParams, new[] {"dateformat"});
            var dateFormatParameter = namedParams.Get("dateformat");
            if (dateFormatParameter != null) {
                ExprNodeUtilityValidate.ValidateNamedExpectType(
                    dateFormatParameter,
                    new[] {
                        typeof(string), 
                        typeof(DateFormat), 
                        typeof(DateTimeFormat)
                    });
            }

            // identify target type
            // try the primitive names including "string"
            SimpleTypeCaster caster;
            var targetType = TypeHelper.GetPrimitiveTypeForName(classIdentifier.Trim());
            if (!ClassIdentifierWArray.IsArrayOfPrimitive) {
                targetType = targetType.GetBoxedType();
            }

            targetType = ApplyDimensions(targetType);

            bool numeric;
            CasterParserComputerForge casterParserComputerForge = null;
            if (dateFormatParameter != null) {
                if (fromType != typeof(string)) {
                    throw new ExprValidationException(
                        "Use of the '" +
                        dateFormatParameter.ParameterName +
                        "' named parameter requires a string-type input");
                }

                if (targetType == null) {
                    try {
                        targetType = TypeHelper.GetClassForName(
                            classIdentifier.Trim(),
                            validationContext.ImportService.ClassForNameProvider);
                        targetType = ApplyDimensions(targetType);
                    }
                    catch (TypeLoadException) {
                        // expected
                    }
                }

                // dynamic or static date format
                numeric = false;
                caster = null;

                if (targetType == typeof(DateTimeEx) ||
                    MatchesClassIdentifier("calendar") ||
                    MatchesClassIdentifier("dateTimeEx")) {
                    targetType = typeof(DateTimeEx);

                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.IsIso8601Format) {
                        casterParserComputerForge = new StringToDateTimeExIsoFormatComputer();
                    }
                    else if (desc.StaticDateFormat != null) {
                        casterParserComputerForge = new StringToDateTimExWStaticFormatComputer(
                            desc.StaticDateFormat,
                            TimeZoneInfo.Utc); // Note how code-generation does not use the default time zone
                    }
                    else {
                        casterParserComputerForge = new StringToDateTimeExWExprFormatComputer(
                            desc.DynamicDateFormat,
                            TimeZoneInfo.Utc);
                    }
                }
                else if (targetType == typeof(DateTimeOffset) ||
                         targetType == typeof(DateTimeOffset?) ||
                         MatchesClassIdentifier("dto") ||
                         MatchesClassIdentifier("datetimeoffset")) {
                    targetType = typeof(DateTimeOffset);
                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.IsIso8601Format) {
                        casterParserComputerForge = new StringToDateTimeOffsetIsoFormatComputer();
                    }
                    else if (desc.StaticDateFormat != null) {
                        casterParserComputerForge =
                            new StringToDateTimeOffsetWStaticFormatComputer(desc.StaticDateFormat);
                    }
                    else {
                        casterParserComputerForge =
                            new StringToDateTimeOffsetWExprFormatComputerForge(desc.DynamicDateFormat);
                    }
                }
                else if (targetType == typeof(DateTime) ||
                         targetType == typeof(DateTime?) ||
                         MatchesClassIdentifier("datetime")) {
                    targetType = typeof(DateTime);
                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.IsIso8601Format) {
                        casterParserComputerForge = new StringToDateTimeIsoFormatComputer();
                    }
                    else if (desc.StaticDateFormat != null) {
                        casterParserComputerForge =
                            new StringToDateTimeWStaticFormatComputer(desc.StaticDateFormat);
                    }
                    else {
                        casterParserComputerForge =
                            new StringToDateTimeWExprFormatComputerForge(desc.DynamicDateFormat);
                    }
                }
                else if (targetType == typeof(long) || targetType == typeof(long?)) {
                    targetType = typeof(long);
                    var desc = ValidateDateFormat(dateFormatParameter, validationContext);
                    if (desc.IsIso8601Format) {
                        casterParserComputerForge = new StringToLongWStaticISOFormatComputer();
                    }
                    else if (desc.StaticDateFormat != null) {
                        casterParserComputerForge = new StringToLongWStaticFormatComputer(desc.StaticDateFormat);
                    }
                    else {
                        casterParserComputerForge = new StringToLongWExprFormatComputerForge(desc.DynamicDateFormat);
                    }
                }
                else {
                    throw new ExprValidationException(
                        "Use of the '" +
                        dateFormatParameter.ParameterName +
                        "' named parameter requires a target type of long, DateTime, DateTimeOffset or DateEx");
                }
            }
            else if (targetType != null) {
                targetType = targetType.GetBoxedType();
                caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType);
                numeric = caster.IsNumericCast;
            }
            else if (MatchesClassIdentifier("bigint") || MatchesClassIdentifier("biginteger")) {
                targetType = typeof(BigInteger);
                targetType = ApplyDimensions(targetType);
                caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType);
                numeric = true;
            }
            else if (MatchesClassIdentifier("decimal")) {
                targetType = typeof(decimal);
                targetType = ApplyDimensions(targetType);
                caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType);
                numeric = true;
            }
            else {
                try {
                    targetType = TypeHelper.GetClassForName(
                        classIdentifier.Trim(),
                        validationContext.ImportService.ClassForNameProvider);
                }
                catch (TypeLoadException e) {
                    throw new ExprValidationException(
                        "Class as listed in cast function by name '" + classIdentifier + "' cannot be loaded",
                        e);
                }

                targetType = ApplyDimensions(targetType);
                numeric = targetType.IsNumeric();
                if (numeric) {
                    caster = SimpleTypeCasterFactory.GetCaster(fromType, targetType);
                }
                else {
                    caster = new SimpleTypeCasterAnyType(targetType);
                }
            }

            // assign a computer unless already assigned
            if (casterParserComputerForge == null) {
                // to-string
                if (targetType == typeof(string)) {
                    casterParserComputerForge = new StringXFormComputer();
                }
                else if (fromType == typeof(string) && targetType != typeof(char)) {
                    // parse
                    SimpleTypeParserSPI parser = SimpleTypeParserFactory.GetParser(targetType.GetBoxedType());
                    casterParserComputerForge = new StringParserComputer(parser);
                }
                else if (numeric) {
                    // numeric cast with check
                    casterParserComputerForge = new NumberCasterComputer(caster);
                }
                else {
                    // non-numeric cast
                    casterParserComputerForge = new NonnumericCasterComputer(caster);
                }
            }

            // determine constant or not
            object theConstant = null;
            var isConstant = false;
            if (ChildNodes[0].Forge.ForgeConstantType.IsCompileTimeConstant) {
                isConstant = casterParserComputerForge.IsConstantForConstInput;
                if (isConstant) {
                    var @in = ChildNodes[0].Forge.ExprEvaluator.Evaluate(null, true, null);
                    theConstant = @in == null
                        ? null
                        : casterParserComputerForge.EvaluatorComputer.Compute(@in, null, true, null);
                }
            }
            
            forge = new ExprCastNodeForge(this, casterParserComputerForge, targetType, isConstant, theConstant);
            return null;
        }
コード例 #9
0
ファイル: FileSourceCSV.cs プロジェクト: lanicon/nesper
        private static ParseMakePropertiesDesc SetupProperties(bool requireOneMatch,
                                                               string[] propertyNamesOffered,
                                                               EventType outputEventType,
                                                               StatementContext statementContext,
                                                               string dateFormat)
        {
            var writeables = EventTypeUtility.GetWriteableProperties(outputEventType, false, false);

            IList <int> indexesList = new List <int>();
            IList <SimpleTypeParser>            parserList    = new List <SimpleTypeParser>();
            IList <WriteablePropertyDescriptor> writablesList = new List <WriteablePropertyDescriptor>();

            for (var i = 0; i < propertyNamesOffered.Length; i++)
            {
                var  propertyName = propertyNamesOffered[i];
                Type propertyType;
                try {
                    propertyType = outputEventType.GetPropertyType(propertyName);
                }
                catch (PropertyAccessException ex) {
                    throw new EPException("Invalid property name '" + propertyName + "': " + ex.Message, ex);
                }

                if (propertyType == null)
                {
                    continue;
                }

                SimpleTypeParser parser;
                if (propertyType.IsDateTime() && !propertyType.IsInt64())
                {
                    var dateTimeFormat = dateFormat != null
                                                ? DateTimeFormat.For(dateFormat)
                                                : DateTimeFormat.ISO_DATE_TIME;

                    if (propertyType == typeof(DateTime?))
                    {
                        parser = new ProxySimpleTypeParser(
                            text => (dateTimeFormat.Parse(text)?.DateTime)?.DateTime);
                    }
                    else if (propertyType == typeof(DateTime))
                    {
                        parser = new ProxySimpleTypeParser(
                            text => dateTimeFormat.Parse(text).DateTime.DateTime);
                    }
                    else if (propertyType == typeof(DateTimeOffset?))
                    {
                        parser = new ProxySimpleTypeParser(
                            text => dateTimeFormat.Parse(text)?.DateTime);
                    }
                    else if (propertyType == typeof(DateTimeOffset))
                    {
                        parser = new ProxySimpleTypeParser(
                            text => dateTimeFormat.Parse(text).DateTime);
                    }
                    else
                    {
                        parser = new ProxySimpleTypeParser(
                            text => dateTimeFormat.Parse(text));
                    }
                }
                else
                {
                    parser = SimpleTypeParserFactory.GetParser(propertyType);
                }

                var writable = EventTypeUtility.FindWritable(propertyName, writeables);
                if (writable == null)
                {
                    continue;
                }

                indexesList.Add(i);
                parserList.Add(parser);
                writablesList.Add(writable);
            }

            if (indexesList.IsEmpty() && requireOneMatch)
            {
                throw new EPException(
                          "Failed to match any of the properties " +
                          CompatExtensions.RenderAny(propertyNamesOffered) +
                          " to the event type properties of event type '" +
                          outputEventType.Name +
                          "'");
            }

            var parsers   = parserList.ToArray();
            var writables = writablesList.ToArray();
            var indexes   = CollectionUtil.IntArray(indexesList);
            EventBeanManufacturer manufacturer;

            try {
                manufacturer = EventTypeUtility.GetManufacturer(
                    outputEventType,
                    writables,
                    statementContext.ImportServiceRuntime,
                    false,
                    statementContext.EventTypeAvroHandler)
                               .GetManufacturer(statementContext.EventBeanTypedEventFactory);
            }
            catch (EventBeanManufactureException e) {
                throw new EPException("Event type '" + outputEventType.Name + "' cannot be written to: " + e.Message, e);
            }

            return(new ParseMakePropertiesDesc(indexes, parsers, manufacturer));
        }