Exemple #1
0
        private static object InternalSerialize(string HandleName, bool Compress = false, string OutputFilePath = null)
        {
            HandleName = HandleNames.GetNameFrom(HandleName);
            IStoredObject storedObj;

            if (!m_ObjectStore.GetByName(HandleName, out storedObj))
            {
                throw new Exception($"No object named {HandleName} found");
            }

            bool WriteToFile = !String.IsNullOrWhiteSpace(OutputFilePath);

            var obj = new SerializationWrapper {
                ContainedObject = storedObj
            };
            var result = XmlSerialize <SerializationWrapper>(obj);

            if (Compress)
            {
                var       compressed = Zip(result);
                XDocument doc        = new XDocument(new XElement("CompressedData", new XAttribute("Algorithm", "gzip"), Convert.ToBase64String(compressed)));

                if (WriteToFile)
                {
                    OutputFilePath = Environment.ExpandEnvironmentVariables(OutputFilePath);

                    using (var fileWriter = new FileStream(OutputFilePath, FileMode.Create))
                    {
                        doc.Save(fileWriter);
                    }
                    return($"Written to '{OutputFilePath}' in compressed format.");
                }
                else
                {
                    using (var stringWriter = new StringWriter())
                    {
                        doc.Save(stringWriter);
                        return(stringWriter.ToString());
                    }
                }
            }
            else
            {
                if (WriteToFile)
                {
                    File.WriteAllLines(OutputFilePath, new string[] { result });
                    return($"Written to '{OutputFilePath}' in uncompressed XML format.");
                }
                else
                {
                    return(result);
                }
            }
        }
        /// <summary>
        /// Gets the object from the store
        /// </summary>
        /// <typeparam name="T">Type of the object to retrieve</typeparam>
        /// <param name="HandleName">Name from which to retrieve the object</param>
        /// <returns>Object from the store</returns>
        /// <exception cref="ArgumentException">Thrown when the HandleName is (empty)</exception>
        /// <exception cref="Exception">Thrown when the given Handle is not found in the store</exception>
        private static T GetFromStoreOrThrow <T>(string HandleName, out IStoredObject <T> storedObj)
            where T : class
        {
            HandleName = HandleNames.GetNameFrom(HandleName);

            if (String.IsNullOrWhiteSpace(HandleName))
            {
                throw new ArgumentException($"Invalid Handle name {HandleName}", nameof(HandleName));
            }

            if (!m_ObjectStore.GetByName <T>(HandleName, out storedObj))
            {
                throw new Exception($"No object {typeof(T).FullName} with handle named {HandleName} found");
            }

            return(storedObj.Object);
        }
        /// <summary>
        /// Adds the given object to the object store, and returns its handle name (name:version).
        /// Throws exceptions if anything goes wrong.
        /// </summary>
        /// <typeparam name="T">Type of the object to add</typeparam>
        /// <param name="HandleName">Name under which the object shall be stored</param>
        /// <param name="obj">The object which shall be stored</param>
        /// <param name="storedObject">[out] the stored object</param>
        /// <returns>The Handle (name:version) under which the object was stored</returns>
        private static string AddOrUpdateInStoreOrThrow <T>(string HandleName, T obj, out IStoredObject <T> storedObject, Action OnUnregister = null)
            where T : class
        {
            // Remove any invalid parts from the input
            HandleName = HandleNames.GetNameFrom(HandleName);

            if (String.IsNullOrWhiteSpace(HandleName))
            {
                throw new ArgumentException($"Invalid Handle name {HandleName}", nameof(HandleName));
            }

            if (!m_ObjectStore.AddOrUpdate <T>(HandleName, obj, out storedObject, OnUnregister))
            {
                throw new Exception("Could not add script to the object store");
            }

            return(HandleNames.ToHandle(storedObject));
        }
Exemple #4
0
        public static object Create(
            [ExcelArgument("Name for a handle under which the script object will be stored in memory")] string HandleName,
            [ExcelArgument("The code for this script. Multiple lines are allowed.")] string[] code,
            [ExcelArgument("A handle to script option definitions as created by ExcelScript.Options.Create. Can be left blank to use defaults.")] string OptionsHandle = "",
            [ExcelArgument("An arbitrary number of handles to parameter definitions (as created by ExcelScript.Parameters.Create), which define input parameters for the script")] params string[] ParameterHandles)
        {
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }

            string _code = String.Join(Environment.NewLine, code);

            // Get Options
            XlScriptOptions xlScriptOptions = (String.IsNullOrEmpty(OptionsHandle)) ? XlScriptOptions.Default : GetFromStoreOrThrow <XlScriptOptions>(OptionsHandle);

            // Create Script object
            var script = CreateScript(xlScriptOptions);

            script.Code       = _code;
            script.ReturnType = xlScriptOptions.ReturnType;

            foreach (var parameterHandle in ParameterHandles)
            {
                var        parameterHandleName = HandleNames.GetNameFrom(parameterHandle);
                IParameter storedParameter     = GetFromStoreOrThrow <IParameter>(parameterHandleName);
                script.Parameters.Add(storedParameter);
            }

            IStoredObject <IScript> storedScript;

            if (m_ObjectStore.GetByName <IScript>(HandleName, out storedScript) && storedScript.Object.GetHashCode() == script.GetHashCode())
            {
                return(HandleNames.ToHandle(storedScript));
            }
            else
            {
                return(AddOrUpdateInStoreOrThrow <IScript>(HandleName, script, () => TryDispose(script)));
            }
        }