Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XsltCompileContext.cs
Go to the documentation of this file.
8
10
11internal sealed class XsltCompileContext : XsltContext
12{
13 private abstract class XsltFunctionImpl : IXsltContextFunction
14 {
15 private int _minargs;
16
17 private int _maxargs;
18
20
22
23 public int Minargs => _minargs;
24
25 public int Maxargs => _maxargs;
26
28
30
32 {
33 }
34
35 public XsltFunctionImpl(int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
36 {
37 Init(minArgs, maxArgs, returnType, argTypes);
38 }
39
40 [MemberNotNull("_argTypes")]
41 protected void Init(int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
42 {
43 _minargs = minArgs;
44 _maxargs = maxArgs;
46 _argTypes = argTypes;
47 }
48
49 public abstract object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext);
50
52 {
53 if (!(argument is XPathNodeIterator result))
54 {
56 }
57 return result;
58 }
59
60 public static XPathNavigator ToNavigator(object argument)
61 {
62 if (!(argument is XPathNavigator result))
63 {
65 }
66 return result;
67 }
68
69 private static string IteratorToString(XPathNodeIterator it)
70 {
71 if (it.MoveNext())
72 {
73 return it.Current.Value;
74 }
75 return string.Empty;
76 }
77
78 [return: NotNullIfNotNull("argument")]
79 public static string ToString(object argument)
80 {
82 {
83 return IteratorToString(it);
84 }
86 }
87
100
101 public static double ToNumber(object argument)
102 {
104 {
106 }
108 {
109 return XmlConvert.ToXPathDouble(xPathNavigator.ToString());
110 }
112 }
113
114 private static object ToNumeric(object argument, Type type)
115 {
117 }
118
119 public static object ConvertToXPathType(object val, XPathResultType xt, Type type)
120 {
121 switch (xt)
122 {
123 case XPathResultType.String:
124 if (type == typeof(string))
125 {
126 return ToString(val);
127 }
128 return ToNavigator(val);
129 case XPathResultType.Number:
130 return ToNumeric(val, type);
131 case XPathResultType.Boolean:
132 return ToBoolean(val);
133 case XPathResultType.NodeSet:
134 return ToIterator(val);
135 case XPathResultType.Any:
136 case XPathResultType.Error:
137 return val;
138 default:
139 return val;
140 }
141 }
142 }
143
144 private sealed class FuncCurrent : XsltFunctionImpl
145 {
146 public FuncCurrent()
148 {
149 }
150
151 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
152 {
153 return ((XsltCompileContext)xsltContext).Current();
154 }
155 }
156
157 private sealed class FuncUnEntityUri : XsltFunctionImpl
158 {
163
164 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
165 {
166 throw XsltException.Create(System.SR.Xslt_UnsuppFunction, "unparsed-entity-uri");
167 }
168 }
169
170 private sealed class FuncGenerateId : XsltFunctionImpl
171 {
176
177 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
178 {
179 if (args.Length != 0)
180 {
182 if (xPathNodeIterator.MoveNext())
183 {
184 return xPathNodeIterator.Current.UniqueId;
185 }
186 return string.Empty;
187 }
188 return docContext.UniqueId;
189 }
190 }
191
192 private sealed class FuncSystemProp : XsltFunctionImpl
193 {
198
199 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
200 {
201 return ((XsltCompileContext)xsltContext).SystemProperty(XsltFunctionImpl.ToString(args[0]));
202 }
203 }
204
206 {
211
212 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
213 {
214 return ((XsltCompileContext)xsltContext).ElementAvailable(XsltFunctionImpl.ToString(args[0]));
215 }
216 }
217
219 {
224
225 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
226 {
227 return ((XsltCompileContext)xsltContext).FunctionAvailable(XsltFunctionImpl.ToString(args[0]));
228 }
229 }
230
231 private sealed class FuncDocument : XsltFunctionImpl
232 {
235 {
236 XPathResultType.Any,
238 })
239 {
240 }
241
242 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
243 {
244 string baseUri = null;
245 if (args.Length == 2)
246 {
248 baseUri = ((!xPathNodeIterator.MoveNext()) ? string.Empty : xPathNodeIterator.Current.BaseURI);
249 }
250 try
251 {
252 return ((XsltCompileContext)xsltContext).Document(args[0], baseUri);
253 }
254 catch (Exception e)
255 {
257 {
258 throw;
259 }
261 }
262 }
263 }
264
265 private sealed class FuncKey : XsltFunctionImpl
266 {
267 public FuncKey()
269 {
270 XPathResultType.String,
272 })
273 {
274 }
275
276 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
277 {
280 string ns = xsltContext.LookupNamespace(prefix);
283 xPathNavigator.MoveToRoot();
284 ArrayList arrayList = null;
285 Key[] keyList = xsltCompileContext._processor.KeyList;
286 foreach (Key key in keyList)
287 {
288 if (!(key.Name == xmlQualifiedName))
289 {
290 continue;
291 }
292 Hashtable hashtable = key.GetKeys(xPathNavigator);
293 if (hashtable == null)
294 {
295 hashtable = xsltCompileContext.BuildKeyTable(key, xPathNavigator);
296 key.AddKey(xPathNavigator, hashtable);
297 }
299 {
301 while (xPathNodeIterator2.MoveNext())
302 {
303 arrayList = AddToList(arrayList, (ArrayList)hashtable[xPathNodeIterator2.Current.Value]);
304 }
305 }
306 else
307 {
309 }
310 }
311 if (arrayList == null)
312 {
314 }
316 {
317 return new XPathArrayIterator(arrayList);
318 }
319 return new XPathMultyIterator(arrayList);
320 }
321
323 {
324 if (newList == null)
325 {
326 return resultCollection;
327 }
328 if (resultCollection == null)
329 {
330 return newList;
331 }
332 if (!(resultCollection[0] is ArrayList))
333 {
337 }
339 return resultCollection;
340 }
341 }
342
343 private sealed class FuncFormatNumber : XsltFunctionImpl
344 {
347 {
348 XPathResultType.Number,
351 })
352 {
353 }
354
355 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
356 {
357 DecimalFormat decimalFormat = ((XsltCompileContext)xsltContext).ResolveFormatName((args.Length == 3) ? XsltFunctionImpl.ToString(args[2]) : null);
359 }
360 }
361
362 private sealed class FuncNodeSet : XsltFunctionImpl
363 {
368
369 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
370 {
372 }
373 }
374
375 private sealed class FuncExtension : XsltFunctionImpl
376 {
377 private readonly object _extension;
378
379 private readonly MethodInfo _method;
380
381 private readonly Type[] _types;
382
384 {
386 _method = method;
388 ParameterInfo[] parameters = method.GetParameters();
389 int num = parameters.Length;
390 int maxArgs = parameters.Length;
391 _types = new Type[parameters.Length];
392 XPathResultType[] array = new XPathResultType[parameters.Length];
393 bool flag = true;
394 int num2 = parameters.Length - 1;
395 while (0 <= num2)
396 {
397 _types[num2] = parameters[num2].ParameterType;
398 array[num2] = GetXPathType(parameters[num2].ParameterType);
399 if (flag)
400 {
401 if (parameters[num2].IsOptional)
402 {
403 num--;
404 }
405 else
406 {
407 flag = false;
408 }
409 }
410 num2--;
411 }
412 Init(num, maxArgs, xPathType, array);
413 }
414
415 public override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
416 {
417 int num = args.Length - 1;
418 while (0 <= num)
419 {
420 args[num] = XsltFunctionImpl.ConvertToXPathType(args[num], base.ArgTypes[num], _types[num]);
421 num--;
422 }
423 return _method.Invoke(_extension, args);
424 }
425 }
426
428
430
431 private static readonly Hashtable s_FunctionTable = CreateFunctionTable();
432
433 private static readonly IXsltContextFunction s_FuncNodeSet = new FuncNodeSet();
434
435 public override string DefaultNamespace => string.Empty;
436
438
445
447 : base(dummy: false)
448 {
449 }
450
456
457 public override int CompareDocument(string baseUri, string nextbaseUri)
458 {
459 return string.Compare(baseUri, nextbaseUri, StringComparison.Ordinal);
460 }
461
462 public override string LookupNamespace(string prefix)
463 {
465 }
466
478
480 {
482 if (variableValue == null && !variable.IsGlobal)
483 {
485 if (variableAction != null)
486 {
488 }
489 }
490 if (variableValue == null)
491 {
493 }
494 return variableValue;
495 }
496
498 {
499 node = node.Clone();
500 node.MoveToParent();
502 }
503
504 private MethodInfo FindBestMethod(MethodInfo[] methods, bool ignoreCase, bool publicOnly, string name, XPathResultType[] argTypes)
505 {
506 int num = methods.Length;
507 int num2 = 0;
508 for (int i = 0; i < num; i++)
509 {
510 if (string.Equals(name, methods[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) && (!publicOnly || methods[i].GetBaseDefinition().IsPublic))
511 {
512 methods[num2++] = methods[i];
513 }
514 }
515 num = num2;
516 if (num == 0)
517 {
518 return null;
519 }
520 if (argTypes == null)
521 {
522 return methods[0];
523 }
524 num2 = 0;
525 for (int j = 0; j < num; j++)
526 {
527 if (methods[j].GetParameters().Length == argTypes.Length)
528 {
529 methods[num2++] = methods[j];
530 }
531 }
532 num = num2;
533 if (num <= 1)
534 {
535 return methods[0];
536 }
537 num2 = 0;
538 for (int k = 0; k < num; k++)
539 {
540 bool flag = true;
541 ParameterInfo[] parameters = methods[k].GetParameters();
542 for (int l = 0; l < parameters.Length; l++)
543 {
546 {
547 XPathResultType xPathType = GetXPathType(parameters[l].ParameterType);
549 {
550 flag = false;
551 break;
552 }
553 }
554 }
555 if (flag)
556 {
557 methods[num2++] = methods[k];
558 }
559 }
560 num = num2;
561 return methods[0];
562 }
563
564 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:RequiresUnreferencedCode", Justification = "In order for this code path to be hit, a previous call to XsltArgumentList.AddExtensionObject is required. That method is already annotated as unsafe and throwing a warning, so we can suppress here.")]
565 private IXsltContextFunction GetExtentionMethod(string ns, string name, XPathResultType[] argTypes, out object extension)
566 {
567 FuncExtension result = null;
569 if (extension != null)
570 {
571 MethodInfo methodInfo = FindBestMethod(extension.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic), ignoreCase: true, publicOnly: false, name, argTypes);
572 if (methodInfo != null)
573 {
574 result = new FuncExtension(extension, methodInfo);
575 }
576 return result;
577 }
579 if (extension != null)
580 {
581 MethodInfo methodInfo2 = FindBestMethod(extension.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic), ignoreCase: false, publicOnly: true, name, argTypes);
582 if (methodInfo2 != null)
583 {
584 result = new FuncExtension(extension, methodInfo2);
585 }
586 return result;
587 }
588 return null;
589 }
590
591 public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] argTypes)
592 {
594 if (prefix.Length == 0)
595 {
597 }
598 else
599 {
600 string text = LookupNamespace(prefix);
601 if (text == "urn:schemas-microsoft-com:xslt" && name == "node-set")
602 {
604 }
605 else
606 {
608 if (extension == null)
609 {
611 }
612 }
613 }
614 if (xsltContextFunction == null)
615 {
617 }
618 if (argTypes.Length < xsltContextFunction.Minargs || xsltContextFunction.Maxargs < argTypes.Length)
619 {
620 throw XsltException.Create(System.SR.Xslt_WrongNumberArgs, name, argTypes.Length.ToString(CultureInfo.InvariantCulture));
621 }
622 return xsltContextFunction;
623 }
624
625 private Uri ComposeUri(string thisUri, string baseUri)
626 {
628 Uri baseUri2 = null;
629 if (baseUri.Length != 0)
630 {
631 baseUri2 = resolver.ResolveUri(null, baseUri);
632 }
633 return resolver.ResolveUri(baseUri2, thisUri);
634 }
635
636 private XPathNodeIterator Document(object arg0, string baseUri)
637 {
639 {
641 Hashtable hashtable = new Hashtable();
642 while (xPathNodeIterator.MoveNext())
643 {
644 Uri uri = ComposeUri(xPathNodeIterator.Current.Value, baseUri ?? xPathNodeIterator.Current.BaseURI);
645 if (!hashtable.ContainsKey(uri))
646 {
647 hashtable.Add(uri, null);
649 }
650 }
651 return new XPathArrayIterator(arrayList);
652 }
654 }
655
657 {
658 Hashtable hashtable = new Hashtable();
663 while (xPathNodeIterator.MoveNext())
664 {
665 XPathNavigator current = xPathNodeIterator.Current;
667 if (current.MoveToFirstAttribute())
668 {
669 do
670 {
672 }
673 while (current.MoveToNextAttribute());
674 current.MoveToParent();
675 }
676 }
677 return hashtable;
678 }
679
681 {
683 if (arrayList == null)
684 {
685 arrayList = new ArrayList();
686 keyTable.Add(key, arrayList);
687 }
688 else if (checkDuplicates && value.ComparePosition((XPathNavigator)arrayList[arrayList.Count - 1]) == XmlNodeOrder.Same)
689 {
690 return;
691 }
692 arrayList.Add(value.Clone());
693 }
694
696 {
697 try
698 {
699 if (matchExpr.MatchNode(node) == null)
700 {
701 return;
702 }
703 }
704 catch (XPathException)
705 {
707 }
708 object obj = useExpr.Evaluate(new XPathSingletonIterator(node, moved: true));
710 {
711 bool checkDuplicates = false;
712 while (xPathNodeIterator.MoveNext())
713 {
715 checkDuplicates = true;
716 }
717 }
718 else
719 {
722 }
723 }
724
726 {
727 string ns = string.Empty;
728 string local = string.Empty;
729 if (formatName != null)
730 {
733 }
735 if (decimalFormat == null)
736 {
737 if (formatName != null)
738 {
740 }
741 decimalFormat = new DecimalFormat(new NumberFormatInfo(), '#', '0', ';');
742 }
743 return decimalFormat;
744 }
745
746 private bool ElementAvailable(string qname)
747 {
750 if (text == "http://www.w3.org/1999/XSL/Transform")
751 {
752 switch (local)
753 {
754 default:
755 return local == "variable";
756 case "apply-imports":
757 case "apply-templates":
758 case "attribute":
759 case "call-template":
760 case "choose":
761 case "comment":
762 case "copy":
763 case "copy-of":
764 case "element":
765 case "fallback":
766 case "for-each":
767 case "if":
768 case "message":
769 case "number":
770 case "processing-instruction":
771 case "text":
772 case "value-of":
773 return true;
774 }
775 }
776 return false;
777 }
778
779 private bool FunctionAvailable(string qname)
780 {
782 string text = LookupNamespace(prefix);
783 if (text == "urn:schemas-microsoft-com:xslt")
784 {
785 return local == "node-set";
786 }
787 if (text.Length == 0)
788 {
789 switch (local)
790 {
791 default:
792 if (s_FunctionTable[local] != null)
793 {
794 return local != "unparsed-entity-uri";
795 }
796 return false;
797 case "last":
798 case "position":
799 case "name":
800 case "namespace-uri":
801 case "local-name":
802 case "count":
803 case "id":
804 case "string":
805 case "concat":
806 case "starts-with":
807 case "contains":
808 case "substring-before":
809 case "substring-after":
810 case "substring":
811 case "string-length":
812 case "normalize-space":
813 case "translate":
814 case "boolean":
815 case "not":
816 case "true":
817 case "false":
818 case "lang":
819 case "number":
820 case "sum":
821 case "floor":
822 case "ceiling":
823 case "round":
824 return true;
825 }
826 }
827 object extension;
828 return GetExtentionMethod(text, local, null, out extension) != null;
829 }
830
832 {
834 if (current != null)
835 {
836 return new XPathSingletonIterator(current.Clone());
837 }
839 }
840
841 private string SystemProperty(string qname)
842 {
843 string result = string.Empty;
845 string text = LookupNamespace(prefix);
846 if (text == "http://www.w3.org/1999/XSL/Transform")
847 {
848 switch (local)
849 {
850 case "version":
851 result = "1";
852 break;
853 case "vendor":
854 result = "Microsoft";
855 break;
856 case "vendor-url":
857 result = "http://www.microsoft.com";
858 break;
859 }
860 return result;
861 }
862 if (text == null && prefix != null)
863 {
865 }
866 return string.Empty;
867 }
868
870 {
871 switch (Type.GetTypeCode(type))
872 {
873 case TypeCode.String:
874 return XPathResultType.String;
875 case TypeCode.Boolean:
876 return XPathResultType.Boolean;
877 case TypeCode.Object:
878 if (typeof(XPathNavigator).IsAssignableFrom(type) || typeof(IXPathNavigable).IsAssignableFrom(type))
879 {
880 return XPathResultType.String;
881 }
882 if (typeof(XPathNodeIterator).IsAssignableFrom(type))
883 {
884 return XPathResultType.NodeSet;
885 }
886 return XPathResultType.Any;
887 case TypeCode.DateTime:
888 return XPathResultType.Error;
889 default:
890 return XPathResultType.Number;
891 }
892 }
893
895 {
896 Hashtable hashtable = new Hashtable(10);
897 hashtable["current"] = new FuncCurrent();
898 hashtable["unparsed-entity-uri"] = new FuncUnEntityUri();
899 hashtable["generate-id"] = new FuncGenerateId();
900 hashtable["system-property"] = new FuncSystemProp();
901 hashtable["element-available"] = new FuncElementAvailable();
902 hashtable["function-available"] = new FuncFunctionAvailable();
903 hashtable["document"] = new FuncDocument();
904 hashtable["key"] = new FuncKey();
905 hashtable["format-number"] = new FuncFormatNumber();
906 return hashtable;
907 }
908}
virtual bool ContainsKey(object key)
Definition Hashtable.cs:724
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
static bool ToBoolean([NotNullWhen(true)] object? value)
Definition Convert.cs:508
static CultureInfo InvariantCulture
object? Invoke(object? obj, object?[]? parameters)
static string Xslt_UnknownXsltFunction
Definition SR.cs:1914
static string Xslt_InvalidVariable
Definition SR.cs:1902
static string Xslt_ScriptInvalidPrefix
Definition SR.cs:2116
static string Xslt_InvalidPrefix
Definition SR.cs:1926
static string Xslt_WrongNumberArgs
Definition SR.cs:2098
static string Xslt_NoNavigatorConversion
Definition SR.cs:2102
static string Xslt_NoDecimalFormat
Definition SR.cs:1936
static string Xslt_InvalidPattern
Definition SR.cs:2134
static string Xslt_NoNodeSetConversion
Definition SR.cs:2100
static string Xslt_UnsuppFunction
Definition SR.cs:2070
Definition SR.cs:7
String(char[]? value)
static TypeCode GetTypeCode(Type? type)
Definition Type.cs:919
virtual XPathNodeIterator SelectDescendants(XPathNodeType type, bool matchSelf)
static double ToXPathDouble(object o)
static string ToXPathString(object value)
static bool IsCatchableException(Exception e)
virtual ? string LookupNamespace(string prefix)
static XsltException Create(string res, params string[] args)
VariableAction ResolveVariable(XmlQualifiedName qname)
VariableAction ResolveGlobalVariable(XmlQualifiedName qname)
static void ParseQualifiedName(string qname, out string prefix, out string local)
object GetVariableValue(VariableAction variable)
Definition Processor.cs:738
object GetScriptObject(string nsUri)
Definition Processor.cs:198
string GetQueryExpression(int key)
Definition Processor.cs:371
object GetExtensionObject(string nsUri)
Definition Processor.cs:193
XPathNavigator GetNavigator(Uri ruri)
Definition Processor.cs:128
DecimalFormat GetDecimalFormat(XmlQualifiedName name)
bool PreserveWhiteSpace(Processor proc, XPathNavigator node)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
static ArrayList AddToList(ArrayList resultCollection, ArrayList newList)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
override object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
XsltFunctionImpl(int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
static XPathNodeIterator ToIterator(object argument)
static object ToNumeric(object argument, Type type)
void Init(int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
object Invoke(XsltContext xsltContext, object[] args, XPathNavigator docContext)
static object ConvertToXPathType(object val, XPathResultType xt, Type type)
XsltCompileContext(InputScopeManager manager, Processor processor)
static XPathResultType GetXPathType(Type type)
override int CompareDocument(string baseUri, string nextbaseUri)
MethodInfo FindBestMethod(MethodInfo[] methods, bool ignoreCase, bool publicOnly, string name, XPathResultType[] argTypes)
override IXsltContextVariable ResolveVariable(string prefix, string name)
object EvaluateVariable(VariableAction variable)
IXsltContextFunction GetExtentionMethod(string ns, string name, XPathResultType[] argTypes, out object extension)
Hashtable BuildKeyTable(Key key, XPathNavigator root)
override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] argTypes)
Uri ComposeUri(string thisUri, string baseUri)
static void AddKeyValue(Hashtable keyTable, string key, XPathNavigator value, bool checkDuplicates)
override string LookupNamespace(string prefix)
void Reinitialize(InputScopeManager manager, Processor processor)
XPathNodeIterator Document(object arg0, string baseUri)
DecimalFormat ResolveFormatName(string formatName)
override bool PreserveWhitespace(XPathNavigator node)
static void EvaluateKey(XPathNavigator node, Query matchExpr, string matchStr, Query useExpr, Hashtable keyTable)
static readonly IXsltContextFunction s_FuncNodeSet
TypeCode
Definition TypeCode.cs:4