コード例 #1
0
        /// <summary>
        /// Replaces the text variables by their formatted values.
        /// The original text in the argument remains unchanged.
        /// </summary>
        private StringBuilder ReplaceTextVariablesWithValues(StringBuilder text, List <Variable> textVariables)
        {
            // work on a copy of the orginal text
            var textResult = new StringBuilder(text.ToString());

            if (textResult.Length == 0)
            {
                return(textResult);
            }

            // first read in all external files - this is done only once - no recursion.
            bool textFileWasIncluded = false;

            foreach (Variable variable in textVariables.Where(variable => !string.IsNullOrEmpty(variable.Format) && _formatAsFilename == variable.Format.ToLower()))
            {
                // If a format name called "file" is found, the variable's value is interpreted as a file name
                // The file name may contain variables as well.
                using (TextVariableManager tvm = Clone())
                {
                    string filename = tvm.Process(variable.Value as string ?? string.Empty);
                    // add all new bad variable names to the list
                    foreach (string badVar in tvm.BadVariables.Where(badVar => !BadVariables.Contains(badVar)))
                    {
                        BadVariables.Add(badVar);
                    }
                    filename = MakeFullPath(filename);
                    string content = ReadFile(filename);

                    if (content == null)
                    {
                        if (FileVariableErrors == VariableError.ReplaceWithEmptyString)
                        {
                            textResult = textResult.Replace(variable.MatchingString, string.Empty);
                        }
                    }
                    else
                    {
                        textResult = textResult.Replace(variable.MatchingString, content);
                    }

                    textFileWasIncluded = true;
                }
            }

            // if a text file was included, re-search and replace the text for variables
            if (textFileWasIncluded)
            {
                foreach (Variable variable in SearchTextVariables(textResult).Where(variable => variable.Format != _formatAsFilename || FileVariableErrors != VariableError.ShowTextVariable))
                {
                    textResult = textResult.Replace(variable.MatchingString, variable.ToString());
                }
            }

            // Next replace variables for the whole text (including any text files read before).
            // If a text file was included, it could again contain variables - but do NOT process
            // external text files recursively.

            return(textVariables.Aggregate(textResult, (current, variable) => current.Replace(variable.MatchingString, variable.ToString())));
        }
コード例 #2
0
        /// <summary>
        /// Processes the supplied text in a way that all text variables
        /// are replaced by the values of the current data item.
        /// </summary>
        /// <param name="text">The text with text variables to be processed.</param>
        /// <param name="clearBadVarsAndFiles">If true, BadFiles and BadVariable lists will be cleared before the process, else not.</param>
        /// <returns>Returns the text with all text variables replaced by the values of the current data item.</returns>
        public StringBuilder Process(StringBuilder text, bool clearBadVarsAndFiles)
        {
            if (clearBadVarsAndFiles)
            {
                BadFiles.Clear();
                BadVariables.Clear();
            }

            return(ReplaceTextVariablesWithValues(text, SearchTextVariables(text)));
        }
コード例 #3
0
 private void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             // Dispose managed resources.
             BadFiles.Clear();
             BadVariables.Clear();
         }
     }
     _disposed = true;
 }
コード例 #4
0
        /// <summary>
        /// Assigns the Value of the CurrentDataItem for a Variable.
        /// If CurrentDataItem contains hierarchical objects, the values
        /// for the sub-objects can be accessed like that:
        /// "TopObjectName.SubObject1Name.SubObject11Name".
        /// This will come in handy when using O/R mappers like LLblGenPro or alike.
        /// </summary>
        /// <param name="variable">The Variable for which the Value shall be assigned.</param>
        private void GetVariableValue(Variable variable)
        {
            string[] hierarchie  = variable.Name.Split(new[] { '.' });
            object   currentData = CurrentDataItem;
            string   orginalProperty;

            foreach (string var in hierarchie)
            {
                orginalProperty = GetOriginalFieldName(var);
                PropertyDescriptor currProp = TypeDescriptor.GetProperties(currentData).Find(orginalProperty, false);
                if (currProp == null)
                {
                    // save missing variable name if not already saved
                    if (!BadVariables.Contains(variable.Name))
                    {
                        BadVariables.Add(variable.Name);
                    }

                    switch (VariableErrors)
                    {
                    case VariableError.ReplaceWithEmptyString:
                        variable.Value = string.Empty;
                        return;

                    case VariableError.ShowTextVariable:
                        variable.Name = null;
                        return;

                    case VariableError.ThrowException:
                        throw new ArgumentOutOfRangeException(variable.Name, _varLeft + variable.Name + _varRight,
                                                              "Variable not found in data source");
                    }

                    return;
                }
                currentData = currProp.GetValue(currentData);
                if (string.IsNullOrEmpty(currentData.ToString()))
                {
                    currentData = GetAlternateValue(orginalProperty);
                }
            }
            variable.Value = currentData;
        }
コード例 #5
0
        private void HandleMissingOrUnreadableVariable(Variable variable)
        {
            // save missing variable name if not already saved
            if (!BadVariables.Contains(variable.Name))
            {
                BadVariables.Add(variable.Name);
            }

            switch (VariableErrors)
            {
            case VariableError.ReplaceWithEmptyString:
                variable.Value = string.Empty;
                return;

            case VariableError.ShowTextVariable:
                variable.Name = null;
                return;

            case VariableError.ThrowException:
                throw new ArgumentOutOfRangeException(variable.Name, _varLeft + variable.Name + _varRight, "Variable not found in data source");
            }
        }