public void TestGetParser()
        {
            object[][] tests = new object[][] {
                new object[] { typeof(bool?), "TrUe", true },
                new object[] { typeof(bool?), "false", false },
                new object[] { typeof(bool), "false", false },
                new object[] { typeof(bool), "true", true },
                new object[] { typeof(int), "73737474 ", 73737474 },
                new object[] { typeof(int?), " -1 ", -1 },
                new object[] { typeof(long), "123456789001222L", 123456789001222L },
                new object[] { typeof(long?), " -2 ", -2L },
                new object[] { typeof(long?), " -2L ", -2L },
                new object[] { typeof(long?), " -2l ", -2L },
                new object[] { typeof(short?), " -3 ", (short)-3 },
                new object[] { typeof(short), "111", (short)111 },
                new object[] { typeof(double?), " -3d ", -3d },
                new object[] { typeof(double), "111.38373", 111.38373d },
                new object[] { typeof(double?), " -3.1D ", -3.1D },
                new object[] { typeof(float?), " -3f ", -3f },
                new object[] { typeof(float), "111.38373", 111.38373f },
                new object[] { typeof(float?), " -3.1F ", -3.1f },
                new object[] { typeof(sbyte), " -3 ", (sbyte)-3 },
                new object[] { typeof(byte), " 1 ", (byte)1 },
                new object[] { typeof(char), "ABC", 'A' },
                new object[] { typeof(char?), " AB", ' ' },
                new object[] { typeof(string), "AB", "AB" },
                new object[] { typeof(string), " AB ", " AB " },
            };

            for (int i = 0; i < tests.Length; i++)
            {
                SimpleTypeParser parser = SimpleTypeParserFactory.GetParser((Type)tests[i][0]);
                Assert.AreEqual(tests[i][2], parser.Parse((string)tests[i][1]), "error in row:" + i);
            }
        }
Exemplo n.º 2
0
        private static Object CastToArray(
            Object result,
            Type optionalCastToType,
            SimpleTypeParser simpleTypeParser,
            XPathExpression expression)
        {
            if (result is XPathNodeIterator)
            {
                return(CastToArray(
                           (XPathNodeIterator)result,
                           optionalCastToType,
                           simpleTypeParser,
                           expression));
            }

            if (result is XmlNodeList)
            {
                return(CastToArray(
                           (XmlNodeList)result,
                           optionalCastToType,
                           simpleTypeParser,
                           expression));
            }

            return(null);
        }
        private static object GetParseTextValue(XObject node, SimpleTypeParser parser)
        {
            if (node is XContainer container)
            {
                return(string.Concat(container.Nodes()));
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        ///     NOTE: Code-generation-invoked method, method name and parameter order matters
        /// </summary>
        /// <param name="node">node</param>
        /// <param name="parser">parser</param>
        /// <returns>value</returns>
        /// <throws>PropertyAccessException exception</throws>
        public static object GetParseTextValue(
            XmlNode node,
            SimpleTypeParser parser)
        {
            var text = node?.InnerText;
            if (text == null) {
                return null;
            }

            return parser.Parse(text);
        }
        private static object GetParseTextValue(XmlNode node, SimpleTypeParser parser)
        {
            var text = node?.InnerText;

            if (text == null)
            {
                return(null);
            }

            return(parser.Invoke(text));
        }
 /// <summary>
 ///     NOTE: Code-generation-invoked method, method name and parameter order matters
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="parser">The parser.</param>
 /// <returns></returns>
 public static object GetParseTextValue(object node, SimpleTypeParser parser)
 {
     if (node is XObject xobject)
     {
         return(GetParseTextValue(xobject, parser));
     }
     else if (node is XmlNode xmlnode)
     {
         return(GetParseTextValue(xmlnode, parser));
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 7
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;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///     NOTE: Code-generation-invoked method, method name and parameter order matters
        /// </summary>
        /// <param name="result">nodes</param>
        /// <param name="componentType">type</param>
        /// <param name="parser">parser</param>
        /// <returns>result</returns>
        public static object GetDOMArrayFromNodes(
            XmlNode[] result,
            Type componentType,
            SimpleTypeParser parser)
        {
            var array = Arrays.CreateInstanceChecked(componentType, result.Length);
            for (var i = 0; i < result.Length; i++) {
                var text = result[i].InnerText;
                if (text == null || text.Length == 0) {
                    continue;
                }

                var parseResult = parser.Parse(text);
                array.SetValue(parseResult, i);
            }

            return array;
        }
Exemplo n.º 9
0
        private static Object CastToArray(
            XPathNodeIterator nodeIterator,
            Type optionalCastToType,
            SimpleTypeParser simpleTypeParser,
            XPathExpression expression)
        {
            var itemList = new List <object>();

            while (nodeIterator.MoveNext())
            {
                var item = nodeIterator.Current;
                if (item != null)
                {
                    try
                    {
                        if ((item.NodeType == XPathNodeType.Attribute) ||
                            (item.NodeType == XPathNodeType.Element))
                        {
                            var textContent = item.InnerXml;
                            itemList.Add(simpleTypeParser.Invoke(textContent));
                        }
                    }
                    catch
                    {
                        if (Log.IsInfoEnabled)
                        {
                            Log.Info("Parse error for text content {0} for expression {1}", item.InnerXml, expression);
                        }
                    }
                }
            }

            var array = Array.CreateInstance(optionalCastToType, itemList.Count);

            for (int ii = 0; ii < itemList.Count; ii++)
            {
                array.SetValue(itemList[ii], ii);
            }

            return(array);
        }
Exemplo n.º 10
0
        private static Object CastToArray(
            XmlNodeList nodeList,
            Type optionalCastToType,
            SimpleTypeParser simpleTypeParser,
            XPathExpression expression)
        {
            var array = Array.CreateInstance(optionalCastToType, nodeList.Count);

            for (int i = 0; i < nodeList.Count; i++)
            {
                Object arrayItem = null;
                try
                {
                    XmlNode item = nodeList.Item(i);
                    String  textContent;
                    if ((item.NodeType == XmlNodeType.Attribute) ||
                        (item.NodeType == XmlNodeType.Element))
                    {
                        textContent = item.InnerText;
                    }
                    else
                    {
                        continue;
                    }

                    arrayItem = simpleTypeParser.Invoke(textContent);
                }
                catch
                {
                    if (Log.IsInfoEnabled)
                    {
                        Log.Info("Parse error for text content {0} for expression {1}", nodeList[i].InnerText, expression);
                    }
                }

                array.SetValue(arrayItem, i);
            }

            return(array);
        }
Exemplo n.º 11
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);
 }
Exemplo n.º 12
0
 public StringParserComputer(SimpleTypeParser parser)
 {
     _parser = parser;
 }
Exemplo n.º 13
0
        /// <summary>
        /// NOTE: Code-generation-invoked method, method name and parameter order matters
        /// </summary>
        /// <param name="navigator">The navigator.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="expressionText">The expression text.</param>
        /// <param name="property">The property.</param>
        /// <param name="optionalCastToType">Type of the optional cast to.</param>
        /// <param name="resultType">Type of the result.</param>
        /// <param name="isCastToArray">if set to <c>true</c> [is cast to array].</param>
        /// <param name="simpleTypeParser">The simple type parser.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// <exception cref="PropertyAccessException">Error getting property " + property</exception>
        public static Object EvaluateXPathGet(
            XPathNavigator navigator,
            XPathExpression expression,
            String expressionText,
            String property,
            Type optionalCastToType,
            XPathResultType resultType,
            bool isCastToArray,
            SimpleTypeParser simpleTypeParser)
        {
            try
            {
                var result = navigator.Evaluate(expression);
                if (result == null)
                {
                    return(null);
                }

                // if there is no parser, return xpath expression type
                if (optionalCastToType == null)
                {
                    var nodeIterator = result as XPathNodeIterator;
                    if (nodeIterator != null)
                    {
                        if (nodeIterator.Count == 0)
                        {
                            return(null);
                        }
                        if (nodeIterator.Count == 1)
                        {
                            nodeIterator.MoveNext();
                            switch (resultType)
                            {
                            case XPathResultType.Any:
                                return(((System.Xml.IHasXmlNode)nodeIterator.Current).GetNode());

                            case XPathResultType.String:
                                return(nodeIterator.Current.TypedValue);

                            case XPathResultType.Boolean:
                                return(nodeIterator.Current.ValueAsBoolean);

                            case XPathResultType.Number:
                                return(nodeIterator.Current.ValueAsDouble);

                            default:
                                return(nodeIterator.Current.TypedValue);
                            }
                        }
                        else
                        {
                            return(new XPathIteratorNodeList(nodeIterator));
                        }
                    }

                    return(result);
                }

                if (isCastToArray)
                {
                    return(CastToArray(result, optionalCastToType, simpleTypeParser, expression));
                }

                if (result is XPathNodeIterator)
                {
                    var nodeIterator = result as XPathNodeIterator;
                    if (nodeIterator.Count == 0)
                    {
                        return(null);
                    }
                    if (nodeIterator.Count == 1)
                    {
                        nodeIterator.MoveNext();
                        result = nodeIterator.Current.TypedValue;
                    }
                    else
                    {
                        if (simpleTypeParser == null)
                        {
                            var resultList = new List <object>();
                            while (nodeIterator.MoveNext())
                            {
                                result = nodeIterator.Current.TypedValue;
                                resultList.Add(result);
                            }

                            return(resultList.ToArray());
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                }

                // string results get parsed
                if (result is String)
                {
                    try
                    {
                        return(simpleTypeParser.Invoke((string)result));
                    }
                    catch
                    {
                        Log.Warn("Error parsing XPath property named '" + property + "' expression result '" + result + " as type " + optionalCastToType.Name);
                        return(null);
                    }
                }

                // coercion
                if (result is Double)
                {
                    try
                    {
                        return(CoercerFactory.CoerceBoxed(result, optionalCastToType));
                    }
                    catch
                    {
                        Log.Warn("Error coercing XPath property named '" + property + "' expression result '" + result + " as type " + optionalCastToType.Name);
                        return(null);
                    }
                }

                // check bool type
                if (result is Boolean)
                {
                    if (optionalCastToType != typeof(bool?))
                    {
                        Log.Warn("Error coercing XPath property named '" + property + "' expression result '" + result + " as type " + optionalCastToType.Name);
                        return(null);
                    }
                    return(result);
                }

                Log.Warn("Error processing XPath property named '" + property + "' expression result '" + result + ", not a known type");
                return(null);
            }
            catch (XPathException e)
            {
                throw new PropertyAccessException("Error getting property " + property, e);
            }
        }
Exemplo n.º 14
0
 /// <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);
 }
Exemplo n.º 15
0
        private static object CastToArray(
            object result,
            Type optionalCastToType,
            SimpleTypeParser simpleTypeParser,
            XPathExpression expression)
        {
            if (result is XPathNodeIterator nodeIterator) {
                if (nodeIterator.Count == 0) {
                    return null;
                }

                var array = Arrays.CreateInstanceChecked(optionalCastToType, nodeIterator.Count);
                for (var i = 0; nodeIterator.MoveNext(); i++) {
                    var nodeCurrent = nodeIterator.Current;
                    object arrayItem = null;
                    
                    try {
                        if ((nodeCurrent.NodeType == XPathNodeType.Attribute) ||
                            (nodeCurrent.NodeType == XPathNodeType.Element)) {
                            var textContent = nodeCurrent.Value;
                            arrayItem = simpleTypeParser.Parse(textContent);
                        }
                    }
                    catch (Exception) {
                        if (Log.IsInfoEnabled) {
                            Log.Info(
                                "Parse error for text content " +
                                nodeCurrent.InnerXml +
                                " for expression " +
                                expression);
                        }
                    }
                    
                    array.SetValue(arrayItem, i);
                }

                return array;
            }
            if (result is XmlNodeList) {
                var nodeList = (XmlNodeList) result;
                var array = Arrays.CreateInstanceChecked(optionalCastToType, nodeList.Count);

                for (var i = 0; i < nodeList.Count; i++) {
                    object arrayItem = null;
                    try {
                        var item = nodeList.Item(i);
                        string textContent;
                        if (item.NodeType == XmlNodeType.Attribute || item.NodeType == XmlNodeType.Element) {
                            textContent = item.InnerText;
                        }
                        else {
                            continue;
                        }

                        arrayItem = simpleTypeParser.Parse(textContent);
                    }
                    catch (Exception) {
                        if (Log.IsInfoEnabled) {
                            Log.Info(
                                "Parse error for text content " +
                                nodeList.Item(i).InnerText +
                                " for expression " +
                                expression);
                        }
                    }

                    array.SetValue(arrayItem, i);
                }

                return array;
            }

            return null;
        }
Exemplo n.º 16
0
        /// <summary>
        /// NOTE: Code-generation-invoked method, method name and parameter order matters.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <param name="componentType">Type of the component.</param>
        /// <param name="parser">The parser.</param>
        /// <returns></returns>
        public static object GetDOMArrayFromNodes(object[] result, Type componentType, SimpleTypeParser parser)
        {
            var array = Array.CreateInstance(componentType, result.Length);

            for (int i = 0; i < result.Length; i++)
            {
                var text = GetInnerText(result[i]);
                if (string.IsNullOrEmpty(text))
                {
                    continue;
                }

                var parseResult = parser.Invoke(text);
                array.SetValue(parseResult, i);
            }

            return(array);
        }