PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/B1ImportTarget.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 156 lines | 127 code | 6 blank | 23 comment | 16 complexity | 0a9a27ae7fe81925bcf816a61af63281 MD5 | raw file
  1. /* This file is part of DI Construction Kit.
  2. *
  3. * DI Construction Kit is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * DI Construction Kit is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with DI Construction Kit. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using System.Xml;
  21. using System.Data;
  22. using System.Reflection;
  23. using DICK.Core;
  24. namespace DICK.B1
  25. {
  26. public class B1XmlImportTarget
  27. {
  28. private const string NAMESPACE = "DICK.Scripts.";
  29. private DICKAssembly dassembly;
  30. private string docstarttoken;
  31. private string docendtoken;
  32. private string className;
  33. private B1Engine engine = B1Engine.GetInstance();
  34. public B1XmlImportTarget(XmlElement importData, DICKAssembly _dassembly)
  35. {
  36. docstarttoken = XmlTools.ParseString(importData, "StartToken", "");
  37. docendtoken = XmlTools.ParseString(importData, "EndToken", "");
  38. className = XmlTools.ParseString(importData, "ClassName", "");
  39. dassembly = _dassembly;
  40. }
  41. public bool ProcessFile(string docbatch, bool batchSuccessful)
  42. {
  43. List<string> importTokens = SplitImportDocuments(docbatch);
  44. object instance = InstantiateClass(engine);
  45. object o = dassembly.InvokeInstanceMethod(instance, "InitializeJob", new object[] { });
  46. foreach (string importToken in importTokens)
  47. {
  48. object ti=null;
  49. try
  50. {
  51. XmlElement importDocumentRoot = LoadXmlDocument(importToken);
  52. ti = dassembly.InvokeInstanceMethod(instance, "Import", new object[] { importDocumentRoot });
  53. }
  54. catch (Exception ex)
  55. {
  56. batchSuccessful = false;
  57. }
  58. if(ti==null)
  59. {
  60. Engine.GetInstance().LogError("B1ImportTarget.ProcessFile: Calling Import resulted with null");
  61. throw new DICKException("B1ImportTarget.ProcessFile: Calling Import resulted with null");
  62. }
  63. }
  64. try
  65. {
  66. object tf = dassembly.InvokeInstanceMethod(instance, "FinalizeJob", new object[] { });
  67. }
  68. catch (Exception ex)
  69. {
  70. engine.LogError(ex.Message);
  71. batchSuccessful = false;
  72. }
  73. return batchSuccessful;
  74. }
  75. private XmlElement LoadXmlDocument(string importToken)
  76. {
  77. XmlElement importDocumentRoot = null;
  78. try
  79. {
  80. XmlDocument importDocument = new XmlDocument();
  81. importDocument.LoadXml(importToken);
  82. importDocumentRoot = importDocument.DocumentElement;
  83. }
  84. catch (XmlException xex)
  85. {
  86. engine.LogError("B1ImportTarget failed parsing the XML document;" + xex.Message);
  87. throw;
  88. }
  89. return importDocumentRoot;
  90. }
  91. private object InstantiateClass(B1Engine engine)
  92. {
  93. if (dassembly == null)
  94. {
  95. engine.LogError("ImportTarget.ProcessFile: assembly is null! Unable to instantiate !!");
  96. throw new DICKException("ImportTarget.ProcessFile: assembly is null! Unable to instantiate !!");
  97. }
  98. object instance = dassembly.Instantiate(NAMESPACE + className);
  99. if (instance == null)
  100. {
  101. engine.LogError("Instantiating " + NAMESPACE + className + " failed !!");
  102. throw new DICKException("Instantiating " + NAMESPACE + className + " failed !!");
  103. }
  104. return instance;
  105. }
  106. /// <summary>
  107. /// Extracts the XML documents from the input stream.
  108. /// </summary>
  109. /// <param name="docbatch"></param>
  110. /// <returns></returns>
  111. private List<string> SplitImportDocuments(string docbatch)
  112. {
  113. System.Collections.Generic.List<string> docs = new List<string>();
  114. //If docstarttoken and docendtoken are empty strings, simply return the whole input string.
  115. if ((docstarttoken == null || docstarttoken.Equals("")) && (docendtoken == null || docendtoken.Equals("")))
  116. {
  117. docs.Add(docbatch);
  118. }
  119. //Otherwise split the input string according to start and end tokens.
  120. else
  121. {
  122. int startloc = 0;
  123. bool repeat = true;
  124. while (repeat)
  125. {
  126. startloc = docstarttoken.Trim().Equals("") ? 0 : docbatch.IndexOf(docstarttoken, startloc, StringComparison.InvariantCultureIgnoreCase);
  127. if (startloc != -1)
  128. {
  129. int endloc = docendtoken.Trim().Equals("") ? docbatch.Length - 1 : docbatch.IndexOf(docendtoken, startloc + docstarttoken.Length, StringComparison.InvariantCultureIgnoreCase);
  130. if (endloc != -1)
  131. {
  132. string batchdoc = docbatch.Substring(startloc, endloc + docendtoken.Length - startloc);
  133. docs.Add(batchdoc);
  134. startloc = endloc + docendtoken.Length;
  135. }
  136. else
  137. {
  138. repeat = false;
  139. }
  140. }
  141. else
  142. {
  143. repeat = false;
  144. }
  145. }
  146. }
  147. return docs;
  148. }
  149. }
  150. }