XsdKeyTable.cs 3.9 KB

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