f3ad00498733354ca6f6d08652b70cd8858cdbae
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / XPath / Internal / NamespaceQuery.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="NamespaceQuery.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7
8 namespace MS.Internal.Xml.XPath {
9     using System;
10     using System.Xml;
11     using System.Xml.XPath;
12     using System.Diagnostics;
13     using System.Globalization;
14
15     internal sealed class NamespaceQuery : BaseAxisQuery {
16         private bool onNamespace;
17         
18         public NamespaceQuery(Query qyParent, string Name, string Prefix, XPathNodeType Type) : base(qyParent, Name, Prefix, Type) {}
19         private NamespaceQuery(NamespaceQuery other) : base(other) {
20             this.onNamespace = other.onNamespace;
21         }
22
23         public override void Reset() {
24             onNamespace = false;
25             base.Reset();
26         }
27
28         public override XPathNavigator Advance() {
29             while (true) {
30                 if (!onNamespace) {
31                     currentNode = qyInput.Advance();
32                     if (currentNode == null) {
33                         return null;
34                     }
35                     position = 0;
36                     currentNode = currentNode.Clone();
37                     onNamespace = currentNode.MoveToFirstNamespace();
38                 } else {
39                     onNamespace = currentNode.MoveToNextNamespace();
40                 }
41
42                 if (onNamespace) {
43                     if (matches(currentNode)) {
44                         position++;
45                         return currentNode;
46                     }
47                 }
48             } // while
49         } // Advance
50
51         public override bool matches(XPathNavigator e) {
52             Debug.Assert(e.NodeType == XPathNodeType.Namespace);
53             if (e.Value.Length == 0) {
54                 Debug.Assert(e.LocalName.Length == 0, "Only xmlns='' can have empty string as a value");
55                 // Namespace axes never returns xmlns='', 
56                 // because it's not a NS declaration but rather undeclaration.
57                 return false;               
58             }
59             if (NameTest) {
60                 return Name.Equals(e.LocalName);
61             } else {
62                 return true;
63             }
64         }
65                     
66         public override XPathNodeIterator Clone() { return new NamespaceQuery(this); }
67     }
68 }