Pattern.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Mono.Xml.XPath.Pattern
  3. //
  4. // Author:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.XPath;
  15. using System.Xml.Xsl;
  16. using Mono.Xml.Xsl;
  17. using Mono.Xml.Xsl.Functions;
  18. namespace Mono.Xml.XPath {
  19. public abstract class Pattern {
  20. internal static Pattern Compile (string s, Compiler comp)
  21. {
  22. return Compile (comp.parser.Compile (s));
  23. }
  24. internal static Pattern Compile (Expression e)
  25. {
  26. if (e is ExprUNION)
  27. return new UnionPattern (
  28. Compile (((ExprUNION)e).left),
  29. Compile (((ExprUNION)e).right)
  30. );
  31. if (e is ExprRoot)
  32. return new LocationPathPattern (
  33. new NodeTypeTest (Axes.Self, XPathNodeType.Root)
  34. );
  35. if (e is NodeTest)
  36. return new LocationPathPattern (
  37. (NodeTest)e
  38. );
  39. if (e is ExprFilter)
  40. return new LocationPathPattern (
  41. (ExprFilter)e
  42. );
  43. if (e is ExprSLASH)
  44. {
  45. Pattern p0 = Compile (((ExprSLASH)e).left);
  46. LocationPathPattern p1
  47. = (LocationPathPattern)Compile (((ExprSLASH)e).right);
  48. p1.SetPreviousPattern (p0, false);
  49. return p1;
  50. }
  51. if (e is ExprSLASH2)
  52. {
  53. if (((ExprSLASH2)e).right is ExprRoot)
  54. return Compile (((ExprSLASH2)e).left);
  55. Pattern p0 = Compile (((ExprSLASH2)e).left);
  56. LocationPathPattern p1
  57. = (LocationPathPattern)Compile (((ExprSLASH2)e).right);
  58. p1.SetPreviousPattern (p0, true);
  59. return p1;
  60. }
  61. if (e is XPathFunctionId)
  62. {
  63. ExprLiteral id = ((XPathFunctionId) e).Id as ExprLiteral;
  64. return new IdPattern (id.Value);
  65. }
  66. if (e is XsltKey)
  67. {
  68. return new KeyPattern ((XsltKey) e);
  69. }
  70. throw new Exception ("Invalid Pattern");
  71. }
  72. public virtual double DefaultPriority { get { return 0.5; }}
  73. public abstract bool Matches (XPathNavigator node, XsltContext ctx);
  74. }
  75. }