/DICK.B1/B1ImportTarget.cs
C# | 156 lines | 127 code | 6 blank | 23 comment | 16 complexity | 0a9a27ae7fe81925bcf816a61af63281 MD5 | raw file
- /* This file is part of DI Construction Kit.
- *
- * DI Construction Kit is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * DI Construction Kit is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with DI Construction Kit. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using System.Data;
- using System.Reflection;
- using DICK.Core;
-
- namespace DICK.B1
- {
- public class B1XmlImportTarget
- {
- private const string NAMESPACE = "DICK.Scripts.";
- private DICKAssembly dassembly;
- private string docstarttoken;
- private string docendtoken;
- private string className;
- private B1Engine engine = B1Engine.GetInstance();
-
- public B1XmlImportTarget(XmlElement importData, DICKAssembly _dassembly)
- {
- docstarttoken = XmlTools.ParseString(importData, "StartToken", "");
- docendtoken = XmlTools.ParseString(importData, "EndToken", "");
- className = XmlTools.ParseString(importData, "ClassName", "");
- dassembly = _dassembly;
- }
-
- public bool ProcessFile(string docbatch, bool batchSuccessful)
- {
- List<string> importTokens = SplitImportDocuments(docbatch);
- object instance = InstantiateClass(engine);
- object o = dassembly.InvokeInstanceMethod(instance, "InitializeJob", new object[] { });
-
- foreach (string importToken in importTokens)
- {
- object ti=null;
- try
- {
- XmlElement importDocumentRoot = LoadXmlDocument(importToken);
- ti = dassembly.InvokeInstanceMethod(instance, "Import", new object[] { importDocumentRoot });
- }
- catch (Exception ex)
- {
- batchSuccessful = false;
- }
- if(ti==null)
- {
- Engine.GetInstance().LogError("B1ImportTarget.ProcessFile: Calling Import resulted with null");
- throw new DICKException("B1ImportTarget.ProcessFile: Calling Import resulted with null");
- }
- }
- try
- {
- object tf = dassembly.InvokeInstanceMethod(instance, "FinalizeJob", new object[] { });
- }
- catch (Exception ex)
- {
- engine.LogError(ex.Message);
- batchSuccessful = false;
- }
- return batchSuccessful;
- }
-
- private XmlElement LoadXmlDocument(string importToken)
- {
- XmlElement importDocumentRoot = null;
- try
- {
- XmlDocument importDocument = new XmlDocument();
- importDocument.LoadXml(importToken);
- importDocumentRoot = importDocument.DocumentElement;
- }
- catch (XmlException xex)
- {
- engine.LogError("B1ImportTarget failed parsing the XML document;" + xex.Message);
- throw;
- }
- return importDocumentRoot;
- }
-
- private object InstantiateClass(B1Engine engine)
- {
- if (dassembly == null)
- {
- engine.LogError("ImportTarget.ProcessFile: assembly is null! Unable to instantiate !!");
- throw new DICKException("ImportTarget.ProcessFile: assembly is null! Unable to instantiate !!");
- }
- object instance = dassembly.Instantiate(NAMESPACE + className);
- if (instance == null)
- {
- engine.LogError("Instantiating " + NAMESPACE + className + " failed !!");
- throw new DICKException("Instantiating " + NAMESPACE + className + " failed !!");
- }
- return instance;
- }
- /// <summary>
- /// Extracts the XML documents from the input stream.
- /// </summary>
- /// <param name="docbatch"></param>
- /// <returns></returns>
- private List<string> SplitImportDocuments(string docbatch)
- {
- System.Collections.Generic.List<string> docs = new List<string>();
- //If docstarttoken and docendtoken are empty strings, simply return the whole input string.
- if ((docstarttoken == null || docstarttoken.Equals("")) && (docendtoken == null || docendtoken.Equals("")))
- {
- docs.Add(docbatch);
- }
- //Otherwise split the input string according to start and end tokens.
- else
- {
- int startloc = 0;
- bool repeat = true;
- while (repeat)
- {
- startloc = docstarttoken.Trim().Equals("") ? 0 : docbatch.IndexOf(docstarttoken, startloc, StringComparison.InvariantCultureIgnoreCase);
- if (startloc != -1)
- {
- int endloc = docendtoken.Trim().Equals("") ? docbatch.Length - 1 : docbatch.IndexOf(docendtoken, startloc + docstarttoken.Length, StringComparison.InvariantCultureIgnoreCase);
- if (endloc != -1)
- {
- string batchdoc = docbatch.Substring(startloc, endloc + docendtoken.Length - startloc);
- docs.Add(batchdoc);
- startloc = endloc + docendtoken.Length;
- }
- else
- {
- repeat = false;
- }
- }
- else
- {
- repeat = false;
- }
- }
- }
- return docs;
- }
- }
- }