public void ConstructorTest()
        {
            XsdtTypeConverter target = new XsdtTypeConverter();

            Assert.IsNotNull(target);
            Assert.IsNotNull(target.TypeLookup);
        }
Exemplo n.º 2
0
        private void ProcessCastOperators(Expression e)
        {
            MethodCallExpression mce = (MethodCallExpression)e;
            XsdtTypeConverter    tc  = new XsdtTypeConverter();
            string typeToCastTo      = tc.GetXsdtAttrFor(mce.Type).TypeUri;
            string argName           = SafeDispatch(mce.Arguments[0]);

            QueryAppend("xsd:{1}({0})", argName, typeToCastTo);
        }
		public void TypeLookupTest()
		{
			XsdtTypeConverter target = new XsdtTypeConverter();

			Dictionary<XsdtPrimitiveDataType, Type> val = new Dictionary<XsdtPrimitiveDataType, Type>();
				// TODO: Assign to an appropriate value for the property

			target.TypeLookup = val;


			Assert.AreEqual(val, target.TypeLookup, "VDS.RDF.Linq.XsdtTypeConverter.TypeLookup was not set correctly.");
		}
        public void GetDataTypeTest()
        {
            XsdtTypeConverter target = new XsdtTypeConverter();

            Type t = typeof (string); // TODO: Initialize to an appropriate value

            XsdtPrimitiveDataType expected = XsdtPrimitiveDataType.XsdtString;
            XsdtPrimitiveDataType actual;

            actual = target.GetDataType(t);

            Assert.AreEqual(expected, actual, "LinqToRdf.XsdtTypeConverter.GetDataType did not return the expected value.");
        }
		public void GetDataTypeTest()
		{
			XsdtTypeConverter target = new XsdtTypeConverter();

			Type t = typeof (string); 

			XsdtPrimitiveDataType expected = XsdtPrimitiveDataType.XsdtString;
			XsdtPrimitiveDataType actual;

			actual = target.GetDataType(t);

			Assert.AreEqual(expected, actual, "VDS.RDF.Linq.XsdtTypeConverter.GetDataType did not return the expected value.");
		}
		public void TestXsdtTypeConversions()
		{
			XsdtTypeConverter target = new XsdtTypeConverter();
			Assert.IsTrue(target.Get("hello world").ToString() == "\"hello world\"");
			Assert.IsTrue(target.Get('a').ToString() == "\"a\"");
			Assert.IsTrue(target.Get(true).ToString() == "True^^xsd:boolean");
			Assert.IsTrue(target.Get(false).ToString() == "False^^xsd:boolean");
			Assert.IsTrue(target.Get((float) 3.14).ToString() == "3.14^^xsd:float");
			Assert.IsTrue(target.Get(3.14).ToString() == "3.14^^xsd:double");
			Assert.IsTrue(target.Get(3.14M).ToString() == "3.14^^xsd:decimal");
			Assert.IsTrue(target.Get(new TimeSpan(0, 5, 0)).ToString() == "\"00:05:00\"^^xsd:duration");
			Assert.IsTrue(target.Get<Int16>(4).ToString() == "4^^xsd:short");
			Assert.IsTrue(target.Get(4).ToString() == "4");
			Assert.IsTrue(target.Get<Int64>(4).ToString() == "4^^xsd:long");
			Assert.IsTrue(target.Get<Byte[]>(ASCIIEncoding.ASCII.GetBytes("hello")).ToString() == "\"hello\"^^xsd:hexBinary");
		}
        public void GetXsdtDateRepresentationForTest()
        {
            XsdtTypeConverter target = new XsdtTypeConverter();

            DateTime d = new DateTime(2007, 10, 17, 19, 42, 01); // TODO: Initialize to an appropriate value

            XsdtPrimitiveDataType dt = XsdtPrimitiveDataType.XsdtDateTime; // TODO: Initialize to an appropriate value

            XsdtAttribute attr = new XsdtAttribute(true, "dateTime"); // TODO: Initialize to an appropriate value

            string expected = "2007-10-17T19:42:01+" + ((d.IsDaylightSavingTime())?"11:00":"10:00");
            string actual;
            XsdtTypeConverter tc = new XsdtTypeConverter();
            actual = tc.GetXsdtDateRepresentationFor(d, dt, attr);

            Assert.AreEqual(expected, actual,
                            "LinqToRdf.XsdtTypeConverter.GetXsdtDateRepresentationFor did not return the expec" +
                            "ted value.");
        }
        /// <summary>
        /// Extracts the corresponding value from the SemWeb results for a member access projection.
        /// </summary>
        /// <param name="vb">The SemWeb results.</param>
        /// <returns>the value that was extracted (and converted) from the results.</returns>
        private object ExtractMemberAccess(SparqlResult vb)
        {
            // work out if the SelectExpression really is a member access
            var ue = (SelectExpression).Arguments[1] as UnaryExpression;
            if (ue == null)
                throw new ArgumentException("incompatible expression type");
    
            var le = ue.Operand as LambdaExpression;
            if (le == null)
                throw new LinqToRdfException("Incompatible expression type found when building ontology projection");

            if (le.Body is MemberExpression)
            {
                // work out which member is being queried on
                var memberExpression = (MemberExpression) le.Body;
                MemberInfo memberInfo = memberExpression.Member;
                // get its name and use that as a key into the results
                //string vVal = vb[memberInfo.Name].ToString();
                // convert the result from XSDT format to .NET types
                if (!vb.HasValue(memberInfo.Name)) return null;
                var tc = new XsdtTypeConverter();
                return tc.Parse(vb[memberInfo.Name]);
                //return tc.Parse(vVal);
            }
            return null;
        }
        /// <summary>
        /// stores the result from a SemWeb result into a <see cref="PropertyInfo"/> doing whatever conversions are required.
        /// </summary>
        /// <param name="semwebBindings">The incoming result from semweb.</param>
        /// <param name="obj">The object instance on wqhich the property is to be set</param>
        /// <param name="propertyInfo">The <see cref="PropertyInfo"/> identifying the property to be populated with the value stored in <see cref="semwebBindings"/>.</param>
        public static void PopulateProperty(SparqlResult semwebBindings, object obj, PropertyInfo propertyInfo)
        {
            if (semwebBindings.HasValue(propertyInfo.Name))
            {
                if (semwebBindings[propertyInfo.Name] == null) return;
                var tc = new XsdtTypeConverter();
                object x = tc.Parse(semwebBindings[propertyInfo.Name]);

                if (x is IConvertible)
                    propertyInfo.SetValue(obj, Convert.ChangeType(x, propertyInfo.PropertyType), null);
                else 
                    // if it's not convertible, it could be because the type is an MS XSDT type rather than a .NET primitive 
                    if (x.GetType().Namespace == "System.Runtime.Remoting.Metadata.W3cXsd2001")
                    {
                        switch (x.GetType().Name)
                        {
                            case "SoapDate":
                                var d = (SoapDate) x;
                                propertyInfo.SetValue(obj, Convert.ChangeType(d.Value, propertyInfo.PropertyType), null);
                                break;
                            default:
                                break;
                        }
                    }
                    else if (propertyInfo.PropertyType == typeof (string))
                    {
                        propertyInfo.SetValue(obj, x.ToString(), null);
                    }
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// stores the result from a SemWeb result into a <see cref="PropertyInfo"/> doing whatever conversions are required.
 /// </summary>
 /// <param name="semwebBindings">The incoming result from semweb.</param>
 /// <param name="obj">The object instance on wqhich the property is to be set</param>
 /// <param name="propertyInfo">The <see cref="PropertyInfo"/> identifying the property to be populated with the value stored in <see cref="semwebBindings"/>.</param>
 public static void PopulateProperty(VariableBindings semwebBindings, object obj, PropertyInfo propertyInfo)
 {
     if (semwebBindings[propertyInfo.Name] != null)
     {
         string vVal = semwebBindings[propertyInfo.Name].ToString();
         var tc = new XsdtTypeConverter();
         object x = tc.Parse(vVal);
     //							vVal = RemoveEnclosingQuotesOnString(vVal, pi);
     //							if (IsXsdtEncoded(vVal))
     //								vVal = DecodeXsdtString(vVal);
         if (x is IConvertible)
             propertyInfo.SetValue(obj, Convert.ChangeType(x, propertyInfo.PropertyType), null);
         else
             // if it's not convertible, it could be because the type is an MS XSDT type rather than a .NET primitive
             if (x.GetType().Namespace == "System.Runtime.Remoting.Metadata.W3cXsd2001")
             {
                 switch (x.GetType().Name)
                 {
                     case "SoapDate":
                         var d = (SoapDate) x;
                         propertyInfo.SetValue(obj, Convert.ChangeType(d.Value, propertyInfo.PropertyType), null);
                         break;
                     default:
                         break;
                 }
             }
             else if (propertyInfo.PropertyType == typeof (string))
             {
                 propertyInfo.SetValue(obj, x.ToString(), null);
             }
     }
 }
Exemplo n.º 11
0
 public void TestXsdtToNet()
 {
     XsdtTypeConverter tc = new XsdtTypeConverter();
     //Assert.AreSame(dbl, tc.Parse("\"10\"^^xsdt:double"));
     Graph g = new Graph();
     Assert.AreEqual(tc.Parse(LiteralExtensions.ToLiteral(10.0d, g)), tc.Parse(LiteralExtensions.ToLiteral(10.0d, g)));
     var f = tc.Parse(LiteralExtensions.ToLiteral(10.0f, g));
     var d = tc.Parse(LiteralExtensions.ToLiteral(10.0d, g));
     Assert.IsTrue(f is float);
     Assert.IsTrue(d is double);
     var s = tc.Parse(g.CreateLiteralNode("hello world"));
     Assert.IsTrue(s is string);
     Assert.AreEqual(tc.Parse(LiteralExtensions.ToLiteral(new TimeSpan(1,0,0,0),g)), tc.Parse(LiteralExtensions.ToLiteral(new TimeSpan(1,0,0,0),g)));
     var ts = tc.Parse(g.CreateLiteralNode("P1Y2M3DT10H30M", new Uri(XmlSpecsHelper.XmlSchemaDataTypeDuration)));
     Assert.IsTrue(ts != null && ts is TimeSpan);
     var i = tc.Parse(g.CreateLiteralNode("10"));
     Assert.IsTrue(i is int);
     var i2 = tc.Parse(LiteralExtensions.ToLiteral(10, g));
     Assert.IsTrue(i2 is int);
 }
 public void TestXsdtToNet()
 {
     XsdtTypeConverter tc = new XsdtTypeConverter();
     //Assert.AreSame(dbl, tc.Parse("\"10\"^^xsdt:double"));
     Assert.AreEqual(tc.Parse("\"10\"^^<http://www.w3.org/2001/XMLSchema#double>"), tc.Parse("\"10\"^^xsdt:double"));
     var f = tc.Parse("\"10\"^^<http://www.w3.org/2001/XMLSchema#float>");
     var d = tc.Parse("\"10\"^^xsdt:double");
     Assert.IsTrue(f is float);
     Assert.IsTrue(d is double);
     var s = tc.Parse("\"hello world\"");
     Assert.IsTrue(s is string);
     Assert.AreEqual(tc.Parse("\"P1D\"^^xsdt:duration"), tc.Parse("\"P1D\"^^<http://www.w3.org/2001/XMLSchema#duration>"));
     var ts = tc.Parse("\"P1Y2M3DT10H30M\"^^xsdt:duration");
     Assert.IsTrue(ts != null && ts is TimeSpan);
     var i = tc.Parse("10");
     Assert.IsTrue(i is int);
     var i2 = tc.Parse("10^^xsdt:integer");
     Assert.IsTrue(i2 is int);
 }