XsltArgumentList.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // System.Xml.Xsl.XsltArgumentList
  2. //
  3. // Author: Tim Coleman <[email protected]>
  4. // (C) Copyright 2002 Tim Coleman
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. using System;
  26. using System.Collections;
  27. using System.Xml;
  28. using System.Xml.XPath;
  29. namespace System.Xml.Xsl
  30. {
  31. public sealed class XsltArgumentList
  32. {
  33. #region Fields
  34. internal Hashtable extensionObjects;
  35. internal Hashtable parameters;
  36. #endregion
  37. #region Constructors
  38. public XsltArgumentList ()
  39. {
  40. extensionObjects = new Hashtable ();
  41. parameters = new Hashtable ();
  42. }
  43. #endregion
  44. #region Methods
  45. public void AddExtensionObject (string namespaceUri, object extension)
  46. {
  47. if (namespaceUri == null)
  48. throw new ArgumentException ("The namespaceUri is a null reference.");
  49. if (namespaceUri == "http://www.w3.org/1999/XSL/Transform")
  50. throw new ArgumentException ("The namespaceUri is http://www.w3.org/1999/XSL/Transform.");
  51. if (extensionObjects.Contains (namespaceUri))
  52. throw new ArgumentException ("The namespaceUri already has an extension object associated with it.");
  53. extensionObjects [namespaceUri] = extension;
  54. }
  55. public void AddParam (string name, string namespaceUri, object parameter)
  56. {
  57. if (namespaceUri == null)
  58. throw new ArgumentException ("The namespaceUri is a null reference.");
  59. if (namespaceUri == "http://www.w3.org/1999/XSL/Transform")
  60. throw new ArgumentException ("The namespaceUri is http://www.w3.org/1999/XSL/Transform.");
  61. if (name == null)
  62. throw new ArgumentException ("The parameter name is a null reference.");
  63. // TODO:
  64. // verify that the name is a valid name according to
  65. // the W3C XML specification
  66. XmlQualifiedName qName = new XmlQualifiedName (name, namespaceUri);
  67. if (parameters.Contains (qName))
  68. throw new ArgumentException ("The namespaceUri already has a parameter associated with it.");
  69. parameter = ValidateParam (parameter);
  70. parameters [qName] = parameter;
  71. }
  72. public void Clear ()
  73. {
  74. extensionObjects.Clear ();
  75. parameters.Clear ();
  76. }
  77. public object GetExtensionObject (string namespaceUri)
  78. {
  79. return extensionObjects [namespaceUri];
  80. }
  81. public object GetParam (string name, string namespaceUri)
  82. {
  83. if (name == null)
  84. throw (new ArgumentException ("The parameter name is a null reference."));
  85. XmlQualifiedName qName = new XmlQualifiedName (name, namespaceUri);
  86. return parameters [qName];
  87. }
  88. public object RemoveExtensionObject (string namespaceUri)
  89. {
  90. object extensionObject = this.GetExtensionObject (namespaceUri);
  91. extensionObjects.Remove (namespaceUri);
  92. return extensionObject;
  93. }
  94. public object RemoveParam (string name, string namespaceUri)
  95. {
  96. XmlQualifiedName qName = new XmlQualifiedName (name, namespaceUri);
  97. object parameter = this.GetParam (name, namespaceUri);
  98. parameters.Remove (qName);
  99. return parameter;
  100. }
  101. private object ValidateParam (object parameter)
  102. {
  103. if (parameter is string) return parameter;
  104. if (parameter is bool) return parameter;
  105. if (parameter is double) return parameter;
  106. if (parameter is XPathNavigator) return parameter;
  107. if (parameter is XPathNodeIterator) return parameter;
  108. if (parameter is Int16) return (double) (Int16) parameter;
  109. if (parameter is UInt16) return (double) (UInt16) parameter;
  110. if (parameter is Int32) return (double) (Int32) parameter;
  111. if (parameter is Int64) return (double) (Int64) parameter;
  112. if (parameter is UInt64) return (double) (UInt64) parameter;
  113. if (parameter is Single) return (double) (Single) parameter;
  114. if (parameter is decimal) return (double) (decimal) parameter;
  115. return parameter.ToString ();
  116. }
  117. #endregion
  118. }
  119. }