Exemplo n.º 1
0
 public XQueryFLWORBase(XQueryContext context, object var, XQuerySequenceType varType, object expr, XQueryExprBase bodyExpr)
     : base(context)
 {
     m_var = var;
     m_varType = varType;
     m_expr = expr;
     m_bodyExpr = bodyExpr;
 }
Exemplo n.º 2
0
 public void PushVar(object id, XQuerySequenceType type, bool external)
 {
     Record node = new Record();
     node.id = id;
     node.type = type;
     node.external = external;
     st.Push(node);
 }
Exemplo n.º 3
0
 public XQueryStepExpr(object nodeTest, XQueryPathExprType type, XQueryContext queryContext)
     : this(type, queryContext)
 {
     if (nodeTest is XmlQualifiedNameTest)
         m_nameTest = (XmlQualifiedNameTest)nodeTest;
     else if (nodeTest is XQuerySequenceType)
         m_typeTest = (XQuerySequenceType)nodeTest;
     else
         throw new ArgumentException("nodeTest");
 }
Exemplo n.º 4
0
 public XQueryFLWOR(XQueryContext context, object var, XQuerySequenceType varType, object pos, object expr, XQueryExprBase bodyExpr, bool convert)
     : base(context, var, varType, expr, bodyExpr)
 {
     m_var = var;
     m_varType = varType;            
     m_pos = pos;
     m_value = new SymbolLink(varType.ValueType);
     m_itemType = varType.ItemType;
     if (m_pos != null)
         m_posValue = new SymbolLink(typeof(Integer));
     m_convert = convert;
 }
Exemplo n.º 5
0
 public bool GetType(object id, out XQuerySequenceType type, out bool external)
 {
     Record[] nodes = st.ToArray();
     for (int k = nodes.Length - 1; k >= 0; k--)
         if (nodes[k].id == id)
         {
             type = nodes[k].type;
             external = nodes[k].external;
             return true;
         }
     type = null;
     external = false;
     return false;
 }
Exemplo n.º 6
0
        private XQuerySequenceType ProcessTypeDecl(Notation notation, Symbol sym)
        {
            if (sym.Tag == Tag.TokenWrapper &&
                ((TokenWrapper)sym).Data == Token.EMPTY_SEQUENCE)
                return XQuerySequenceType.Void;
            else
            {
                XQuerySequenceType type = ProcessItemType(notation, sym);
                Notation.Record[] recs = notation.Select(sym, Descriptor.Occurrence, 1);
                if (recs.Length > 0)
                {
                    type = new XQuerySequenceType(type);
                    TokenWrapper w = (TokenWrapper)recs[0].Arg0;
                    switch (w.Data)
                    {
                        case Token.Indicator1: // (*)
                            type.Cardinality = XmlTypeCardinality.ZeroOrMore; 
                            break;

                        case Token.Indicator2: // (+)
                            type.Cardinality = XmlTypeCardinality.OneOrMore;
                            break;

                        case Token.Indicator3: // (?)
                            type.Cardinality = XmlTypeCardinality.ZeroOrOne;
                            break;
                    }
                }
                return type;
            }
        }
Exemplo n.º 7
0
 private object ProcessFuncallExpr(Notation notation, Notation.Record rec)
 {
     Qname qname = (Qname)rec.Arg0;
     XmlQualifiedName identity = QNameParser.Parse(qname.Name, _context.NamespaceManager, _context.nameTable);
     string ns = identity.Namespace;
     if (identity.Namespace == String.Empty)            
     {
         if (_context.DefaultFunctionNS == null)
             ns = XmlReservedNs.NsXQueryFunc;
         else
             ns = _context.DefaultFunctionNS;
     }
     object f = ATOM.Create(ns, new string[] { identity.Name }, false);
     object[] arg;
     if (rec.args[1] == null)
         arg = new object[1];
     else
     {
         Symbol[] arr = Lisp.ToArray<Symbol>(rec.args[1]);
         arg = new object[arr.Length + 1];
         for (int k = 0; k < arr.Length; k++)
             arg[k + 1] = ProcessExprSingle(notation, arr[k]);                
     }
     if (arg.Length == 2)
     {
         XmlSchemaObject schemaType;
         if (TryProcessTypeName(notation, qname, false, out schemaType))
         {
             XQuerySequenceType seqtype =
                 new XQuerySequenceType((XmlSchemaSimpleType)schemaType, XmlTypeCardinality.One, null);
             if (seqtype == null)
                 throw new XQueryException(Properties.Resources.XPST0051, "untyped");
             if (seqtype.TypeCode == XmlTypeCode.Notation)
                 throw new XQueryException(Properties.Resources.XPST0051, "NOTATION");
             return Lisp.List(ID.CastToItem, arg[1], seqtype);
         }
     }
     arg[0] = f;
     XQueryFunctionRecord fr = _context.FunctionTable.GetRecord(f, arg.Length - 1);
     if (fr == null)
         throw new XQueryException(Properties.Resources.XPST0017,
             identity.Name, arg.Length - 1, ns);
     if (fr.validationReader)
         _context.NeedValidatedParser = true;
     for (int s = 1; s < arg.Length; s++)
     {
         XQuerySequenceType seqtype;
         if (fr.variableParams)
             seqtype = fr.parameters[0];
         else
             seqtype = fr.parameters[s - 1];
         arg[s] = CompileConversion(arg[s], seqtype);
     }
     return Lisp.List(arg);
 }
Exemplo n.º 8
0
 public void PushVar(object id, XQuerySequenceType type)
 {
     PushVar(id, type, false);
 }
Exemplo n.º 9
0
        public static object ValueAs(object value, XQuerySequenceType type,
            XmlNameTable nameTable, XmlNamespaceManager nsmgr)
        {
            if (value == Undefined.Value)
                return value;
            if (value == null)
                value = CoreServices.Generation.RuntimeOps.False;
            if (type.TypeCode == XmlTypeCode.None)
                throw new XQueryException(Properties.Resources.XPTY0004,
                   new XQuerySequenceType(value.GetType(), XmlTypeCardinality.One), "empty-sequence()");
            if (value.GetType() != type.ItemType)
            {
                UntypedAtomic untypedAtomic = value as UntypedAtomic;
                if (untypedAtomic != null)
                    return ChangeType(XQuerySequenceType.XmlSchema.UntypedAtomic, value, type, nameTable, nsmgr);
                switch (type.TypeCode)
                {
                    case XmlTypeCode.Double:
                        if (value is System.Single)
                            return Convert.ToDouble((float)value);
                        if (value is System.Int32)
                            return Convert.ToDouble((int)value);
                        if (value is System.Int64)
                            return Convert.ToDouble((long)value);
                        if (value is Decimal)
                            return Convert.ToDouble((decimal)value);
                        if (value is Integer)
                            return Convert.ToDouble((decimal)(Integer)value);
                        if (value is System.Int16)
                            return Convert.ToDouble((short)value);
                        if (value is System.SByte)
                            return Convert.ToDouble((sbyte)value);
                        break;

                    case XmlTypeCode.Float:
                        if (value is System.Double)
                            return Convert.ToSingle((double)value);
                        if (value is System.Int32)
                            return Convert.ToSingle((int)value);
                        if (value is System.Int64)
                            return Convert.ToSingle((long)value);
                        if (value is Decimal)
                            return Convert.ToSingle((decimal)value);
                        if (value is Integer)
                            return Convert.ToSingle((decimal)(Integer)value);
                        if (value is System.Int16)
                            return Convert.ToSingle((short)value);
                        if (value is System.SByte)
                            return Convert.ToSingle((sbyte)value);
                        break;

                    case XmlTypeCode.Integer:
                        if (Integer.IsDerivedSubtype(value))
                            return Integer.ToInteger(value);
                        break;

                    case XmlTypeCode.Decimal:
                        if (value is Integer)
                            return (decimal)(Integer)value;
                        if (Integer.IsDerivedSubtype(value))
                            return (decimal)Integer.ToInteger(value);
                        break;

                    case XmlTypeCode.Int:
                        if (value is System.Int16)
                            return (int)(short)value;
                        if (value is System.UInt16)
                            return (int)(ushort)value;
                        if (value is System.SByte)
                            return (int)(sbyte)value;
                        if (value is System.Byte)
                            return (int)(byte)value;
                        if (value is Integer)
                            return (int)(Integer)value;
                        break;

                    case XmlTypeCode.Long:
                        if (value is System.Int32)
                            return (long)(int)value;
                        if (value is System.Int16)
                            return (long)(short)value;
                        if (value is System.SByte)
                            return (long)(sbyte)value;
                        if (value is Integer)
                            return (long)(Integer)value;
                        break;
                }
                if (type.TypeCode == XmlTypeCode.AnyUri && value is String)
                    return new AnyUriValue((string)value);
                if (type.TypeCode == XmlTypeCode.String && value is AnyUriValue)
                    return value.ToString();
                if (type.TypeCode == XmlTypeCode.Duration &&
                    (value is YearMonthDurationValue || value is DayTimeDurationValue))
                    return value;
                throw new XQueryException(Properties.Resources.XPTY0004,
                    new XQuerySequenceType(value.GetType(), XmlTypeCardinality.One), type);
            }
            return value;
        }
Exemplo n.º 10
0
 public XQueryLET(XQueryContext context, object var, XQuerySequenceType varType, object expr, XQueryExprBase bodyExpr, bool convert)
     : base(context, var, varType, expr, bodyExpr)
 {
     m_value = new SymbolLink(varType.ValueType);
     m_convert = convert;
 }
Exemplo n.º 11
0
 private void PreProcessFuncDecl(Notation notation, Notation.Record rec)
 {
     Qname qname = (Qname)rec.Arg0;
     XmlQualifiedName identity = QNameParser.Parse(qname.Name, _context.NamespaceManager, _context.nameTable);
     string ns = identity.Namespace;
     if (ns == String.Empty)
     {
         if (_context.DefaultFunctionNS == null)
             ns = XmlReservedNs.NsXQueryFunc;
         else
             ns = _context.DefaultFunctionNS;
     }
     if (ns == String.Empty ||
         ns == XmlReservedNs.NsXml ||
         ns == XmlReservedNs.NsXQueryFunc ||
         ns == XmlReservedNs.NsXs ||
         ns == XmlReservedNs.NsXsi)
         throw new XQueryException(Properties.Resources.XQST0045, identity.Name, identity.Namespace);
     object f = ATOM.Create(ns, new string[] { identity.Name }, false);
     FunctionParameter[] parameters = ProcessParamList(notation, rec.args[1]);
     if (_context.FunctionTable.IsRegistered(f, parameters.Length))
         throw new XQueryException(Properties.Resources.XQST0034, identity.Name, identity.Namespace);            
     XQuerySequenceType[] parameterTypes = new XQuerySequenceType[parameters.Length];
     for (int k = 0; k < parameters.Length; k++)
         parameterTypes[k] = parameters[k].type;
     FunctionDecl decl = new FunctionDecl(f, identity, parameters); 
     if (rec.args.Length > 3)
     {
         decl.returnType = new XQuerySequenceType(ProcessTypeDecl(notation, rec.Arg2));
         if (rec.args[3] == null) // external
             throw new NotImplementedException();
         else
             _context.FunctionTable.Register(f, _context, parameterTypes, decl.returnType);
     }
     else
     {
         if (rec.args[2] == null) // external
             throw new NotImplementedException();
         else
             decl.returnType = XQuerySequenceType.Item;
         _context.FunctionTable.Register(f, _context, parameterTypes, decl.returnType);
     }
     _fdecl.Add(rec.sym, decl);
 }
Exemplo n.º 12
0
 private XQuerySequenceType EvalExprType(object expr)
 {
     if (Lisp.IsFunctor(expr))
     {
         if (IsBooleanFunctor(expr))
             return new XQuerySequenceType(XmlTypeCode.Boolean, XmlTypeCardinality.ZeroOrOne);
         else if (Lisp.IsFunctor(expr, Funcs.Neg))
             return EvalExprType(Lisp.Arg1(expr));
         else if (IsNumberFunctor(expr))
         {
             XQuerySequenceType typ1 = EvalExprType(Lisp.Arg1(expr));
             XQuerySequenceType typ2 = EvalExprType(Lisp.Arg2(expr));
             if (typ1.IsUntypedAtomic)
                 typ1 = new XQuerySequenceType(XmlTypeCode.Double, typ1.Cardinality);
             if (typ2.IsUntypedAtomic)
                 typ2 = new XQuerySequenceType(XmlTypeCode.Double, typ2.Cardinality);
             XmlTypeCardinality cardinality;
             if (typ1.Cardinality == XmlTypeCardinality.ZeroOrOne ||
                 typ2.Cardinality == XmlTypeCardinality.ZeroOrOne)
                 cardinality = XmlTypeCardinality.ZeroOrOne;
             else
                 cardinality = XmlTypeCardinality.One;
             if (Lisp.IsFunctor(expr, Funcs.IDiv))
                 return new XQuerySequenceType(typeof(CoreServices.Integer), cardinality);
             else if (Lisp.IsFunctor(expr, Funcs.Div))
             {
                 if (typ1.ItemType == typeof(System.Double) || typ2.ItemType == typeof(System.Double))
                     return new XQuerySequenceType(XmlTypeCode.Double, cardinality);
                 if (typ1.ItemType == typeof(System.Single) || typ2.ItemType == typeof(System.Single))
                     return new XQuerySequenceType(XmlTypeCode.Float, cardinality);
                 return new XQuerySequenceType(XmlTypeCode.Decimal, cardinality);
             }
             else
             {
                 Type typ3 = ValueProxy.GetType(typ1.ItemType, typ2.ItemType);
                 return new XQuerySequenceType(typ3, cardinality);
             }
         }
         else if (Lisp.IsFunctor(expr, ID.CastToItem))
         {
             XQuerySequenceType type = (XQuerySequenceType)Lisp.Arg2(expr);
             if (type.Cardinality == XmlTypeCardinality.One)
                 return new XQuerySequenceType(type.SchemaType, XmlTypeCardinality.ZeroOrOne, type.ParameterType);
             return new XQuerySequenceType(type);
         }
         else if (Lisp.IsFunctor(expr, Funcs.Cast))
         {
             object arg = Lisp.Arg2(expr);
             if ((Type)arg == typeof(XQuerySequenceType))
                 return new XQuerySequenceType(XQuerySequenceType.ItemS);
             else
                 return new XQuerySequenceType((Type)arg, XmlTypeCardinality.One);
         }
         else if (Lisp.IsFunctor(expr, ID.Number) ||
             Lisp.IsFunctor(expr, ID.CastToNumber2) ||
             Lisp.IsFunctor(expr, ID.CastToNumber3))
             return new XQuerySequenceType(XmlTypeCode.Double);
         else if (Lisp.IsFunctor(expr, ID.CastToNumber1))
             return EvalExprType(Lisp.Second(expr));
         else if (Lisp.IsFunctor(expr, Funcs.Cast))
             return EvalExprType(Lisp.Second(expr));
         else if (Lisp.IsFunctor(expr, ID.Atomize) ||
             Lisp.IsFunctor(expr, ID.AtomizeBody))
         {
             XQuerySequenceType typ = EvalExprType(Lisp.Second(expr));
             XmlTypeCardinality cardinality;
             if (typ.Cardinality == XmlTypeCardinality.One ||
                 typ.Cardinality == XmlTypeCardinality.OneOrMore)
                 cardinality = XmlTypeCardinality.One;
             else
                 cardinality = XmlTypeCardinality.ZeroOrOne;
             if (typ.IsNode)
             {
                 if (typ.SchemaType != null)
                     return new XQuerySequenceType(typ.SchemaType, cardinality, typeof(System.Object));
                 else
                     return new XQuerySequenceType(XmlTypeCode.AnyAtomicType, cardinality);
             }
             else
                 return new XQuerySequenceType(typ.TypeCode, cardinality);
         }
         else if (Lisp.IsFunctor(expr, ID.CastTo))
             return (XQuerySequenceType)Lisp.Arg2(expr);
         else if (Lisp.IsFunctor(expr, ID.Par))
             return EvalExprType(Lisp.Second(expr));
         else if (Lisp.IsFunctor(expr, ID.Range))
             return new XQuerySequenceType(XmlTypeCode.Integer, XmlTypeCardinality.ZeroOrMore);
         else if (IsXPathFunctor(expr))
             return XQuerySequenceType.ItemS;
         else if (Lisp.IsFunctor(expr, Funcs.If))
         {
             XQuerySequenceType typ1 = EvalExprType(Lisp.Arg2(expr));
             XQuerySequenceType typ2 = EvalExprType(Lisp.Arg3(expr));
             if (typ1.Equals(typ2))
                 return typ1;
         }
         else
         {
             XQueryFunctionRecord rec = _context.FunctionTable.GetRecord(expr);
             if (rec != null)
                 return rec.returnType;
         }
     }
     else if (expr is XQueryExpr)
     {
         XQueryExpr dynExpr = (XQueryExpr)expr;
         XQuerySequenceType typ = new XQuerySequenceType(EvalExprType(dynExpr.m_expr[0]));
         for (int k = 1; k < dynExpr.m_expr.Length; k++)
         {
             XQuerySequenceType typ2 = EvalExprType(dynExpr.m_expr[k]);
             if (typ.TypeCode != typ2.TypeCode)
                 return XQuerySequenceType.ItemS;
             if (typ.Cardinality == XmlTypeCardinality.One)
                 typ.Cardinality = XmlTypeCardinality.OneOrMore;
             else if (typ.Cardinality == XmlTypeCardinality.ZeroOrOne)
             {
                 if (typ2.Cardinality == XmlTypeCardinality.One ||
                     typ2.Cardinality == XmlTypeCardinality.OneOrMore)
                     typ.Cardinality = XmlTypeCardinality.OneOrMore;
                 else
                     typ.Cardinality = XmlTypeCardinality.ZeroOrMore;
             }
             else if (typ.Cardinality == XmlTypeCardinality.ZeroOrMore)
             {
                 if (typ2.Cardinality == XmlTypeCardinality.One ||
                     typ2.Cardinality == XmlTypeCardinality.OneOrMore)
                     typ.Cardinality = XmlTypeCardinality.OneOrMore;
             }
         }
         return typ;
     }
     else
     {
         if (Lisp.IsAtom(expr))
         {
             XQuerySequenceType type = _varTable.GetType(expr);
             if (type != null)
                 return type;
         }
         else
             return new XQuerySequenceType(expr.GetType(), XmlTypeCardinality.One);
     }
     return XQuerySequenceType.Item;
 }
Exemplo n.º 13
0
 public static bool InstanceOf([Implict] Executive engine, object value, XQuerySequenceType destType)
 {
     XQueryContext context = (XQueryContext)engine.Owner;
     if (value == Undefined.Value)
         return destType.Cardinality == XmlTypeCardinality.ZeroOrOne ||
             destType.Cardinality == XmlTypeCardinality.ZeroOrMore;
     if (value == null)
         value = CoreServices.Generation.RuntimeOps.False;
     XQueryNodeIterator iter = value as XQueryNodeIterator;
     if (iter != null)
     {
         int num = 0;
         foreach (XPathItem item in iter)
         {
             if (num == 1)
             {
                 if (destType.Cardinality == XmlTypeCardinality.ZeroOrOne ||
                     destType.Cardinality == XmlTypeCardinality.One)
                     return false;
             }
             if (!destType.Match(item, context))
                 return false;
             num++;
         }
         if (num == 0)
         {
             if (destType.TypeCode != XmlTypeCode.None && (destType.Cardinality == XmlTypeCardinality.One ||
                  destType.Cardinality == XmlTypeCardinality.OneOrMore))
                 return false;
         }
         return true;
     }
     else                
     {
         if (destType.ItemType == value.GetType())
             return true;
         XPathItem item = value as XPathItem;
         if (item == null)
             item = new XQueryItem(value);
         return destType.Match(item, context);
     }
 }
Exemplo n.º 14
0
 public static object CastToItem([Implict] Executive executive, 
     object value, XQuerySequenceType destType)
 {            
     XQueryContext context = (XQueryContext)executive.Owner;
     if (value == null)
         value = CoreServices.Generation.RuntimeOps.False;
     else
     {
         value = Atomize(value);
         if (value == Undefined.Value)
         {
             if (destType.TypeCode == XmlTypeCode.String)
                 return String.Empty;
             return value;
         }
     }
     XmlTypeCode typeCode = XQuerySequenceType.GetXmlTypeCode(value.GetType());
     XmlSchemaType xmlType = XmlSchemaSimpleType.GetBuiltInSimpleType(typeCode);
     return XQueryConvert.ChangeType(xmlType, value, 
         destType, context.nameTable, context.NamespaceManager);
 }
Exemplo n.º 15
0
 private object CompileAtomizeNumberExpr(object expr, XQuerySequenceType type)
 {
     if (type.Equals(XQuerySequenceType.Item))
         return Lisp.List(ID.CastToNumber1, Lisp.List(ID.Atomize, expr));
     else if (type.IsNode || type.Cardinality != XmlTypeCardinality.One)
     {
         Type resType = type.AtomizedValueType;
         if (resType == typeof(UntypedAtomic) &&
             (_context.SchemaProcessing == SchemaProcessingMode.Force || _context.NeedValidatedParser))
             resType = typeof(System.Object);
         if (type.Cardinality == XmlTypeCardinality.One ||
             type.Cardinality == XmlTypeCardinality.OneOrMore)
         {
             if (ValueProxy.IsNumeric(resType))
                 return Lisp.List(Funcs.Cast, Lisp.List(ID.AtomizeX, expr), resType);
             else if (resType == typeof(UntypedAtomic))
                 return Lisp.List(ID.CastToNumber2, Lisp.List(ID.AtomizeX, expr));
             else
                 return Lisp.List(ID.CastToNumber1, Lisp.List(ID.AtomizeX, expr));
         }
         else
         {
             if (ValueProxy.IsNumeric(resType))
                 return Lisp.List(Funcs.Cast, Lisp.List(ID.Atomize, expr), resType);
             else if (resType == typeof(UntypedAtomic))
                 return Lisp.List(ID.CastToNumber2, Lisp.List(ID.Atomize, expr));
             else
                 return Lisp.List(ID.CastToNumber1, Lisp.List(ID.Atomize, expr));
         }
     }
     else
     {
         if (type.IsNumeric)
             return Lisp.List(Funcs.Cast, expr, type.ItemType);
         else if (type.IsUntypedAtomic)
             return Lisp.List(ID.CastToNumber2, expr);
         else
             return Lisp.List(ID.CastToNumber1, expr);
     }
 }
Exemplo n.º 16
0
 public static bool Castable([Implict] Executive engine, object value, XQuerySequenceType destType, Type exprType)
 {
     try
     {
         CastTo(engine, value, destType, exprType);
         return true;
     }
     catch(XQueryException)
     {
         return false;
     }
 }
Exemplo n.º 17
0
 private object CompileAtomizeValueExpr(object expr, XQuerySequenceType type)
 {
     if (type.Equals(XQuerySequenceType.Item))
         return Lisp.List(ID.Atomize, expr);
     else if (type.IsNode || type.Cardinality != XmlTypeCardinality.One)
     {
         Type resType = type.AtomizedValueType;
         if (type.Cardinality == XmlTypeCardinality.One ||
             type.Cardinality == XmlTypeCardinality.OneOrMore)
         {
             if (resType != typeof(System.Object))
                 return Lisp.List(Funcs.Cast, Lisp.List(ID.AtomizeX, expr), resType);
             else
                 return Lisp.List(ID.AtomizeX, expr);
         }
         else
         {
             if (resType != typeof(System.Object))
                 return Lisp.List(Funcs.Cast, Lisp.List(ID.Atomize, expr), resType);
             else
                 return Lisp.List(ID.Atomize, expr);
         }
     }
     else
     {
         if (type.ItemType != typeof(System.Object))
             return Lisp.List(Funcs.Cast, expr, type.ItemType);
         else
             return expr;
     }
 }
Exemplo n.º 18
0
 public static object CastTo([Implict] Executive engine, object value, XQuerySequenceType destType, Type exprType)
 {
     XQueryContext context = (XQueryContext)engine.Owner;
     if (destType == XQuerySequenceType.Item)
         return value;
     if (value == Undefined.Value)
     {
         if (destType.Cardinality == XmlTypeCardinality.ZeroOrMore)
             return EmptyIterator.Shared;
         if (destType.TypeCode != XmlTypeCode.None && destType.Cardinality != XmlTypeCardinality.ZeroOrOne)
             throw new XQueryException(Properties.Resources.XPTY0004, "empty-sequence()", destType);
         return Undefined.Value;
     }
     if (destType.Cardinality == XmlTypeCardinality.One ||
         destType.Cardinality == XmlTypeCardinality.ZeroOrOne)
     {
         XPathItem res;
         XQueryNodeIterator iter = value as XQueryNodeIterator;
         if (iter != null)
         {
             iter = iter.Clone();
             if (!iter.MoveNext())
             {
                 if (destType.TypeCode != XmlTypeCode.None && 
                     (destType.Cardinality == XmlTypeCardinality.One || destType.Cardinality == XmlTypeCardinality.OneOrMore))
                     throw new XQueryException(Properties.Resources.XPTY0004, "empty-sequence()", destType);
                 return Undefined.Value;
             }
             if (exprType != null && exprType != typeof(System.String))
             {
                 if ((destType.TypeCode == XmlTypeCode.QName && iter.Current.XmlType.TypeCode != XmlTypeCode.QName) ||
                     (destType.TypeCode == XmlTypeCode.Notation && iter.Current.XmlType.TypeCode != XmlTypeCode.Notation))
                     throw new XQueryException(Properties.Resources.XPTY0004_CAST, destType);
             }
             res = iter.Current.ChangeType(destType, context);
             if (iter.MoveNext())
                 throw new XQueryException(Properties.Resources.MoreThanOneItem);
             if (destType.IsNode)
                 return res;                    
             return res.TypedValue;
         }
         XPathItem item = value as XPathItem;
         if (item == null)
             item = new XQueryItem(value);
         if (exprType != null && exprType != typeof(System.String))
         {
             if ((destType.TypeCode == XmlTypeCode.QName && item.XmlType.TypeCode != XmlTypeCode.QName) ||
                 (destType.TypeCode == XmlTypeCode.Notation && item.XmlType.TypeCode != XmlTypeCode.Notation))
                 throw new XQueryException(Properties.Resources.XPTY0004_CAST, destType);
         }
         res = item.ChangeType(destType, context);
         if (destType.IsNode)
             return res;
         return res.TypedValue;
     }
     else
         return new NodeIterator(XPathFactory.ConvertIterator(XQueryNodeIterator.Create(value), destType, context));
 }
Exemplo n.º 19
0
 private object CompileConversion(object expr, XQuerySequenceType destType)
 {
     if (destType == XQuerySequenceType.Item)
         return expr;
     XQuerySequenceType type = EvalExprType(expr);
     if (destType.TypeCode == XmlTypeCode.None)
     {
         if (type != XQuerySequenceType.Item &&
             (type.Cardinality == XmlTypeCardinality.One ||
              type.Cardinality == XmlTypeCardinality.OneOrMore))
           throw new XQueryException(Properties.Resources.XPTY0004, type, destType);
         return Lisp.List(ID.CastArg, expr, destType);
     }
     if ((destType.Cardinality == XmlTypeCardinality.ZeroOrMore ||
          destType.Cardinality == XmlTypeCardinality.OneOrMore) &&
         (type.Cardinality == XmlTypeCardinality.One ||
          type.Cardinality == XmlTypeCardinality.ZeroOrOne ||
          Lisp.IsFunctor(expr, Funcs.Let1) || Lisp.IsFunctor(expr, Funcs.Let2)))
         expr = Lisp.List(ID.Seq, Lisp.List(Funcs.LambdaQuote, expr)); // !!
     Type clrType = destType.ParameterType;
     if (clrType != null && clrType.IsArray)
         clrType = clrType.GetElementType();
     if (clrType == null)
         clrType = destType.ValueType;
     if ((destType.Cardinality == XmlTypeCardinality.One ||
          destType.Cardinality == XmlTypeCardinality.ZeroOrOne) &&
            (type.Cardinality == XmlTypeCardinality.ZeroOrOne ||
             type.Cardinality == XmlTypeCardinality.ZeroOrMore ||
             type.Cardinality == XmlTypeCardinality.OneOrMore ||                    
             type.Equals(XQuerySequenceType.Item)))
     {
         if (destType.IsNode)
         {
             if (clrType == typeof(XPathNavigator))
             {
                 if (destType.Cardinality == XmlTypeCardinality.One)
                     return Lisp.List(ID.NodeValueX, expr);
                 else
                     return Lisp.List(ID.NodeValue, expr);
             }
             else
                 return Lisp.List(ID.NodeValueBody,
                     Lisp.List(Funcs.LambdaQuote, expr));
         }
         else
         {
             if (destType.Cardinality == XmlTypeCardinality.One)
             {
                 if (IsNumberFunctor(expr))
                     expr = Lisp.List(ID.ExactlyOne, 
                         Lisp.List(Funcs.LambdaQuote, expr));
                 else
                     expr = Lisp.List(ID.AtomizeX, expr);
             }
             else
             {
                 if (clrType == typeof(System.Object))
                     expr = Lisp.List(ID.AtomizeBody, expr);
                 else
                     expr = Lisp.List(ID.Atomize, expr);
             }
         }
     }
     if (clrType == typeof(XPathNavigator) && !type.IsNode)
         throw new XQueryException(Properties.Resources.XPTY0004, type, destType);
     if (destType.ItemType != typeof(System.Object) &&
          type.ItemType != destType.ItemType)
     {
         if (destType.TypeCode != XmlTypeCode.AnyAtomicType)
             expr = Lisp.List(ID.CastArg, expr, destType);
     }
     if (clrType != typeof(System.Object))
         expr = Lisp.List(Funcs.Cast, expr, clrType);
     return expr;
 }
Exemplo n.º 20
0
 public static object CastArg([Implict] Executive engine, object value, XQuerySequenceType destType)
 {
     XQueryContext context = (XQueryContext)engine.Owner;
     if (destType == XQuerySequenceType.Item)
         return value;
     if (value == Undefined.Value)
     {
         if (destType.Cardinality == XmlTypeCardinality.ZeroOrMore)
             return EmptyIterator.Shared;
         if (destType.TypeCode != XmlTypeCode.None && destType.Cardinality != XmlTypeCardinality.ZeroOrOne)
             throw new XQueryException(Properties.Resources.XPTY0004, "empty-sequence()", destType);
         return Undefined.Value;
     }
     if (destType.Cardinality == XmlTypeCardinality.One ||
         destType.Cardinality == XmlTypeCardinality.ZeroOrOne)
     {
         object res;
         XQueryNodeIterator iter = value as XQueryNodeIterator;
         if (iter != null)
         {
             iter = iter.Clone();
             if (!iter.MoveNext())
             {
                 if (destType.TypeCode != XmlTypeCode.None &&
                     (destType.Cardinality == XmlTypeCardinality.One || destType.Cardinality == XmlTypeCardinality.OneOrMore))
                     throw new XQueryException(Properties.Resources.XPTY0004, "empty-sequence()", destType);
                 return Undefined.Value;
             }
             if (destType.IsNode)
             {
                 if (!destType.Match(iter.Current, context))
                     throw new XQueryException(Properties.Resources.XPTY0004,
                         new XQuerySequenceType(iter.Current.XmlType, XmlTypeCardinality.OneOrMore, null), destType);
                 res = iter.Current.Clone();
             }
             else
                 res = XQueryConvert.ValueAs(iter.Current.TypedValue, destType, context.nameTable, context.NamespaceManager);
             if (iter.MoveNext())
                 throw new XQueryException(Properties.Resources.MoreThanOneItem);
             return res;
         }
         else
         {
             XPathItem item = value as XPathItem;
             if (item != null)
             {
                 if (item.IsNode)
                 {
                     if (!destType.Match(item, context))
                         throw new XQueryException(Properties.Resources.XPTY0004,
                             new XQuerySequenceType(item.XmlType, XmlTypeCardinality.OneOrMore, null), destType);
                     return item;
                 }
                 else
                     return XQueryConvert.ValueAs(item.TypedValue, destType,
                         context.nameTable, context.NamespaceManager);
             }
             return XQueryConvert.ValueAs(value, destType, context.nameTable, context.NamespaceManager);
         }
     }
     else
         return new NodeIterator(XPathFactory.ValueIterator(XQueryNodeIterator.Create(value), destType, context));
 }
Exemplo n.º 21
0
 public void Register(object id, XQueryContext module, XQuerySequenceType[] parameters, XQuerySequenceType resType)
 {
     XQueryFunctionRecord rec = new XQueryFunctionRecord();
     rec.id = id;
     rec.module = module;
     rec.parameters = parameters;
     rec.returnType = resType;
     FunctionSocket sock = new FunctionSocket(rec);
     FunctionSocket next;
     if (m_table.TryGetValue(rec.id, out next))
         sock.next = next;
     m_table[rec.id] = sock;
 }
Exemplo n.º 22
0
        public bool IsDerivedFrom(XQuerySequenceType src)
        {
            switch (src.TypeCode)
            {
                case XmlTypeCode.Node:
                    if (!IsNode)
                        return false;
                    break;

                case XmlTypeCode.AnyAtomicType:
                case XmlTypeCode.UntypedAtomic:
                    if (IsNode)
                        return false;
                    break;

                case XmlTypeCode.Document:
                    if (TypeCode != XmlTypeCode.Document ||
                        SchemaElement != src.SchemaElement)
                        return false;
                    break;

                case XmlTypeCode.Element:
                    if (TypeCode != XmlTypeCode.Element ||
                        SchemaElement != src.SchemaElement)
                        return false;
                    break;

                case XmlTypeCode.Attribute:
                    if (TypeCode != XmlTypeCode.Attribute ||
                        SchemaAttribute != src.SchemaAttribute)
                        return false;
                    break;

                case XmlTypeCode.ProcessingInstruction:
                    if (TypeCode != XmlTypeCode.ProcessingInstruction)
                        return false;
                    break;

                case XmlTypeCode.Comment:
                    if (TypeCode != XmlTypeCode.Comment)
                        return false;
                    break;

                case XmlTypeCode.Text:
                    if (TypeCode != XmlTypeCode.Text)
                        return false;
                    break;
            }
            if (SchemaType != null || src.SchemaType != null)
            {
                if (SchemaType != null && src.SchemaType != null)
                {
                    if (!XmlSchemaType.IsDerivedFrom(SchemaType, src.SchemaType, XmlSchemaDerivationMethod.Empty))
                        return false;
                }
                else
                    return false;
            }

            if (Cardinality != src.Cardinality)
            {
                if ((Cardinality == XmlTypeCardinality.ZeroOrOne ||
                     Cardinality == XmlTypeCardinality.ZeroOrMore) &&
                     (src.Cardinality == XmlTypeCardinality.One ||
                      src.Cardinality == XmlTypeCardinality.OneOrMore))
                    return false;
                if (Cardinality == XmlTypeCardinality.One &&
                    src.Cardinality == XmlTypeCardinality.OneOrMore)
                    return false;
            }
            return true;
        }
Exemplo n.º 23
0
        public static object ChangeType(XmlSchemaType xmlType, object value, XQuerySequenceType type, 
            XmlNameTable nameTable, XmlNamespaceManager nsmgr)
        {
            if (type.TypeCode == XmlTypeCode.AnyAtomicType || xmlType.TypeCode == type.TypeCode)
                return value;
            try
            {
                switch (xmlType.TypeCode)
                {
                    case XmlTypeCode.String:
                    case XmlTypeCode.UntypedAtomic:
                        switch (type.TypeCode)
                        {
                            case XmlTypeCode.UntypedAtomic:
                                return new UntypedAtomic(value.ToString());
                            case XmlTypeCode.String:
                                return value.ToString();
                            case XmlTypeCode.DateTime:
                                return DateTimeValue.Parse(value.ToString());
                            case XmlTypeCode.Date:
                                return DateValue.Parse(value.ToString());
                            case XmlTypeCode.Time:
                                return TimeValue.Parse(value.ToString());
                            case XmlTypeCode.GYearMonth:
                                return GYearMonthValue.Parse(value.ToString());
                            case XmlTypeCode.GYear:
                                return GYearValue.Parse(value.ToString());
                            case XmlTypeCode.GMonth:
                                return GMonthValue.Parse(value.ToString());
                            case XmlTypeCode.GMonthDay:
                                return GMonthDayValue.Parse(value.ToString());
                            case XmlTypeCode.GDay:
                                return GDayValue.Parse(value.ToString());
                            case XmlTypeCode.Duration:
                                return DurationValue.Parse(value.ToString());
                            case XmlTypeCode.QName:
                                if (xmlType.TypeCode == XmlTypeCode.UntypedAtomic)
                                    throw new XQueryException(Properties.Resources.XPTY0004,
                                        new XQuerySequenceType(xmlType, XmlTypeCardinality.One, null), type);
                                return QNameValue.Parse(value.ToString(), nsmgr);
                            case XmlTypeCode.Notation:
                                return NotationValue.Parse(value.ToString(), nsmgr);
                            case XmlTypeCode.AnyUri:
                                return new AnyUriValue(value.ToString());

                            default:
                                {
                                    string text = value.ToString();
                                    object res = type.SchemaType.Datatype.ParseValue(text, nameTable, nsmgr);
                                    switch (type.TypeCode)
                                    {
                                        case XmlTypeCode.Integer:
                                        case XmlTypeCode.PositiveInteger:
                                        case XmlTypeCode.NegativeInteger:
                                        case XmlTypeCode.NonPositiveInteger:
                                        case XmlTypeCode.NonNegativeInteger:
                                            return (Integer)Convert.ToDecimal(res);
                                        case XmlTypeCode.DayTimeDuration:
                                            return new DayTimeDurationValue((TimeSpan)res);
                                        case XmlTypeCode.YearMonthDuration:
                                            return new YearMonthDurationValue((TimeSpan)res);
                                        case XmlTypeCode.HexBinary:
                                            return new HexBinaryValue((byte[])res);
                                        case XmlTypeCode.Base64Binary:
                                            if (text.EndsWith("==") && (text.Length < 3 || "AQgw".IndexOf(text[text.Length - 3]) == -1))
                                                throw new XQueryException(Properties.Resources.FORG0001, value);
                                            return new Base64BinaryValue((byte[])res);
                                        case XmlTypeCode.Idref:
                                            if (type.SchemaType == XQuerySequenceType.XmlSchema.IDREFS)
                                                return new IDREFSValue((string[])res);
                                            goto default;
                                        case XmlTypeCode.NmToken:
                                            if (type.SchemaType == XQuerySequenceType.XmlSchema.NMTOKENS)
                                                return new NMTOKENSValue((string[])res);
                                            goto default;
                                        case XmlTypeCode.Entity:
                                            if (type.SchemaType == XQuerySequenceType.XmlSchema.ENTITIES)
                                                return new ENTITIESValue((string[])res);
                                            goto default;
                                        default:
                                            return res;
                                    }
                                }
                        }

                    case XmlTypeCode.Boolean:
                        switch (type.TypeCode)
                        {
                            case XmlTypeCode.Decimal:
                            case XmlTypeCode.Float:
                            case XmlTypeCode.Double:
                            case XmlTypeCode.Integer:
                            case XmlTypeCode.NonPositiveInteger:
                            case XmlTypeCode.NegativeInteger:
                            case XmlTypeCode.Long:
                            case XmlTypeCode.Int:
                            case XmlTypeCode.Short:
                            case XmlTypeCode.Byte:
                            case XmlTypeCode.NonNegativeInteger:
                            case XmlTypeCode.UnsignedLong:
                            case XmlTypeCode.UnsignedInt:
                            case XmlTypeCode.UnsignedShort:
                            case XmlTypeCode.UnsignedByte:
                            case XmlTypeCode.PositiveInteger:
                                return ChangeType(value, type.ItemType);

                            case XmlTypeCode.String:
                                return XQueryConvert.ToString((bool)value);
                            case XmlTypeCode.UntypedAtomic:
                                return new UntypedAtomic(XQueryConvert.ToString((bool)value));
                        }
                        break;

                    case XmlTypeCode.Integer:
                    case XmlTypeCode.NonPositiveInteger:
                    case XmlTypeCode.NegativeInteger:
                    case XmlTypeCode.Long:
                    case XmlTypeCode.Int:
                    case XmlTypeCode.Short:
                    case XmlTypeCode.Byte:
                    case XmlTypeCode.NonNegativeInteger:
                    case XmlTypeCode.UnsignedLong:
                    case XmlTypeCode.UnsignedInt:
                    case XmlTypeCode.UnsignedShort:
                    case XmlTypeCode.UnsignedByte:
                    case XmlTypeCode.PositiveInteger:
                    case XmlTypeCode.Decimal:
                    case XmlTypeCode.Float:
                    case XmlTypeCode.Double:
                        switch (type.TypeCode)
                        {
                            case XmlTypeCode.String:
                                return ToString(value);
                            case XmlTypeCode.UntypedAtomic:
                                return new UntypedAtomic(ToString(value));
                            case XmlTypeCode.Boolean:
                                return Core.BooleanValue(value);
                            case XmlTypeCode.AnyUri:
                                throw new XQueryException(Properties.Resources.XPTY0004,
                                    new XQuerySequenceType(xmlType, XmlTypeCardinality.One, null), type);
                            default:
                                return ChangeType(value, type.ItemType);
                        }

                    default:
                        {
                            IXmlConvertable convert = value as IXmlConvertable;
                            if (convert != null)
                                return convert.ValueAs(type, nsmgr);
                            if (type.TypeCode == XmlTypeCode.String)
                                return ToString(value);
                            if (type.TypeCode == XmlTypeCode.UntypedAtomic)
                                return new UntypedAtomic(ToString(value));
                            return type.SchemaType.Datatype.ChangeType(value, type.ValueType);
                        }
                }
            }
            catch (XmlSchemaException ex)
            {
                throw new XQueryException(ex.Message, ex);
            }
            catch (InvalidCastException)
            {
                throw new XQueryException(Properties.Resources.XPTY0004,
                    new XQuerySequenceType(xmlType, XmlTypeCardinality.One, null), type);
            }
            catch (FormatException)
            {
                throw new XQueryException(Properties.Resources.XPTY0004,
                    new XQuerySequenceType(xmlType, XmlTypeCardinality.One, null),type);
            }            
            throw new XQueryException(Properties.Resources.XPTY0004,
                new XQuerySequenceType(xmlType, XmlTypeCardinality.One, null), type);
        }
Exemplo n.º 24
0
 public XQuerySequenceType(XQuerySequenceType src)
 {
     TypeCode = src.TypeCode;
     NameTest = src.NameTest;
     Cardinality = src.Cardinality;
     SchemaType = src.SchemaType;
     SchemaElement = src.SchemaElement;
     SchemaAttribute = src.SchemaAttribute;
     Nillable = src.Nillable;
     ParameterType = src.ParameterType;
     IsNode = src.IsNode;
     ItemType = src.ItemType;
 }
Exemplo n.º 25
0
 public static object TreatValueAs(object value, XQuerySequenceType type)
 {
     if (value == Undefined.Value)
         return value;
     if (value == null)
         value = CoreServices.Generation.RuntimeOps.False;
     if (type.TypeCode == XmlTypeCode.None)
         throw new XQueryException(Properties.Resources.XPTY0004,
            new XQuerySequenceType(value.GetType(), XmlTypeCardinality.One), "empty-sequence()");
     if (value.GetType() != type.ItemType && 
         type.ItemType != typeof(System.Object))
     {
         if (type.ItemType == typeof(Integer))
         {
             if (value is System.Int32)
                 return (Integer)(int)value;
             else if (value is System.UInt32)
                 return (Integer)(uint)value;
             else if (value is System.Int64)
                 return (Integer)(long)value;
             else if (value is System.UInt64)
                 return (Integer)(ulong)value;
             else if (value is System.Int16)
                 return (Integer)(short)value;
             else if (value is System.UInt16)
                 return (Integer)(ushort)value;
             else if (value is System.SByte)
                 return (Integer)(sbyte)value;                    
         }
         else if (type.ItemType == typeof(Decimal))
         {
             if (value is Integer)
                 return (Decimal)(Integer)value;
             else if (value is System.Int32)
                 return (Decimal)(int)value;
             if (value is System.UInt32)
                 return (Decimal)(uint)value;
             else if (value is System.Int64)
                 return (Decimal)(long)value;
             else if (value is System.UInt64)
                 return (Decimal)(ulong)value;
             else if (value is System.Int16)
                 return (Decimal)(short)value;
             else if (value is System.UInt16)
                 return (Decimal)(ushort)value;
             else if (value is System.SByte)
                 return (Decimal)(sbyte)value;
         }
         throw new XQueryException(Properties.Resources.XPTY0004,
             new XQuerySequenceType(value.GetType(), XmlTypeCardinality.One), type);
     }
     return value;
 }
Exemplo n.º 26
0
 object IXmlConvertable.ValueAs(XQuerySequenceType type, XmlNamespaceManager nsmgr)
 {
     switch (type.TypeCode)
     {
         case XmlTypeCode.AnyAtomicType:
         case XmlTypeCode.QName:
             return this;
         case XmlTypeCode.String:
             return ToString();
         case XmlTypeCode.UntypedAtomic:
             return new UntypedAtomic(ToString());
         default:
             throw new InvalidCastException();
     }
 }