XmlRpcSerializer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Xml;
  34. /// <summary>Base class of classes serializing data to XML-RPC's XML format.</summary>
  35. /// <remarks>This class handles the basic type conversions like Integer to &lt;i4&gt;. </remarks>
  36. /// <seealso cref="XmlRpcXmlTokens"/>
  37. public class XmlRpcSerializer : XmlRpcXmlTokens
  38. {
  39. /// <summary>Serialize the <c>XmlRpcRequest</c> to the output stream.</summary>
  40. /// <param name="output">An <c>XmlTextWriter</c> stream to write data to.</param>
  41. /// <param name="obj">An <c>Object</c> to serialize.</param>
  42. /// <seealso cref="XmlRpcRequest"/>
  43. virtual public void Serialize(XmlTextWriter output, Object obj)
  44. {
  45. }
  46. /// <summary>Serialize the <c>XmlRpcRequest</c> to a String.</summary>
  47. /// <remarks>Note this may represent a real memory hog for a large request.</remarks>
  48. /// <param name="obj">An <c>Object</c> to serialize.</param>
  49. /// <returns><c>String</c> containing XML-RPC representation of the request.</returns>
  50. /// <seealso cref="XmlRpcRequest"/>
  51. public String Serialize(Object obj)
  52. {
  53. StringWriter strBuf = new StringWriter();
  54. XmlTextWriter xml = new XmlTextWriter(strBuf);
  55. xml.Formatting = Formatting.Indented;
  56. xml.Indentation = 4;
  57. Serialize(xml, obj);
  58. xml.Flush();
  59. String returns = strBuf.ToString();
  60. xml.Close();
  61. return returns;
  62. }
  63. /// <remarks>Serialize the object to the output stream.</remarks>
  64. /// <param name="output">An <c>XmlTextWriter</c> stream to write data to.</param>
  65. /// <param name="obj">An <c>Object</c> to serialize.</param>
  66. public void SerializeObject(XmlTextWriter output, Object obj)
  67. {
  68. if (obj == null)
  69. return;
  70. if (obj is byte[])
  71. {
  72. byte[] ba = (byte[])obj;
  73. output.WriteStartElement(BASE64);
  74. output.WriteBase64(ba, 0, ba.Length);
  75. output.WriteEndElement();
  76. }
  77. else if (obj is String)
  78. {
  79. output.WriteElementString(STRING, obj.ToString());
  80. }
  81. else if (obj is Int32)
  82. {
  83. output.WriteElementString(INT, obj.ToString());
  84. }
  85. else if (obj is DateTime)
  86. {
  87. output.WriteElementString(DATETIME, ((DateTime)obj).ToString(ISO_DATETIME));
  88. }
  89. else if (obj is Double)
  90. {
  91. output.WriteElementString(DOUBLE, obj.ToString());
  92. }
  93. else if (obj is Boolean)
  94. {
  95. output.WriteElementString(BOOLEAN, ((((Boolean)obj) == true) ? "1" : "0"));
  96. }
  97. else if (obj is IList)
  98. {
  99. output.WriteStartElement(ARRAY);
  100. output.WriteStartElement(DATA);
  101. if (((ArrayList)obj).Count > 0)
  102. {
  103. foreach (Object member in ((IList)obj))
  104. {
  105. output.WriteStartElement(VALUE);
  106. SerializeObject(output, member);
  107. output.WriteEndElement();
  108. }
  109. }
  110. output.WriteEndElement();
  111. output.WriteEndElement();
  112. }
  113. else if (obj is IDictionary)
  114. {
  115. IDictionary h = (IDictionary)obj;
  116. output.WriteStartElement(STRUCT);
  117. foreach (String key in h.Keys)
  118. {
  119. output.WriteStartElement(MEMBER);
  120. output.WriteElementString(NAME, key);
  121. output.WriteStartElement(VALUE);
  122. SerializeObject(output, h[key]);
  123. output.WriteEndElement();
  124. output.WriteEndElement();
  125. }
  126. output.WriteEndElement();
  127. }
  128. }
  129. }
  130. }