Пример #1
0
        /// <summary>
        /// 执行变量
        /// </summary>
        /// <param name="txt">原始字符串</param>
        /// <param name="ParameterValues">保存参数的列表</param>
        /// <returns>处理后的字符串</returns>
        public string Execute(string txt, System.Collections.ArrayList ParameterValues)
        {
            if (this.myVariables == null)
            {
                throw new System.InvalidOperationException("未设置 Variables 属性");
            }

            if (txt == null || txt.Length == 0)
            {
                return(txt);
            }

            string[] strItems = AnalyseVariableString(txt, strVariablePrefix, strVariableEndfix);
            if (strItems == null)
            {
                return(null);
            }
            System.Text.StringBuilder myStr = new System.Text.StringBuilder();
            for (int iCount = 0; iCount < strItems.Length; iCount++)
            {
                if ((iCount % 2) == 0)
                {
                    myStr.Append(strItems[iCount]);
                }
                else
                {
                    string strName      = strItems[iCount];
                    bool   bolParameter = strName.StartsWith("@");
                    if (bolParameter)
                    {
                        strName = strName.Substring(1);
                    }
                    string strValue = null;
                    if (myVariables.Exists(strName))
                    {
                        strValue = myVariables.Get(strName);
                    }
                    else
                    {
                        strValue = "";
                    }
                    if (ParameterValues != null && bolParameter)
                    {
                        ParameterValues.Add(strValue);
                        myStr.Append(" ? ");
                    }
                    else
                    {
                        myStr.Append(strValue);
                    }
                }
            }
            return(myStr.ToString());
        }
Пример #2
0
        /// <summary>
        /// Formats the source text based on the given provider
        /// </summary>
        /// <param name="sourceText">The text to format</param>
        /// <param name="regions">The regions containing text</param>
        /// <param name="provider">The provider to use when replacing variables</param>
        /// <returns>The formatted text</returns>
        public static string FormatText(string sourceText, IList <Region> regions, IVariableProvider provider)
        {
            if (string.IsNullOrEmpty(sourceText))
            {
                return(string.Empty);
            }

            if (regions.Count == 0)
            {
                return(sourceText);
            }

            if (provider == null)
            {
                return(sourceText);
            }

            StringBuilder builder = new StringBuilder();

            int startIndex = 0;
            int endIndex   = 0;

            // go through each region and replace this area in the text with the actual
            // value for the variable
            foreach (Region region in regions)
            {
                int length = region.AbsolutePosition - startIndex;

                // check for text before the first anchor
                if (region.AbsolutePosition > 0)
                {
                    builder.Append(sourceText.Substring(startIndex, length));
                }

                builder.Append(provider.Get(region.RegionText));

                startIndex += region.RegionText.Length + OffSet + length;
                endIndex    = startIndex;
            }

            // check for text after the last anchor
            if (endIndex < sourceText.Length)
            {
                builder.Append(sourceText.Substring(endIndex));
            }

            return(builder.ToString());
        }
        /// <summary>
        /// Called to process the input file.
        /// In this case, the input file is passed on the command line to the application
        /// that this instance is configured for.
        /// </summary>
        /// <param name="inputFile">The input file to process</param>
        /// <param name="variables">The variable provider</param>
        /// <param name="next">The next action to call</param>
        public void Process(string inputFile, IVariableProvider variables, Action <string> next)
        {
            string fileName = variables.Get("filename");

            Boolean startClientAsUsual = true;

            Logger.InfoFormat("Process called, original file name is '{0}'", fileName);

            // Check if a specific registry key is available. If it is, copy the resulted PDF to the
            // specific location instead of launch the application.
            RegistryKey key = Registry.CurrentUser.OpenSubKey(printToEwbWebKey);

            if (key != null)
            {
                string destFile = (string)key.GetValue(saveToDisk);

                if (destFile != null)
                {
                    Logger.InfoFormat("{0} key is found. Saving resulted PDF to specific location.", saveToDisk);
                    try
                    {
                        Logger.InfoFormat("Copying resulted PDF file from {0} to {1}", inputFile, destFile);
                        File.Copy(inputFile, destFile);

                        if (File.Exists(destFile))
                        {
                            // Do not start client.
                            startClientAsUsual = false;
                            Logger.InfoFormat("Copy success. Deleting registry key value.");
                            Registry.CurrentUser.OpenSubKey(printToEwbWebKey, true).DeleteValue("SaveToDisk");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.ErrorFormat(e.ToString());
                        Logger.InfoFormat("An error has occurred when saving resulted PDF into specific location. Deleting registry key value.");
                        Registry.CurrentUser.OpenSubKey(printToEwbWebKey, true).DeleteValue("SaveToDisk");
                        Logger.InfoFormat("Launching Print to E-WorkBook Web instead.");
                    }
                }
            }

            if (startClientAsUsual)
            {
                StartClient(inputFile, fileName, next);
            }
        }