This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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;\r
31 using System.Collections;\r
32 using System.Collections.Specialized;\r
33 using System.Xml;\r
34 using System.Xml.Schema;\r
35 \r
36 namespace Mono.Xml.Schema\r
37 {\r
38         // Created per constraining element.\r
39         internal class XsdKeyTable\r
40         {\r
41                 private XsdIdentitySelector selector;\r
42                 private XmlSchemaIdentityConstraint source;\r
43                 private XmlQualifiedName qname;\r
44                 private XmlQualifiedName refKeyName;\r
45 \r
46                 public ArrayList Entries = new ArrayList ();\r
47                 public ArrayList FinishedEntries = new ArrayList ();\r
48 \r
49                 public int StartDepth;\r
50                 public XsdKeyTable ReferencedKey;\r
51 \r
52                 public XsdKeyTable (XmlSchemaIdentityConstraint source, XmlReader reader)\r
53                 {\r
54                         Reset (source, reader);\r
55                 }\r
56 \r
57                 public XmlQualifiedName QualifiedName {\r
58                         get { return qname; }\r
59                 }\r
60 \r
61                 public XmlQualifiedName RefKeyName {\r
62                         get { return refKeyName; }\r
63                 }\r
64 \r
65                 public XmlSchemaIdentityConstraint SourceSchemaIdentity {\r
66                         get { return source; }\r
67                 }\r
68 \r
69                 public XsdIdentitySelector Selector {\r
70                         get { return selector; }\r
71                 }\r
72 \r
73                 public void Reset (XmlSchemaIdentityConstraint source, XmlReader reader)\r
74                 {\r
75                         this.source = source;\r
76                         this.selector = source.CompiledSelector;\r
77                         this.qname = source.QualifiedName;\r
78                         XmlSchemaKeyref kr = source as XmlSchemaKeyref;\r
79                         if (kr != null)\r
80                                 this.refKeyName = kr.Refer;\r
81                         StartDepth = 0;\r
82                 }\r
83 \r
84                 // In this method, attributes are ignored.\r
85                 public XsdIdentityPath SelectorMatches (ArrayList qnameStack, XmlReader reader)\r
86                 {\r
87                         for (int i = 0; i < Selector.Paths.Length; i++) {\r
88                                 XsdIdentityPath path = Selector.Paths [i];\r
89                                 // Only "." hits.\r
90                                 if (reader.Depth == this.StartDepth) {\r
91                                         if (path.OrderedSteps.Length == 0)\r
92                                                 return path;\r
93                                         else\r
94                                                 continue;\r
95                                 }\r
96                                 // It does not hit as yet (too shallow to hit).\r
97                                 if (reader.Depth - this.StartDepth < path.OrderedSteps.Length - 1)\r
98                                         continue;\r
99 \r
100                                 int iter = path.OrderedSteps.Length;\r
101                                 if (path.OrderedSteps [iter-1].IsAttribute)\r
102                                         iter--;\r
103 \r
104                                 if (path.Descendants && reader.Depth < this.StartDepth + iter)\r
105                                         continue;\r
106                                 else if (!path.Descendants && reader.Depth != this.StartDepth + iter)\r
107                                         continue;\r
108 \r
109                                 iter--;\r
110 \r
111                                 XsdIdentityStep step;\r
112                                 for (int x = 0; 0 <= iter; x++, iter--) {\r
113                                         step = path.OrderedSteps [iter];\r
114                                         if (step.IsAnyName)\r
115                                                 continue;\r
116                                         XmlQualifiedName qname = (XmlQualifiedName) qnameStack [qnameStack.Count - x - 1];\r
117                                         if (step.NsName != null && qname.Namespace == step.NsName)\r
118                                                 continue;\r
119                                         if (step.Name == qname.Name && step.Namespace == qname.Namespace)\r
120                                                 continue;\r
121                                         else\r
122                                                 break;\r
123                                 }\r
124                                 if (iter >= 0)  // i.e. did not match against the path.\r
125                                         continue;\r
126                                 return path;\r
127                         }\r
128                         return null;\r
129                 }\r
130         }\r
131 }\r