Example #1
0
        /** Used by group creation routine, not by users */
        internal Template(TemplateGroup group)
        {
            if (group == null)
                throw new ArgumentNullException("group");

            this.groupThatCreatedThisInstance = group;

            if (group.TrackCreationEvents)
            {
                if (_debugState == null)
                    _debugState = new TemplateDebugState();
                _debugState.NewTemplateEvent = new ConstructionEvent();
            }
        }
Example #2
0
        public virtual Template Add(string name, object value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.IndexOf('.') >= 0)
            {
                throw new ArgumentException("cannot have '.' in attribute names");
            }

            if (Group.TrackCreationEvents)
            {
                if (_debugState == null)
                {
                    _debugState = new TemplateDebugState();
                }
                _debugState.AddAttributeEvents.Add(name, new AddAttributeEvent(name, value));
            }

            FormalArgument arg = null;

            if (impl.HasFormalArgs)
            {
                arg = impl.TryGetFormalArgument(name);
                if (arg == null)
                {
                    throw new ArgumentException("no such attribute: " + name);
                }
            }
            else
            {
                // define and make room in locals (a hack to make new Template("simple template") work.)
                arg = impl.TryGetFormalArgument(name);
                if (arg == null)
                {
                    // not defined
                    arg = new FormalArgument(name);
                    impl.AddArgument(arg);
                    if (locals == null)
                    {
                        locals = new object[1];
                    }
                    else
                    {
                        Array.Resize(ref locals, impl.FormalArguments.Count);
                    }

                    locals[arg.Index] = EmptyAttribute;
                }
            }

            object curvalue = locals[arg.Index];

            if (curvalue == EmptyAttribute)
            {
                // new attribute
                locals[arg.Index] = value;
                return(this);
            }

            // attribute will be multi-valued for sure now
            // convert current attribute to list if not already
            // copy-on-Write semantics; copy a list injected by user to Add new value
            AttributeList multi = ConvertToAttributeList(curvalue);

            locals[arg.Index] = multi; // replace with list

            // now, Add incoming value to multi-valued attribute
            IList list = value as IList;

            if (list != null)
            {
                // flatten incoming list into existing list
                multi.AddRange(list.Cast <object>());
            }
            else
            {
                multi.Add(value);
            }

            return(this);
        }