/// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="parent">The template item that contains this item.</param>
        /// <param name="name">The name of this template item.</param>
        /// <param name="lineNumber">The line number within the xml template file.</param>
        /// <param name="positionInLine">The column number within the xml template file.</param>
        /// <param name="functionName">The name of the function include parameters.</param>
        /// <param name="valueFunction">The custom function for this formula</param>
        /// <param name="parameterNames">The parameter names if the function is parameterized (otherwise null).</param>
        protected internal CustomFormulaTemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine, string functionName, Delegate valueFunction, string [] parameterNames) :
            base(parent, name, lineNumber, positionInLine)
        {
            ParameterCheck.ParameterRequired(valueFunction, "valueFunction");
            ParameterCheck.StringRequiredAndNotWhitespace(functionName, "functionName");

            this.valueFunction = new CustomFormula(functionName, valueFunction);
            this.functionName = functionName;
            this.parameterNames = parameterNames;
        }
Exemplo n.º 2
0
		/// <summary>
		/// Construct an instance.
		/// </summary>
		/// <param name="parent">The template item that contains this item.</param>
		/// <param name="name">The name of this item.</param>
		/// <param name="lineNumber">This item's line number position within the xml template file.</param>
		/// <param name="positionInLine">This item's column position within the xml template file.</param>
		protected internal TemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine)
		{
			this.name = name;
			this.lineNumber = lineNumber;
			this.positionInLine = positionInLine;
			this.parent = parent;
			if (this.parent != null)
			{
				this.parent.children.Add(this);
			}

			if (name.IndexOf(".") != -1)
			{
				string [] parts = name.Split('.');
				if (parts.Length > 2) {
				    string mess =
				        string.Format(
				            Messages.TemplateItem_TemplateItem_VariableNameIsNotLegal,
				            name);
                    logger.Error(string.Format(Messages.ErrorAtLineAndColumn, mess, this.lineNumber, this.positionInLine));
                    throw new TemplateExpansionException(this.lineNumber, this.positionInLine, mess);
				}

				if (parts.Length == 2)
				{
					TemplateItem currentParent = this;
					while (parentTableItem == null && currentParent.Parent != null)
					{
						currentParent = currentParent.Parent;
						if (currentParent is TableTemplateItem)
						{
							if (((TableTemplateItem)currentParent).IsTableOwner(parts[0]))
							{
								parentTableItem = (TableTemplateItem)currentParent;
							}
						}
					}

					this.name = parts[1];
				}
			}
		}
        FormulaTemplateItem CreateCustomFormulaIfRegistered(TemplateItem parent, string name, int lineNumber, int positionInLine,
                                                     string functionName, ITemplateEngineSettings templateEngineSettings, string [] parameterNames)
        {
            IDictionary<string, Delegate> customFormulas = templateEngineSettings.CustomFormulas;
            int openParenPos = functionName.IndexOf("(");
            string funcNameWithoutParameters = functionName;
            if (openParenPos > -1) {
                funcNameWithoutParameters = functionName.Substring(0, openParenPos);
            }
            if (additionalCustomFormulas != null && additionalCustomFormulas.ContainsKey(funcNameWithoutParameters)) {
                return new CustomFormulaTemplateItem(parent, name, lineNumber, positionInLine, functionName,
                                                     additionalCustomFormulas[funcNameWithoutParameters], parameterNames);
            }
            if (customFormulas.ContainsKey(funcNameWithoutParameters)) {
                return new CustomFormulaTemplateItem(parent, name, lineNumber, positionInLine, functionName,
                                                     customFormulas[funcNameWithoutParameters], parameterNames);
            }

            return null;
        }
        /// <summary>
        /// Construct a descendant of FormulaTemplateItem
        /// </summary>
        /// <param name="parent">Parent that contains this template item.</param>
        /// <param name="name">The name of this template item.</param>
        /// <param name="lineNumber">Line number within the xml template file.</param>
        /// <param name="positionInLine">Column number within the xml template file.</param>
        /// <param name="functionNameWithParameterList">The name of the function.</param>
        /// <param name="templateEngineSettings">The template engine settings.</param>
        /// <returns>A FormulaTemplateItem descendant.</returns>
        public FormulaTemplateItem CreateFormulaItem(TemplateItem parent, string name, int lineNumber, int positionInLine,
                                                     string functionNameWithParameterList, ITemplateEngineSettings templateEngineSettings) {
            ParameterCheck.StringRequiredAndNotWhitespace(name, "name");
            ParameterCheck.StringRequiredAndNotWhitespace(functionNameWithParameterList, "functionName");

            var openingParenPosition = functionNameWithParameterList.IndexOf("(");
            var closingParenPosition = functionNameWithParameterList.IndexOf(")");
            var functionName = functionNameWithParameterList.Substring(0, openingParenPosition) + "()";

            string[] parameterNames = null;
            if (closingParenPosition - openingParenPosition > 1) {
                var startArgumentList = openingParenPosition + 1;
                var lengthArgumentList = closingParenPosition - startArgumentList;
                parameterNames = functionNameWithParameterList.Substring(startArgumentList, lengthArgumentList).Split(',');
               for (int i = 0; i < parameterNames.Length; i++) {
                   parameterNames[i] = parameterNames[i].Trim();
               }
            }

            var customFormulaTemplateItem = CreateCustomFormulaIfRegistered(parent, name, lineNumber, positionInLine, functionName,
                                                                            templateEngineSettings, parameterNames);
            if (customFormulaTemplateItem != null) return customFormulaTemplateItem;

            if (functionName == "generate()") {
                return new GenerateFormulaTemplateItem(parent, name, lineNumber, positionInLine);
            }
            if (functionName == "now()") {
                return new NowFormulaTemplateItem(parent, name, lineNumber, positionInLine);
            }
            if (functionName == "jdfDefault()") {
                return new JdfDefaultFormulaTemplateItem(parent, name, lineNumber, positionInLine);
            }

            string mess = string.Format(Messages.FormulaTemplateItemFactory_CreateFormulaItem_InvalidFunctionNameMessage, functionNameWithParameterList);
            logger.ErrorFormat(Messages.ErrorAtLineAndColumn, mess, lineNumber, positionInLine);
            throw new TemplateExpansionException(lineNumber, positionInLine, mess);
        }
		/// <summary>
		/// Add a template item to the collection.
		/// </summary>
		/// <param name="item">The template item to be added.</param>
		/// <returns>The newly added template item.</returns>
		protected internal TemplateItem Add(TemplateItem item)
		{
			_items.Add(item);
			return item;
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="parent">The template item that contains this item.</param>
		/// <param name="name">The name of this template item.</param>
		/// <param name="lineNumber">The line number within the xml template file.</param>
		/// <param name="positionInLine">The column number withing the xml template file.</param>
		protected internal JdfDefaultFormulaTemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine) :
			base(parent, name, lineNumber, positionInLine)
		{
		}
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="parent">The parent item that contains this item.</param>
		/// <param name="name">The name of this item.</param>
		/// <param name="lineNumber">This item's line number within the xml template file.</param>
		/// <param name="positionInLine">This item's column number within the xml template file.</param>
		/// <param name="defaultValue">The default value used if a replacement value is not provided.  If this is null, the replacement data is 
		/// required.</param>
		/// <remarks>
		/// By default, variable names that contain a dot are assumed to come from a containing table.  That is, names in the form tableName.fieldName
		/// are assumed when there is a dot.  By default, fields can contain zero or one dot.  You can override this behavior by adding the DottedTemplateVarsAlwaysTables
		/// configuration item to your JdpSettings with value="false".  In this case, if the variable name contains more than one dot or a table with the correct
		/// name is not found in the template, the system will attempt to find a replacement variable in the StringDictionary with the dotted name.
		/// </remarks>
		protected internal VariableTemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine, string defaultValue) :
			base(parent, name, lineNumber, positionInLine)
		{
			this.defaultValue = defaultValue;
		}
		/// <summary>
		/// Construct an item.
		/// </summary>
		/// <param name="parent">The template item that contains this item.</param>
		/// <param name="name">The name of this item.</param>
		/// <param name="lineNumber">The line number in the xml template file.</param>
		/// <param name="positionInLine">The column number in the xml template file.</param>
		/// <param name="text">The static text this item will represent.</param>
		protected internal StaticTemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine, string text) :
			base(parent, name, lineNumber, positionInLine)
		{
			_text = text;
		}
		/// <summary>
		/// Create an instance.
		/// </summary>
		/// <param name="parent">The template item that contains this item.</param>
		/// <param name="name">The name of this item.</param>
		/// <param name="lineNumber">This items's line number within the xml template file.</param>
		/// <param name="positionInLine">This item's column number within the xml template file.</param>
		/// <param name="tableName">The key of the item in the name/values dictionary that is the table.</param>
		protected internal TableTemplateItem(TemplateItem parent, string name, int lineNumber, int positionInLine, string tableName) :
			base(parent, name, lineNumber, positionInLine)
		{
			this.tableName = tableName;
		}