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