/** Find an attr in this template only. */ public virtual object GetAttribute(string name) { FormalArgument localArg = impl.TryGetFormalArgument(name); if (localArg != null) { object o = locals[localArg.Index]; if (o == Template.EmptyAttribute) { o = null; } return(o); } return(null); // if (context != null) // { // p = context.Template; // context = context.Parent; // } // else // { // p = null; // } //// got to root template and no definition, try dictionaries in group //if (impl.NativeGroup.IsDictionary(name)) //{ // return impl.NativeGroup.RawGetDictionary(name); //} //throw new TemplateNoSuchPropertyException(name); }
/** Find an attr via dynamic scoping up enclosing Template chain. * If not found, look for a map. So attributes sent in to a template * override dictionary names. */ public virtual object GetAttribute(string name) { Template p = this; while (p != null) { FormalArgument localArg = p.impl.TryGetFormalArgument(name); if (localArg != null) { object o = p.locals[localArg.Index]; if (o == Template.EmptyAttribute) { o = null; } return(o); } p = p.EnclosingInstance; } // got to root template and no definition, try dictionaries in group if (impl.NativeGroup.IsDictionary(name)) { return(impl.NativeGroup.RawGetDictionary(name)); } throw new TemplateNoSuchPropertyException(name); }
/** Set this.locals attr value when you only know the name, not the index. * This is ultimately invoked by calling Template.Add() from outside so toss * an exception to notify them. */ protected internal virtual void RawSetAttribute(string name, object value) { if (impl.FormalArguments == null) throw new ArgumentException("no such attribute: " + name); FormalArgument arg = impl.TryGetFormalArgument(name); if (arg == null) throw new ArgumentException("no such attribute: " + name); locals[arg.Index] = value; }
/** Remove an attribute value entirely (can't Remove attribute definitions). */ public virtual void Remove(string name) { if (impl.FormalArguments == null) { if (impl.HasFormalArgs) throw new ArgumentException("no such attribute: " + name); return; } FormalArgument arg = impl.TryGetFormalArgument(name); if (arg == null) throw new ArgumentException("no such attribute: " + name); locals[arg.Index] = EmptyAttribute; // reset value }
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); }