Pattern.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Mono.Xml.XPath.Pattern
  3. //
  4. // Author:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  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.IO;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using System.Xml.XPath;
  35. using System.Xml.Xsl;
  36. using Mono.Xml.Xsl;
  37. namespace Mono.Xml.XPath
  38. {
  39. internal abstract class Pattern
  40. {
  41. internal static Pattern Compile (string s, Compiler comp)
  42. {
  43. return Compile (comp.patternParser.Compile (s));
  44. }
  45. internal static Pattern Compile (Expression e)
  46. {
  47. if (e is ExprUNION)
  48. return new UnionPattern (
  49. Compile (((ExprUNION)e).left),
  50. Compile (((ExprUNION)e).right)
  51. );
  52. if (e is ExprRoot)
  53. return new LocationPathPattern (
  54. new NodeTypeTest (Axes.Self, XPathNodeType.Root)
  55. );
  56. if (e is NodeTest)
  57. return new LocationPathPattern (
  58. (NodeTest)e
  59. );
  60. if (e is ExprFilter)
  61. return new LocationPathPattern (
  62. (ExprFilter)e
  63. );
  64. if (e is ExprSLASH)
  65. {
  66. Pattern p0 = Compile (((ExprSLASH)e).left);
  67. LocationPathPattern p1
  68. = (LocationPathPattern)Compile (((ExprSLASH)e).right);
  69. p1.SetPreviousPattern (p0, false);
  70. return p1;
  71. }
  72. if (e is ExprSLASH2)
  73. {
  74. if (((ExprSLASH2)e).left is ExprRoot)
  75. return Compile (((ExprSLASH2)e).right);
  76. Pattern p0 = Compile (((ExprSLASH2)e).left);
  77. LocationPathPattern p1
  78. = (LocationPathPattern)Compile (((ExprSLASH2)e).right);
  79. p1.SetPreviousPattern (p0, true);
  80. return p1;
  81. }
  82. if (e is XPathFunctionId)
  83. {
  84. ExprLiteral id = ((XPathFunctionId) e).Id as ExprLiteral;
  85. return new IdPattern (id.Value);
  86. }
  87. if (e is XsltKey)
  88. {
  89. return new KeyPattern ((XsltKey) e);
  90. }
  91. return null; // throw Exception outer this method.
  92. }
  93. public virtual double DefaultPriority { get { return 0.5; }}
  94. public virtual XPathNodeType EvaluatedNodeType {
  95. get { return XPathNodeType.All; }
  96. }
  97. public abstract bool Matches (XPathNavigator node, XsltContext ctx);
  98. }
  99. }