示例#1
0
        public static XPath2Expression Compile(string xpath, IXmlNamespaceResolver resolver)
        {
            if (xpath == null)
            {
                throw new ArgumentNullException("xpath");
            }
            if (xpath == "")
            {
                throw new XPath2Exception("", "Empty xpath expression");
            }
            XPath2Context context   = new XPath2Context(resolver);
            Tokenizer     tokenizer = new Tokenizer(xpath);
            YYParser      parser    = new YYParser(context);
            AbstractNode  exprTree  = (AbstractNode)parser.yyparseSafe(tokenizer);

            return(new XPath2Expression(xpath, exprTree, context));
        }
        public bool Match(XPathItem item, XPath2Context context)
        {
            switch (TypeCode)
            {
            case XmlTypeCode.None:
                return(false);

            case XmlTypeCode.Item:
                return(true);

            case XmlTypeCode.Node:
                return(item.IsNode);

            case XmlTypeCode.AnyAtomicType:
                return(!item.IsNode);

            case XmlTypeCode.UntypedAtomic:
                return(!item.IsNode && item.GetSchemaType() == XmlSchema.UntypedAtomic);

            case XmlTypeCode.Document:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    if (nav.NodeType == XPathNodeType.Root)
                    {
                        XPathNavigator cur = nav.Clone();
                        if (SchemaElement == null)
                        {
                            if (cur.MoveToChild(XPathNodeType.Element) && MatchName(cur, context))
                            {
                                if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                                {
                                    return(true);
                                }
                                IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                                if (schemaInfo != null)
                                {
                                    if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                    XmlSchemaDerivationMethod.Empty))
                                    {
                                        return(!schemaInfo.IsNil || Nillable);
                                    }
                                }
                                else
                                {
                                    return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                       XmlSchemaDerivationMethod.Empty));
                                }
                            }
                        }
                        else
                        {
                            if (!cur.MoveToChild(XPathNodeType.Element))
                            {
                                return(false);
                            }
                            IXmlSchemaInfo schemaInfo = cur.SchemaInfo;
                            if (schemaInfo != null)
                            {
                                return(schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName);
                            }
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.Element:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null && nav.NodeType == XPathNodeType.Element)
                {
                    if (SchemaElement == null)
                    {
                        if (MatchName(nav, context))
                        {
                            if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                            {
                                return(true);
                            }
                            IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                            if (schemaInfo != null)
                            {
                                if (XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                XmlSchemaDerivationMethod.Empty))
                                {
                                    return(!schemaInfo.IsNil || Nillable);
                                }
                            }
                            else
                            {
                                return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                        }
                    }
                    else
                    {
                        IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                        if (schemaInfo != null)
                        {
                            return(schemaInfo.SchemaElement.QualifiedName == SchemaElement.QualifiedName);
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.Attribute:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null && nav.NodeType == XPathNodeType.Attribute)
                {
                    if (SchemaAttribute == null)
                    {
                        if (MatchName(nav, context))
                        {
                            if (SchemaType == null || SchemaType == XmlSchema.UntypedAtomic)
                            {
                                return(true);
                            }
                            IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                            if (schemaInfo == null)
                            {
                                return(XmlSchemaType.IsDerivedFrom(XmlSchema.UntypedAtomic, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                            else
                            {
                                return(XmlSchemaType.IsDerivedFrom(schemaInfo.SchemaType, SchemaType,
                                                                   XmlSchemaDerivationMethod.Empty));
                            }
                        }
                    }
                    else
                    {
                        IXmlSchemaInfo schemaInfo = nav.SchemaInfo;
                        if (schemaInfo != null)
                        {
                            return(schemaInfo.SchemaAttribute.QualifiedName == SchemaAttribute.QualifiedName);
                        }
                    }
                }
            }
            break;

            case XmlTypeCode.ProcessingInstruction:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.ProcessingInstruction &&
                           (NameTest.IsNameWildcard || NameTest.Name == nav.Name));
                }
            }
            break;

            case XmlTypeCode.Comment:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.Comment);
                }
            }
            break;

            case XmlTypeCode.Text:
            {
                XPathNavigator nav = item as XPathNavigator;
                if (nav != null)
                {
                    return(nav.NodeType == XPathNodeType.Text ||
                           nav.NodeType == XPathNodeType.SignificantWhitespace);
                }
            }
            break;

            case XmlTypeCode.PositiveInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) > 0);
                }
                break;

            case XmlTypeCode.NegativeInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) < 0);
                }
                break;

            case XmlTypeCode.NonPositiveInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) <= 0);
                }
                break;

            case XmlTypeCode.NonNegativeInteger:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                    return((decimal)item.ValueAs(typeof(Decimal)) >= 0);

                case XmlTypeCode.UnsignedByte:
                case XmlTypeCode.UnsignedShort:
                case XmlTypeCode.UnsignedInt:
                case XmlTypeCode.UnsignedLong:
                    return(true);
                }
                break;

            case XmlTypeCode.Integer:
                switch (item.GetSchemaType().TypeCode)
                {
                case XmlTypeCode.Byte:
                case XmlTypeCode.Short:
                case XmlTypeCode.Int:
                case XmlTypeCode.Long:
                case XmlTypeCode.Integer:
                case XmlTypeCode.UnsignedByte:
                case XmlTypeCode.UnsignedShort:
                case XmlTypeCode.UnsignedInt:
                case XmlTypeCode.UnsignedLong:
                    return(true);

                case XmlTypeCode.Decimal:
                    decimal value = (decimal)item.ValueAs(typeof(Decimal));
                    return(value == Math.Truncate(value));
                }
                break;

            case XmlTypeCode.Entity:
                return((item.GetSchemaType().TypeCode == XmlTypeCode.String) ||
                       (item.GetSchemaType().TypeCode == XmlTypeCode.Entity));

            default:
            {
                if (item.XmlType != null)
                {
                    return(XmlSchemaType.IsDerivedFrom(item.XmlType, SchemaType, XmlSchemaDerivationMethod.Empty));
                }
            }
            break;
            }
            return(false);
        }
 private bool MatchName(XPathNavigator nav, XPath2Context context)
 {
     return((NameTest.IsNamespaceWildcard || NameTest.Namespace == nav.NamespaceURI) &&
            (NameTest.IsNameWildcard || NameTest.Name == nav.LocalName));
 }
示例#4
0
 private XPath2Expression(string expr, AbstractNode exprTree, XPath2Context context)
 {
     this.expr           = expr;
     this.ExpressionTree = exprTree;
     this.context        = context;
 }
示例#5
0
 public object Invoke(XPath2Context context, IContextProvider provider, object[] args)
 {
     return(Delegate?.Invoke(context, provider, args));
 }