Pattern.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. namespace Mono.Xml.XPath {
  18. public abstract class Pattern {
  19. internal static Pattern Compile (string s, Compiler comp)
  20. {
  21. return Compile (comp.parser.Compile (s));
  22. }
  23. internal static Pattern Compile (Expression e)
  24. {
  25. if (e is ExprUNION)
  26. return new UnionPattern (
  27. Compile (((ExprUNION)e).left),
  28. Compile (((ExprUNION)e).right)
  29. );
  30. if (e is ExprRoot)
  31. return new LocationPathPattern (
  32. new NodeTypeTest (Axes.Self, XPathNodeType.Root)
  33. );
  34. if (e is NodeTest)
  35. return new LocationPathPattern (
  36. (NodeTest)e
  37. );
  38. if (e is ExprFilter)
  39. return new LocationPathPattern (
  40. (ExprFilter)e
  41. );
  42. if (e is ExprSLASH)
  43. {
  44. Pattern p0 = Compile (((ExprSLASH)e).left);
  45. LocationPathPattern p1
  46. = (LocationPathPattern)Compile (((ExprSLASH)e).right);
  47. p1.SetPreviousPattern (p0, false);
  48. return p1;
  49. }
  50. if (e is ExprSLASH2)
  51. {
  52. if (((ExprSLASH2)e).right is ExprRoot)
  53. return Compile (((ExprSLASH2)e).left);
  54. Pattern p0 = Compile (((ExprSLASH2)e).left);
  55. LocationPathPattern p1
  56. = (LocationPathPattern)Compile (((ExprSLASH2)e).right);
  57. p1.SetPreviousPattern (p0, true);
  58. return p1;
  59. }
  60. // TODO: Handle ID/KEY
  61. throw new Exception ("Invalid Pattern");
  62. }
  63. public virtual double DefaultPriority { get { return 0.5; }}
  64. public abstract bool Matches (XPathNavigator node, XsltContext ctx);
  65. }
  66. }