XsdKeyTable.cs 4.1 KB

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