2004-03-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / XPathBinder.cs
1 //
2 // System.Web.UI.XPathBinder
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 #if NET_1_2
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.Text;
14 using System.Xml.XPath;
15 using System.Xml;
16
17 namespace System.Web.UI {
18         public sealed class XPathBinder {
19                 private XPathBinder ()
20                 {
21                 }
22                 
23                 public static object Eval (object container, string xpath)
24                 {
25                         if (xpath == null || xpath.Length == 0)
26                                 throw new ArgumentNullException ("xpath");
27                         
28                         IXPathNavigable factory = container as IXPathNavigable;
29                         
30                         if (factory == null)
31                                 throw new ArgumentException ("container");
32                         
33                         object result = factory.CreateNavigator ().Evaluate (xpath);
34                         
35                         XPathNodeIterator itr = result as XPathNodeIterator;
36                         if (itr != null) {
37                                 if (itr.MoveNext())
38                                         return itr.Current.Value;
39                                 else
40                                         return null;
41                         }
42                         return result;
43                 }
44                 
45                 public static string Eval (object container, string xpath, string format)
46                 {
47                         object result = Eval (container, xpath);
48                         
49                         if (result == null)
50                                 return String.Empty;
51                         if (format == null || format.Length == 0)
52                                 return result.ToString ();
53                         
54                         return String.Format (format, result);
55                 }
56
57                 public static IEnumerable Select (object container, string xpath)
58                 {
59                         if (xpath == null || xpath.Length == 0)
60                                 throw new ArgumentNullException ("xpath");
61                         
62                         IXPathNavigable factory = container as IXPathNavigable;
63                         
64                         if (factory == null)
65                                 throw new ArgumentException ("container");
66                         
67                         XPathNodeIterator itr = factory.CreateNavigator ().Select (xpath);
68                         ArrayList ret = new ArrayList ();
69                         
70                         while (itr.MoveNext ()) {
71                                 IHasXmlNode nodeAccessor = itr.Current as IHasXmlNode;
72                                 if (nodeAccessor == null)
73                                         throw new InvalidOperationException ();
74                                 ret.Add (nodeAccessor.GetNode ());
75                         }
76                         return ret;
77                 }
78                 
79         }
80 }
81 #endif
82