2 // XmlDsigXPathTransform.cs -
3 // XmlDsigXPathTransform implementation for XML Signature
4 // http://www.w3.org/TR/1999/REC-xpath-19991116
7 // Sebastien Pouliot <sebastien@ximian.com>
8 // Atsushi Enomoto <atsushi@ximian.com>
10 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 // (C) 2004 Novell (http://www.novell.com)
14 using System.Collections;
18 using System.Xml.XPath;
21 namespace System.Security.Cryptography.Xml
24 // www.w3.org/TR/xmldsig-core/
25 // see Section 6.6.3 of the XMLDSIG specification
26 public class XmlDsigXPathTransform : Transform
29 private Type [] input;
30 private Type [] output;
31 private XmlNodeList xpath;
32 private XmlDocument doc;
33 private XsltContext ctx;
35 public XmlDsigXPathTransform ()
37 Algorithm = "http://www.w3.org/TR/1999/REC-xpath-19991116";
40 public override Type [] InputTypes {
44 // this way the result is cached if called multiple time
46 input [0] = typeof (System.IO.Stream);
47 input [1] = typeof (System.Xml.XmlDocument);
48 input [2] = typeof (System.Xml.XmlNodeList);
55 public override Type[] OutputTypes {
59 // this way the result is cached if called multiple time
60 output = new Type [1];
61 output [0] = typeof (System.Xml.XmlNodeList);
68 protected override XmlNodeList GetInnerXml ()
72 XmlDocument xpdoc = new XmlDocument ();
73 xpdoc.LoadXml ("<XPath xmlns=\"" + XmlSignature.NamespaceURI + "\"></XPath>");
74 xpath = xpdoc.ChildNodes;
79 [MonoTODO ("Evaluation of extension function here() results in different from MS.NET (is MS.NET really correct??).")]
80 public override object GetOutput ()
83 return new XmlDsigNodeList (new ArrayList ());
85 // evaluate every time since input or xpath might have changed.
87 for (int i = 0; i < xpath.Count; i++) {
88 switch (xpath [i].NodeType) {
89 case XmlNodeType.Text:
90 case XmlNodeType.CDATA:
91 case XmlNodeType.Element:
92 x += xpath [i].InnerText;
97 return doc.SelectNodes (x, ctx);
98 ctx = new XmlDsigXPathContext (doc);
99 return EvaluateMatch (doc, x);
102 public override object GetOutput (Type type)
104 if (type != typeof (XmlNodeList))
105 throw new ArgumentException ("type");
109 private XmlDsigNodeList EvaluateMatch (XmlNode n, string xpath)
111 ArrayList al = new ArrayList ();
112 // Strictly to say, document node is explicitly
113 // excluded by W3C spec (context node is initialized
114 // to the document root and XPath expression is
115 // "//. | //@* | //namespace::*)
116 XPathNavigator nav = n.CreateNavigator ();
117 XPathExpression exp = nav.Compile (xpath);
118 exp.SetContext (ctx);
119 EvaluateMatch (n, exp, al);
120 return new XmlDsigNodeList (al);
123 private void EvaluateMatch (XmlNode n, XPathExpression exp, ArrayList al)
125 if (NodeMatches (n, exp))
127 if (n.Attributes != null)
128 for (int i = 0; i < n.Attributes.Count; i++)
129 EvaluateMatch (n.Attributes [i], exp, al);
130 for (int i = 0; i < n.ChildNodes.Count; i++)
131 EvaluateMatch (n.ChildNodes [i], exp, al);
134 private bool NodeMatches (XmlNode n, XPathExpression exp)
136 // This looks waste of memory since it creates
137 // XPathNavigator every time, but even if we use
138 // XPathNodeIterator.Current, it also clones every time.
139 object ret = n.CreateNavigator ().Evaluate (exp);
143 double d = (double) ret;
144 return !(d == 0.0 || d == double.NaN);
147 return ((string) ret).Length > 0;
148 if (ret is XPathNodeIterator) {
149 XPathNodeIterator retiter = (XPathNodeIterator) ret;
150 return retiter.Count > 0;
155 public override void LoadInnerXml (XmlNodeList nodeList)
157 if (nodeList == null)
158 throw new CryptographicException ("nodeList");
162 public override void LoadInput (object obj)
164 // possible input: Stream, XmlDocument, and XmlNodeList
166 doc = new XmlDocument ();
167 doc.XmlResolver = GetResolver ();
168 doc.Load (obj as Stream);
170 else if (obj is XmlDocument) {
171 doc = (obj as XmlDocument);
173 else if (obj is XmlNodeList) {
174 doc = new XmlDocument ();
175 doc.XmlResolver = GetResolver ();
176 foreach (XmlNode xn in (obj as XmlNodeList)) {
177 XmlNode importedNode = doc.ImportNode (xn, true);
178 doc.AppendChild (importedNode);
183 // Internal classes to support XPath extension function here()
185 internal class XmlDsigXPathContext : XsltContext
187 XmlDsigXPathFunctionHere here;
188 public XmlDsigXPathContext (XmlNode node)
190 here = new XmlDsigXPathFunctionHere (node);
193 public override IXsltContextFunction ResolveFunction (
194 string prefix, string name, XPathResultType [] argType)
196 // Here MS.NET incorrectly allows arbitrary
197 // name e.g. "heretic()".
198 if (name == "here" &&
199 prefix == String.Empty &&
206 public override bool Whitespace {
210 public override bool PreserveWhitespace (XPathNavigator node)
215 public override int CompareDocument (string s1, string s2)
217 return String.Compare (s1, s2);
220 public override IXsltContextVariable ResolveVariable (string prefix, string name)
222 throw new InvalidOperationException ();
226 internal class XmlDsigXPathFunctionHere : IXsltContextFunction
230 static XPathResultType [] types;
231 static XmlDsigXPathFunctionHere ()
233 types = new XPathResultType [0];
238 XPathNodeIterator xpathNode;
240 public XmlDsigXPathFunctionHere (XmlNode node)
242 xpathNode = node.CreateNavigator ().Select (".");
245 public XPathResultType [] ArgTypes {
246 get { return types; }
249 public int Maxargs { get { return 0; } }
251 public int Minargs { get { return 0; } }
253 public XPathResultType ReturnType {
254 get { return XPathResultType.NodeSet; }
257 public object Invoke (XsltContext ctx, object [] args, XPathNavigator docContext)
259 if (args.Length != 0)
260 throw new ArgumentException ("Not allowed arguments for function here().", "args");
262 return xpathNode.Clone ();