XmlDataConnection.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. using System.Data.Common;
  8. using System.Net;
  9. using System.IO;
  10. namespace FastReport.Data
  11. {
  12. /// <summary>
  13. /// Represents a connection to xml file-based database.
  14. /// </summary>
  15. /// <example>This example shows how to add a new connection to the report.
  16. /// <code>
  17. /// Report report1;
  18. /// XmlDataConnection conn = new XmlDataConnection();
  19. /// conn.XmlFile = @"c:\data.xml";
  20. /// report1.Dictionary.Connections.Add(conn);
  21. /// conn.CreateAllTables();
  22. /// </code>
  23. /// </example>
  24. public partial class XmlDataConnection : DataConnectionBase
  25. {
  26. #region Properties
  27. /// <summary>
  28. /// Gets or sets the path to .xsd file.
  29. /// </summary>
  30. [Category("Data")]
  31. public string XsdFile
  32. {
  33. get
  34. {
  35. XmlConnectionStringBuilder builder = new XmlConnectionStringBuilder(ConnectionString);
  36. return builder.XsdFile;
  37. }
  38. set
  39. {
  40. XmlConnectionStringBuilder builder = new XmlConnectionStringBuilder(ConnectionString);
  41. builder.XsdFile = value;
  42. ConnectionString = builder.ToString();
  43. }
  44. }
  45. /// <summary>
  46. /// Gets or sets the path to .xml file.
  47. /// </summary>
  48. [Category("Data")]
  49. public string XmlFile
  50. {
  51. get
  52. {
  53. XmlConnectionStringBuilder builder = new XmlConnectionStringBuilder(ConnectionString);
  54. return builder.XmlFile;
  55. }
  56. set
  57. {
  58. XmlConnectionStringBuilder builder = new XmlConnectionStringBuilder(ConnectionString);
  59. builder.XmlFile = value;
  60. ConnectionString = builder.ToString();
  61. }
  62. }
  63. #endregion
  64. #region Protected Methods
  65. /// <inheritdoc/>
  66. protected override DataSet CreateDataSet()
  67. {
  68. DataSet dataset = base.CreateDataSet();
  69. ReadXmlSchema(dataset);
  70. ReadXml(dataset);
  71. return dataset;
  72. }
  73. /// <inheritdoc/>
  74. protected override void SetConnectionString(string value)
  75. {
  76. DisposeDataSet();
  77. base.SetConnectionString(value);
  78. }
  79. #endregion
  80. #region Public Methods
  81. /// <inheritdoc/>
  82. public override void FillTableSchema(DataTable table, string selectCommand, CommandParameterCollection parameters)
  83. {
  84. // do nothing
  85. }
  86. /// <inheritdoc/>
  87. public override void FillTableData(DataTable table, string selectCommand, CommandParameterCollection parameters)
  88. {
  89. // do nothing
  90. }
  91. /// <inheritdoc/>
  92. public override string[] GetTableNames()
  93. {
  94. string[] result = new string[DataSet.Tables.Count];
  95. for (int i = 0; i < DataSet.Tables.Count; i++)
  96. {
  97. result[i] = DataSet.Tables[i].TableName;
  98. }
  99. return result;
  100. }
  101. /// <inheritdoc/>
  102. public override void CreateTable(TableDataSource source)
  103. {
  104. if (DataSet.Tables.Contains(source.TableName))
  105. {
  106. source.Table = DataSet.Tables[source.TableName];
  107. base.CreateTable(source);
  108. }
  109. else
  110. source.Table = null;
  111. }
  112. /// <inheritdoc/>
  113. public override void DeleteTable(TableDataSource source)
  114. {
  115. // do nothing
  116. }
  117. /// <inheritdoc/>
  118. public override string QuoteIdentifier(string value, DbConnection connection)
  119. {
  120. return value;
  121. }
  122. #endregion
  123. #region private methods
  124. private void ReadXml(DataSet dataset)
  125. {
  126. try
  127. {
  128. // fix for datafile in current folder
  129. if (File.Exists(XmlFile))
  130. XmlFile = Path.GetFullPath(XmlFile);
  131. Uri uri = new Uri(XmlFile);
  132. if (uri.IsFile)
  133. {
  134. if (Config.ForbidLocalData)
  135. throw new Exception(Res.Get("ConnectionEditors,Common,OnlyUrlException"));
  136. dataset.ReadXml(XmlFile);
  137. }
  138. else if (uri.OriginalString.StartsWith("http") || uri.OriginalString.StartsWith("ftp"))
  139. {
  140. LoadXmlFromUrl(dataset);
  141. }
  142. }
  143. catch (Exception e)
  144. {
  145. throw e;
  146. }
  147. }
  148. private void LoadXmlFromUrl(DataSet dataset)
  149. {
  150. ServicePointManager.Expect100Continue = true;
  151. ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  152. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(XmlFile);
  153. using (var response = req.GetResponse() as HttpWebResponse)
  154. {
  155. var encoding = response.CharacterSet.Equals(String.Empty) ? Encoding.UTF8 : Encoding.GetEncoding(response.CharacterSet);
  156. using (var responseStream = response.GetResponseStream())
  157. using (var reader = new System.IO.StreamReader(responseStream, encoding))
  158. dataset.ReadXml(reader, XmlReadMode.Auto);
  159. }
  160. }
  161. private void ReadXmlSchema(DataSet dataset)
  162. {
  163. if (String.IsNullOrEmpty(XsdFile))
  164. return;
  165. try
  166. {
  167. Uri uri = new Uri(XsdFile);
  168. if (uri.IsFile)
  169. {
  170. if (Config.ForbidLocalData)
  171. throw new Exception(Res.Get("ConnectionEditors,Common,OnlyUrlException"));
  172. dataset.ReadXmlSchema(XsdFile);
  173. }
  174. else if (uri.OriginalString.StartsWith("http") || uri.OriginalString.StartsWith("ftp"))
  175. {
  176. LoadXmlSchemaFromUrl(dataset);
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. throw e;
  182. }
  183. }
  184. private void LoadXmlSchemaFromUrl(DataSet dataset)
  185. {
  186. ServicePointManager.Expect100Continue = true;
  187. ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  188. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(XsdFile);
  189. using (var response = req.GetResponse() as HttpWebResponse)
  190. {
  191. var encoding = response.CharacterSet.Equals(String.Empty) ? Encoding.UTF8 : Encoding.GetEncoding(response.CharacterSet);
  192. using (var responseStream = response.GetResponseStream())
  193. using (var reader = new System.IO.StreamReader(responseStream, encoding))
  194. dataset.ReadXmlSchema(reader);
  195. }
  196. }
  197. #endregion
  198. /// <summary>
  199. /// Initializes a new instance of the <see cref="XmlDataConnection"/> class with default settings.
  200. /// </summary>
  201. public XmlDataConnection()
  202. {
  203. IsSqlBased = false;
  204. }
  205. }
  206. }