svn path=/branches/mono-1-1-9/mcs/; revision=51207
[mono.git] / mcs / class / System.XML / Mono.Xml.Schema / XsdKeyTable.cs
1 //
2 // Mono.Xml.Schema.XsdKeyTable.cs
3 //
4 // Author:
5 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
6 //
7 //      (C)2003 Atsushi Enomoto
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Xml;
34 using System.Xml.Schema;
35
36 namespace Mono.Xml.Schema
37 {
38         internal class XsdKeyEntryCollection : CollectionBase
39         {
40                 public void Add (XsdKeyEntry entry)
41                 {
42                         List.Add (entry);
43                 }
44
45                 public XsdKeyEntry this [int i] {
46                         get { return (XsdKeyEntry) List [i]; }
47                         set { List [i] = value; }
48                 }
49         }
50
51         // Created per constraining element.
52         internal class XsdKeyTable
53         {
54                 // FIXME: no need after #70419
55                 public readonly bool alwaysTrue = true;
56
57                 private XsdIdentitySelector selector;
58                 private XmlSchemaIdentityConstraint source;
59                 private XmlQualifiedName qname;
60                 private XmlQualifiedName refKeyName;
61
62                 public XsdKeyEntryCollection Entries =
63                         new XsdKeyEntryCollection ();
64                 public XsdKeyEntryCollection FinishedEntries =
65                         new XsdKeyEntryCollection ();
66
67                 public int StartDepth;
68                 public XsdKeyTable ReferencedKey;
69
70                 public XsdKeyTable (XmlSchemaIdentityConstraint source)
71                 {
72                         Reset (source);
73                 }
74
75                 public XmlQualifiedName QualifiedName {
76                         get { return qname; }
77                 }
78
79                 public XmlQualifiedName RefKeyName {
80                         get { return refKeyName; }
81                 }
82
83                 public XmlSchemaIdentityConstraint SourceSchemaIdentity {
84                         get { return source; }
85                 }
86
87                 public XsdIdentitySelector Selector {
88                         get { return selector; }
89                 }
90
91                 public void Reset (XmlSchemaIdentityConstraint source)
92                 {
93                         this.source = source;
94                         this.selector = source.CompiledSelector;
95                         this.qname = source.QualifiedName;
96                         XmlSchemaKeyref kr = source as XmlSchemaKeyref;
97                         if (kr != null)
98                                 this.refKeyName = kr.Refer;
99                         StartDepth = 0;
100                 }
101
102                 // In this method, attributes are ignored.
103                 public XsdIdentityPath SelectorMatches (ArrayList qnameStack, int depth)
104                 {
105                         for (int i = 0; i < Selector.Paths.Length; i++) {
106                                 XsdIdentityPath path = Selector.Paths [i];
107                                 // Only "." hits.
108                                 if (depth == this.StartDepth) {
109                                         if (path.OrderedSteps.Length == 0)
110                                                 return path;
111                                         else
112                                                 continue;
113                                 }
114                                 // It does not hit as yet (too shallow to hit).
115                                 if (depth - this.StartDepth < path.OrderedSteps.Length - 1)
116                                         continue;
117
118                                 int iter = path.OrderedSteps.Length;
119                                 if (path.OrderedSteps [iter-1].IsAttribute)
120                                         iter--;
121
122                                 if (path.Descendants && depth < this.StartDepth + iter)
123                                         continue;
124                                 else if (!path.Descendants && depth != this.StartDepth + iter)
125                                         continue;
126
127                                 iter--;
128
129                                 XsdIdentityStep step;
130                                 for (int x = 0; 0 <= iter; x++, iter--) {
131                                         step = path.OrderedSteps [iter];
132                                         if (step.IsAnyName)
133                                                 continue;
134                                         XmlQualifiedName qname = (XmlQualifiedName) qnameStack [qnameStack.Count - x - 1];
135                                         if (step.NsName != null && qname.Namespace == step.NsName)
136                                                 continue;
137                                         if (step.Name == qname.Name && step.Namespace == qname.Namespace)
138                                                 continue;
139                                         if (alwaysTrue)
140                                                 break;
141                                 }
142                                 if (iter >= 0)  // i.e. did not match against the path.
143                                         continue;
144                                 return path;
145                         }
146                         return null;
147                 }
148         }
149 }