DesignerUtilsService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using FastReport.Utils;
  2. using FastReport.Utils.Json;
  3. using FastReport.Web.Infrastructure;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Net;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Xml.Linq;
  15. namespace FastReport.Web.Services
  16. {
  17. internal sealed class DesignerUtilsService : IDesignerUtilsService
  18. {
  19. private const string IsCustomSqlAllowedKey = "custom-sql-allowed";
  20. public string GetMSChartTemplateXML(string templateName)
  21. {
  22. var stream = ResourceLoader.GetStream("MSChart." + templateName + ".xml");
  23. try
  24. {
  25. return new StreamReader(stream).ReadToEnd();
  26. }
  27. catch
  28. {
  29. return null;
  30. }
  31. }
  32. public string GetFunctions(Report report)
  33. {
  34. using (var xml = new XmlDocument())
  35. {
  36. xml.AutoIndent = true;
  37. var list = new List<FunctionInfo>();
  38. RegisteredObjects.Functions.EnumItems(list);
  39. FunctionInfo rootFunctions = null;
  40. foreach (FunctionInfo item in list)
  41. {
  42. if (item.Name == "Functions")
  43. {
  44. rootFunctions = item;
  45. break;
  46. }
  47. }
  48. xml.Root.Name = "ReportFunctions";
  49. if (rootFunctions != null)
  50. RegisteredObjects.CreateFunctionsTree(report, rootFunctions, xml.Root);
  51. using (var stream = new MemoryStream())
  52. {
  53. xml.Save(stream);
  54. stream.Position = 0;
  55. byte[] buff = new byte[stream.Length];
  56. stream.Read(buff, 0, buff.Length);
  57. return Encoding.UTF8.GetString(buff);
  58. }
  59. }
  60. }
  61. public string GetPropertiesJSON(string componentName)
  62. {
  63. if (ComponentInformationCache.ComponentPropertiesCache.TryGetValue(componentName, out string componentPropertiesJson))
  64. return componentPropertiesJson;
  65. var prefixes = new string[]
  66. {
  67. "", "SVG.", "MSChart.", "Dialog.", "AdvMatrix.", "Table.", "Barcode.", "Map.", "CrossView.", "Matrix.",
  68. "Gauge.Simple.", "Gauge.Radial.", "Gauge.Linear.", "Gauge.Simple.Progress."
  69. };
  70. Type type = null;
  71. foreach (var prefix in prefixes)
  72. {
  73. type = ComponentInformationCache.Assembly.GetType("FastReport." + prefix + componentName);
  74. if (type != null)
  75. break;
  76. }
  77. if (type is null || !type.IsPublic)
  78. return null;
  79. var jsonObject = new JsonObject();
  80. // Doesn't return collections because they don't have a setter (Example - TextObject.Highlight)
  81. foreach (var property in type.GetProperties())
  82. {
  83. if (!property.CanWrite) continue;
  84. var isVisible = true;
  85. var defaultValue = "null";
  86. var category = "Misc";
  87. foreach (var attribute in property.GetCustomAttributes())
  88. switch (attribute)
  89. {
  90. case CategoryAttribute categoryAttribute:
  91. category = categoryAttribute.Category;
  92. break;
  93. case DefaultValueAttribute defaultValueAttribute:
  94. defaultValue = defaultValueAttribute.Value.ToString();
  95. break;
  96. case BrowsableAttribute browsableAttribute:
  97. isVisible = browsableAttribute.Browsable;
  98. break;
  99. }
  100. if (isVisible)
  101. jsonObject[$@"{category}:{property.Name}"] = defaultValue;
  102. }
  103. componentPropertiesJson = jsonObject.ToString();
  104. ComponentInformationCache.ComponentPropertiesCache.Add(componentName, componentPropertiesJson);
  105. return componentPropertiesJson;
  106. }
  107. public string DesignerObjectPreview(WebReport webReport, string reportObj)
  108. {
  109. var sb = new StringBuilder();
  110. using (var report = new Report())
  111. {
  112. var obj = report.Xml(reportObj);
  113. //obj.SetReport(report);
  114. using (var html = new Export.Html.HTMLExport(true))
  115. {
  116. html.StylePrefix = obj.Name.Trim();
  117. html.SetReport(report);
  118. html.ExportReportObject(obj);
  119. sb.Append("<div>");
  120. //sb.Append("<div ")
  121. // .Append(" style=\"position:relative;")
  122. // .Append(" width:").Append(Px(maxWidth * Zoom + 3))
  123. // .Append(" height:").Append(Px(maxHeight * Zoom))
  124. // .Append("\">");
  125. webReport.DoHtmlPage(sb, html, 0);
  126. }
  127. }
  128. return sb.ToString();
  129. }
  130. public string GetConfig(WebReport webReport)
  131. {
  132. JsonBase config;
  133. try
  134. {
  135. config = JsonBase.FromString(webReport.Designer.Config);
  136. }
  137. catch
  138. {
  139. config = new JsonObject();
  140. }
  141. config[IsCustomSqlAllowedKey] = FastReportGlobal.AllowCustomSqlQueries;
  142. return config.ToString();
  143. }
  144. }
  145. static class ComponentInformationCache
  146. {
  147. internal static readonly Dictionary<string, string> ComponentPropertiesCache = new Dictionary<string, string>();
  148. internal static readonly Assembly Assembly = typeof(Report).Assembly;
  149. }
  150. }