forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPathQueryManager.cs
More file actions
881 lines (757 loc) · 31.6 KB
/
XPathQueryManager.cs
File metadata and controls
881 lines (757 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
// WFS provider by Peter Robineau (peter.robineau@gmx.at)
// This file can be redistributed and/or modified under the terms of the GNU Lesser General Public License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace SharpMap.Utilities.Wfs
{
/// <summary>
/// This class provides an easy-to-use interface for complex (parameterized) XPath queries.
/// </summary>
public class XPathQueryManager : IXPathQueryManager
{
#region Fields
private int _NavDiff = -1;
private CustomQueryContext _ParamContext;
private XPathNodeIterator _XIter;
private XPathNavigator _XNav;
private XPathDocument _XPathDoc;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
public XPathQueryManager()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="documentStream">A Stream with XML data</param>
public XPathQueryManager(Stream documentStream)
{
SetDocumentToParse(documentStream);
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="document">A byte array with XML data</param>
public XPathQueryManager(byte[] document)
{
SetDocumentToParse(document);
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="xPathDoc">An XmlDocument instance</param>
public XPathQueryManager(XPathDocument xPathDoc)
{
SetDocumentToParse(xPathDoc);
_ParamContext = new CustomQueryContext(new NameTable());
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="httpClientUtil">A configured <see cref="HttpClientUtil"/> instance for performing web requests</param>
public XPathQueryManager(HttpClientUtil httpClientUtil)
{
SetDocumentToParse(httpClientUtil);
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="fileName"></param>
public XPathQueryManager(string fileName)
{
SetDocumentToParse(fileName);
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="xPathDoc">An XmlDocument instance</param>
/// <param name="xNav">An <see cref="XPathNavigator"/> instance</param>
/// <param name="paramContext">A <see cref="XPathQueryManager.CustomQueryContext"/> instance for parameterized XPath expressions</param>
private XPathQueryManager(XPathDocument xPathDoc, XPathNavigator xNav, CustomQueryContext paramContext)
{
_XNav = xNav.Clone();
SetDocumentToParse(xPathDoc);
InitializeCustomContext(paramContext);
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathQueryManager"/> class.
/// </summary>
/// <param name="xPathDoc">An XmlDocument instance</param>
/// <param name="xIter">An XPathNodeIterator instance</param>
/// <param name="paramContext">A <see cref="XPathQueryManager.CustomQueryContext"/> instance for parameterized XPath expressions</param>
private XPathQueryManager(XPathDocument xPathDoc, XPathNodeIterator xIter, CustomQueryContext paramContext)
: this(xPathDoc)
{
if (xIter != null)
_XNav = xIter.Current;
InitializeCustomContext(paramContext);
}
#endregion
#region IXPathQueryManager Member
/// <summary>
/// This method adds a namespace for XPath queries.
/// </summary>
/// <param name="prefix">The namespace prefix</param>
/// <param name="ns">The namespace URI</param>
public void AddNamespace(string prefix, string ns)
{
_ParamContext.AddNamespace(prefix, ns);
}
/// <summary>
/// This method compiles an XPath string.
/// </summary>
/// <param name="xPath">The XPath string</param>
/// <returns>A compiled XPath expression</returns>
public XPathExpression Compile(string xPath)
{
return _XNav.Compile(xPath);
}
/// <summary>
/// This method returns a clone of the current instance.
/// The cloned instance operates on the same (read-only) XmlDocument instance.
/// </summary>
public IXPathQueryManager Clone()
{
return new XPathQueryManager(_XPathDoc, _XNav, _ParamContext);
}
/// <summary>
/// This method returns an XPathNodeIterator instance positioned at the nodes
/// the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
public XPathNodeIterator GetIterator(XPathExpression xPath)
{
findXPath(xPath);
return _XIter.Clone();
}
/// <summary>
/// This method returns an XPathNodeIterator instance positioned at the nodes
/// the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
/// <param name="queryParameters">Parameters for the compiled XPath expression</param>
public XPathNodeIterator GetIterator(XPathExpression xPath, DictionaryEntry[] queryParameters)
{
_ParamContext.AddParam(queryParameters);
return GetIterator(xPath);
}
/// <summary>
/// This method returns the value of the first node the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
public string GetValueFromNode(XPathExpression xPath)
{
string result = null;
findXPath(xPath);
if (_XIter.MoveNext())
result = _XIter.Current.Value;
return result;
}
/// <summary>
/// This method returns the value of the first node the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
/// <param name="queryParameters">Parameters for the compiled XPath expression</param>
public string GetValueFromNode(XPathExpression xPath, DictionaryEntry[] queryParameters)
{
_ParamContext.AddParam(queryParameters);
return GetValueFromNode(xPath);
}
/// <summary>
/// This method returns a collection of the values of all nodes the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
public List<string> GetValuesFromNodes(XPathExpression xPath)
{
List<string> valuesList = new List<string>();
findXPath(xPath);
while (_XIter.MoveNext())
valuesList.Add(_XIter.Current.ToString());
return valuesList;
}
/// <summary>
/// This method returns a collection of the values of all nodes the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
/// <param name="queryParameters">Parameters for the compiled XPath expression</param>
public List<string> GetValuesFromNodes(XPathExpression xPath, DictionaryEntry[] queryParameters)
{
_ParamContext.AddParam(queryParameters);
return GetValuesFromNodes(xPath);
}
/// <summary>
/// This method returns an instance of <see cref="XPathQueryManager"/>
/// in the context of the first node the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
public IXPathQueryManager GetXPathQueryManagerInContext(XPathExpression xPath)
{
findXPath(xPath);
if (_XIter.MoveNext())
return new XPathQueryManager(_XPathDoc, _XIter, _ParamContext);
return null;
}
/// <summary>
/// This method returns an instance of <see cref="XPathQueryManager"/>
/// in the context of the first node the XPath expression addresses.
/// </summary>
/// <param name="xPath">The compiled XPath expression</param>
/// <param name="queryParameters">Parameters for the compiled XPath expression</param>
public IXPathQueryManager GetXPathQueryManagerInContext(XPathExpression xPath, DictionaryEntry[] queryParameters)
{
_ParamContext.AddParam(queryParameters);
findXPath(xPath);
if (_XIter.MoveNext())
return new XPathQueryManager(_XPathDoc, _XIter, _ParamContext);
return null;
}
/// <summary>
/// This method moves the current instance of <see cref="XPathQueryManager"/>
/// to the context of the next node a previously handed over XPath expression addresses.
/// </summary>
public bool GetContextOfNextNode()
{
return GetContextOfNode((uint) _NavDiff + 1);
}
/// <summary>
/// This method moves the current instance of <see cref="XPathQueryManager"/>
/// to the context of node[index] of current position.
/// </summary>
/// <param name="index">The index of the node to search</param>
public bool GetContextOfNode(uint index)
{
if (_NavDiff == -1) ++_NavDiff;
while (_NavDiff < index)
{
if (!_XNav.MoveToNext()) break;
_NavDiff++;
}
while (_NavDiff > index)
{
_XNav.MoveToPrevious();
_NavDiff--;
}
if (_NavDiff == index)
return true;
else
{
ResetNavigator();
return false;
}
}
/// <summary>
/// This method deletes the current namespace context.
/// </summary>
public void ResetNamespaces()
{
_ParamContext = null;
}
/// <summary>
/// This method resets the inherent XPathNavigator instance.
/// </summary>
public void ResetNavigator()
{
GetContextOfNode(0);
_NavDiff--;
}
/// <summary>
/// Sets a new XML document.
/// </summary>
/// <param name="documentStream">A Stream with XML data</param>
public void SetDocumentToParse(Stream documentStream)
{
initializeXPathObjects(documentStream);
}
/// <summary>
/// Sets a new XML document.
/// </summary>
/// <param name="document">A byte array with XML data</param>
public void SetDocumentToParse(byte[] document)
{
initializeXPathObjects(new MemoryStream(document));
}
/// <summary>
/// Sets a new XML document.
/// </summary>
/// <param name="httpClientUtil">A configured <see cref="HttpClientUtil"/> instance for performing web requests</param>
public void SetDocumentToParse(HttpClientUtil httpClientUtil)
{
try
{
initializeXPathObjects(httpClientUtil.GetDataStream());
}
finally
{
httpClientUtil.Close();
}
}
/// <summary>
/// Sets a new XmlDocument
/// </summary>
/// <param name="fileName"></param>
public void SetDocumentToParse(string fileName)
{
try
{
initializeXPathObjects(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));
}
catch (Exception ex)
{
Trace.TraceError("An exception occured while reading the xml file: " + fileName);
throw ex;
}
}
#endregion
#region Private Member
/// <summary>
/// Sets a new XML document.
/// </summary>
/// <param name="xPathDoc">An XPathDocument instance</param>
private void SetDocumentToParse(XPathDocument xPathDoc)
{
_XPathDoc = xPathDoc;
if (_XNav == null) _XNav = _XPathDoc.CreateNavigator().Clone();
}
/// <summary>
/// This method does some XPath specific initializations.
/// </summary>
private void initializeXPathObjects(Stream xmlStream)
{
try
{
_XPathDoc = new XPathDocument(xmlStream);
_XNav = _XPathDoc.CreateNavigator();
_ParamContext = new CustomQueryContext(new NameTable());
}
catch (XmlException ex)
{
Trace.TraceError("An XML specific exception occured " +
"while initializing XPathDocument and XPathNavigator in XPathQueryManager: " +
ex.Message);
throw ex;
}
catch (Exception ex)
{
Trace.TraceError("An exception occured " +
"while initializing XPathDocument and XPathNavigator in XPathQueryManager: " +
ex.Message);
throw ex;
}
finally
{
xmlStream.Dispose();
}
}
/// <summary>
/// This method sets the inherent XPathNodeIterator instance.
/// </summary>
/// <param name="xPath">A compiled XPath expression</param>
private void findXPath(XPathExpression xPath)
{
xPath.SetContext(_ParamContext);
_XIter = _XNav.Select(xPath);
InitializeCustomContext(_ParamContext);
}
private void InitializeCustomContext(CustomQueryContext paramContext)
{
IDictionary<string, string> namespaces = paramContext.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
_ParamContext = new CustomQueryContext((NameTable) paramContext.NameTable);
_ParamContext.AddNamespace(namespaces);
}
#endregion
#region Nested Types
#region CustomQueryContext
/// <summary>
/// This class represents a custom context for XPath queries.
/// It is derived from XsltContext.
/// </summary>
public class CustomQueryContext : XsltContext
{
#region Fields
private XsltArgumentList _ArgumentList = new XsltArgumentList();
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CustomQueryContext"/> class.
/// </summary>
public CustomQueryContext()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomQueryContext"/> class.
/// </summary>
/// <param name="table">A NameTable instance</param>
public CustomQueryContext(NameTable table)
: base(table)
{
}
#endregion
#region Public Member
/// <summary>
/// Method from XsltContext.
/// </summary>
public override bool Whitespace
{
get { return true; }
}
/// <summary>
/// This method adds a list of namespaces to use in the custom context.
/// </summary>
/// <param name="namespaces">A list of namespaces</param>
public void AddNamespace(IDictionary<string, string> namespaces)
{
foreach (string ns in namespaces.Keys)
AddNamespace(ns, namespaces[ns]);
}
/// <summary>
/// Method from XsltContext.
/// </summary>
public override int CompareDocument(string baseUri, string nextbaseUri)
{
return 0;
}
/// <summary>
/// Method from XsltContext.
/// </summary>
public override bool PreserveWhitespace(XPathNavigator node)
{
return true;
}
/// <summary>
/// This method resolves a function appearing in an XPath expression.
/// </summary>
/// <param name="prefix">The prefix of the function</param>
/// <param name="name">The name of the function</param>
/// <param name="ArgTypes">A list of argument types of the function</param>
public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] ArgTypes)
{
if (name.Equals(ParamCompare.FunctionName)) return new ParamCompare(ArgTypes, 2, 2);
if (name.Equals(ParamCompareWithTargetNs.FunctionName))
return new ParamCompareWithTargetNs(ArgTypes, 3, 3);
return null;
}
/// <summary>
/// This method resolves a variable appearing in an XPath expression.
/// </summary>
/// <param name="prefix">The prefix of the variable</param>
/// <param name="name">The name of the variable</param>
public override IXsltContextVariable ResolveVariable(string prefix, string name)
{
object param = GetParam(name);
if (param != null)
return new ParamFunctionVar(name, param);
return null;
}
/// <summary>
/// This method adds a parameter to the custom context.
/// </summary>
/// <param name="name">The name of the parameter</param>
/// <param name="parameter">The value of the parameter</param>
public void AddParam(string name, object parameter)
{
_ArgumentList.AddParam(name, string.Empty, parameter);
}
/// <summary>
/// This method adds a list of parameters to the custom context.
/// </summary>
/// <param name="parameters">A list of parameters</param>
public void AddParam(DictionaryEntry[] parameters)
{
int length = parameters.Length;
for (int i = 0; i < length; i++)
_ArgumentList.AddParam(parameters[i].Key.ToString(),
string.Empty, parameters[i].Value.ToString());
}
/// <summary>
/// This method gets a parameter by name.
/// </summary>
/// <param name="name">The name of the parameter</param>
public object GetParam(string name)
{
return _ArgumentList.GetParam(name, string.Empty);
}
/// <summary>
/// This method removes a parameter from the inherent parameter list.
/// </summary>
/// <param name="name">The name of the parameter</param>
public object RemoveParam(string name)
{
return _ArgumentList.RemoveParam(name, string.Empty);
}
/// <summary>
/// This methods clears the inherent parameter list.
/// </summary>
public void ResetParams()
{
_ArgumentList.Clear();
}
#endregion
}
#endregion
#region ParamBase
/// <summary>
/// This class is the base class of <see cref="ParamCompare"/> and <see cref="ParamCompareWithTargetNs"/>.
/// </summary>
public abstract class ParamBase
{
#region Fields
private XPathResultType[] _ArgTypes;
private int _MaxArgs;
private int _MinArgs;
private XPathResultType _ReturnType;
#endregion
#region Properties
/// <summary>
/// Gets the argument types.
/// </summary>
public XPathResultType[] ArgTypes
{
get { return _ArgTypes; }
}
/// <summary>
/// Gets the return type.
/// </summary>
public XPathResultType ReturnType
{
get { return _ReturnType; }
}
/// <summary>
/// Gets the minimum number of arguments allowed.
/// </summary>
public int Minargs
{
get { return _MinArgs; }
}
/// <summary>
/// Gets the maximum number of arguments allowed.
/// </summary>
public int Maxargs
{
get { return _MaxArgs; }
}
#endregion
#region Constructors
/// <summary>
/// Protected constructor for the abstract class.
/// </summary>
/// <param name="argTypes">The argument types of the function</param>
/// <param name="returnType">The return type of the function</param>
/// <param name="minArgs">The minimum number of arguments allowed</param>
/// <param name="maxArgs">The maximum number of arguments allowed</param>
protected ParamBase(XPathResultType[] argTypes, XPathResultType returnType,
int minArgs, int maxArgs)
{
_ArgTypes = argTypes;
_ReturnType = returnType;
_MinArgs = minArgs;
_MaxArgs = maxArgs;
}
#endregion
}
#endregion
#region ParamCompare
/// <summary>
/// This class performs a string comparison in an XPath expression.
/// </summary>
public class ParamCompare : ParamBase, IXsltContextFunction
{
#region Fields
/// <summary>
/// The name to use when embedding the function in an XPath expression.
/// </summary>
public static readonly string FunctionName = "_PARAMCOMP_";
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ParamCompare"/> class.
/// </summary>
/// <param name="argTypes">The argument types of the function</param>
/// <param name="minArgs">The minimum number of arguments allowed</param>
/// <param name="maxArgs">The maximum number of arguments allowed</param>
public ParamCompare(XPathResultType[] argTypes, int minArgs, int maxArgs)
: base(argTypes, XPathResultType.Boolean, minArgs, maxArgs)
{
}
#endregion
#region IXsltContextFunction Member
/// <summary>
/// This method performs a string comparison.
/// </summary>
/// <param name="xsltContext">The Xslt context</param>
/// <param name="args">The arguments of the function</param>
/// <param name="docContext">The document context</param>
/// <returns>A boolean value indicating whether the argument strings are identical</returns>
public virtual object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
{
return resolveNsPrefix(ResolveArgument(args[0]), xsltContext).Equals(
resolveNsPrefix(ResolveArgument(args[1]), xsltContext), StringComparison.Ordinal);
}
#endregion
#region Protected Member
/// <summary>
/// This method creates a string from an object argument.
/// In many cases the argument is an XPathNodeIterator that must be resolved.
/// </summary>
/// <param name="arg">An argument of the function to be resolved</param>
protected string ResolveArgument(object arg)
{
if (arg is string)
return arg.ToString();
else if (arg is XPathNodeIterator)
if (((XPathNodeIterator) arg).MoveNext() == true)
return ((XPathNodeIterator) arg).Current.Value;
return string.Empty;
}
#endregion
#region Private Member
/// <summary>
/// This method resolves the prefix of an argument.
/// If a prefix is found, the corresponding namespace URI is looked up
/// and substituted.
/// </summary>
/// <param name="args">An argument of the function to be resolved</param>
/// <param name="xsltContext">The Xslt context for namespace resolving</param>
private string resolveNsPrefix(string args, XsltContext xsltContext)
{
string prefix;
string ns;
if (args.Contains(":"))
{
prefix = args.Substring(0, args.IndexOf(":"));
if (!string.IsNullOrEmpty((ns = xsltContext.LookupNamespace(prefix))))
args = args.Replace(prefix + ":", ns);
}
return args;
}
#endregion
}
#endregion
#region ParamCompareWithTargetNs
/// <summary>
/// This class performs a string comparison in an XPath expression.
/// It is specifically created for using in XML schema documents.
/// </summary>
public class ParamCompareWithTargetNs : ParamCompare
{
#region Fields
/// <summary>
/// The name to use when embedding the function in an XPath expression.
/// </summary>
public static new readonly string FunctionName = "_PARAMCOMPWITHTARGETNS_";
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ParamCompareWithTargetNs"/> class.
/// </summary>
/// <param name="argTypes">The argument types of the function</param>
/// <param name="minArgs">The minimum number of arguments allowed</param>
/// <param name="maxArgs">The maximum number of arguments allowed</param>
public ParamCompareWithTargetNs(XPathResultType[] argTypes, int minArgs, int maxArgs)
: base(argTypes, minArgs, maxArgs)
{
}
#endregion
#region IXsltContextFunction Member
/// <summary>
/// This method performs a string comparison.
/// </summary>
/// <param name="xsltContext">The Xslt context</param>
/// <param name="args">The arguments of the function</param>
/// <param name="docContext">The document context</param>
/// <returns>A boolean value indicating whether the argument strings are identical</returns>
public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
{
return ((string) ((string) args[1] + ResolveArgument(args[2]))).Equals(
ResolveNsPrefix(ResolveArgument(args[0]), (string) args[1], docContext), StringComparison.Ordinal);
}
#endregion
#region Private Member
/// <summary>
/// This method resolves the prefix of an argument.
/// If a prefix is found, the corresponding namespace URI (if existing) is looked up
/// and substituted. Otherwise the target namespace is placed first.
/// </summary>
/// <param name="args">An argument of the function to be resolved</param>
/// <param name="targetNs">The target namespace</param>
/// <param name="docContext">The document context</param>
private static string ResolveNsPrefix(string args, string targetNs, XPathNavigator docContext)
{
if (args.Contains(":"))
{
var prefix = args.Substring(0, args.IndexOf(":", StringComparison.Ordinal));
string ns;
if (!string.IsNullOrEmpty((ns = docContext.LookupNamespace(prefix))))
return args.Replace(prefix + ":", ns);
return targetNs + args;
}
return targetNs + args;
}
#endregion
}
#endregion
#region ParamFunctionVar
/// <summary>
/// This class represents a variable in an XPath expression.
/// </summary>
public class ParamFunctionVar : IXsltContextVariable
{
#region Fields
private string _Name;
private object _Param;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ParamFunctionVar"/> class.
/// </summary>
/// <param name="name">The name of the variable</param>
/// <param name="param">The parameter</param>
public ParamFunctionVar(string name, object param)
{
_Name = name;
_Param = param;
}
#endregion
#region IXsltContextVariable Member
/// <summary>
/// Method implementing IXsltContextVariable
/// </summary>
public object Evaluate(XsltContext xsltContext)
{
return _Param;
}
/// <summary>
/// Method implementing IXsltContextVariable
/// </summary>
public bool IsLocal
{
get { return true; }
}
/// <summary>
/// Method implementing IXsltContextVariable
/// </summary>
public bool IsParam
{
get { return true; }
}
/// <summary>
/// Method implementing IXsltContextVariable
/// </summary>
public XPathResultType VariableType
{
get { return XPathResultType.Any; }
}
#endregion
}
#endregion
#endregion
}
}