XsltArgumentList.cs 5.1 KB

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