internal static Predicate <PropertyTreeNavigator> ImplicitDirective(PropertyTreeMetaObject target, string name) { var defaultNS = NamespaceUri.Default + name; var langNS = Xmlns.PropertyTrees2010Uri + name; var existing = (PropertyNodeDefinition)target.SelectProperty(defaultNS, allowDefaultProperties: false) ?? target.SelectOperator(defaultNS); if (existing == null) { return(t => t.Name == name); } else { return(t => t.QualifiedName == langNS); } }
bool IApplyMemberStep.Apply(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator node) { PropertyDefinition prop; if (_allowDefault) { prop = target.GetDefinition().DefaultProperty; if (prop == null) return false; } else { var im = ImpliedName(node, target); prop = target.SelectProperty(im); if (prop == null || prop.IsIndexer) return false; } DoPropertyBind(parent, target, node, prop); return true; }
public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children) { Predicate <PropertyTreeNavigator> predicate = ImplicitDirective(target, "id"); var node = children.FindAndRemove(predicate).FirstOrDefault(); if (node == null) { node = children.FindAndRemove("name").FirstOrDefault(); } if (node != null) { // TODO Handle when a name is duplicated or contains whitespace var ns = parent.FindNameScope(target); string id = Convert.ToString(node.Value); if (string.IsNullOrEmpty(id)) { parent._errors.IdCannotBeBlank(node.FileLocation); } else if (ns.FindName(id) == null) { ns.RegisterName(id, target.Component); } else { parent._errors.IdAlreadyRegistered(id, node.FileLocation); } var nameProperty = target.SelectProperty(NamespaceUri.Default + "name"); if (nameProperty != null) { parent.DoPropertyBind(target, node, nameProperty); } } return(target); }
bool IApplyMemberStep.Apply(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator node) { PropertyDefinition prop = null; if (_allowDefault) { var defaultProperties = target.GetDefinition().DefaultProperties; if (defaultProperties.Skip(1).Any()) { prop = defaultProperties.FirstOrDefault(p => p.TryGetValue(target.Component, node.QualifiedName)); } if (prop == null) { prop = defaultProperties.FirstOrDefault(); } if (prop == null) { return(false); } } else { var im = ImpliedName(node, target); prop = target.SelectProperty(im); if (prop == null || prop.IsIndexer) { return(false); } } parent.DoPropertyBind(target, node, prop); return(true); }
public Dictionary <string, PropertyTreeMetaObject> ExtractParameterDictionary( OperatorDefinition op, PropertyTreeMetaObject target, IServiceProvider serviceProvider, NodeList children) { // Named constructor arguments var duplicates = new HashSet <QualifiedName>(); var mapped = new Dictionary <QualifiedName, PropertyTreeNavigator>(); foreach (var child in children) { // Implicitly map default NS to real var impliedName = ImpliedName(child, target); if (duplicates.Contains(impliedName)) { // Duplicates can't bind to parameters (only to param arrays) } else if (mapped.ContainsKey(impliedName)) { // Detected a duplicate duplicates.Add(impliedName); mapped.Remove(impliedName); } else { mapped.Add(impliedName, child); } } var args = new Dictionary <string, PropertyTreeMetaObject>(op.Parameters.Count); PropertyDefinition myParam = null; List <string> requiredMissing = new List <string>(); foreach (PropertyDefinition p in op.Parameters) { // Fallback to empty ns PropertyTreeNavigator nav; QualifiedName impliedName = p.QualifiedName; if (p.QualifiedName.Namespace.IsDefault) { impliedName = impliedName.ChangeNamespace(op.Namespace); } if (mapped.TryGetValue(impliedName, out nav)) { // Binds a parameter required for activating an instance // TODO Should we supply/use attributes from the parameter // and/or corresponding property descriptor? var childContext = target.CreateChild(p.PropertyType); args[p.Name] = Bind(childContext, nav, serviceProvider); children.Remove(nav); } else if (p.IsOptional) { PropertyTreeMetaObject defaultValue; if (p.DefaultValue == null) { defaultValue = PropertyTreeMetaObject.Create(p.PropertyType); } else { defaultValue = PropertyTreeMetaObject.Create(p.DefaultValue); } args[p.Name] = defaultValue; } else if (p.IsParamArray) { myParam = p; } else if (TypeHelper.IsParameterRequired(p.PropertyType)) { requiredMissing.Add(Utility.DisplayName(p.QualifiedName)); } } if (requiredMissing.Count > 0) { _errors.RequiredPropertiesMissing(requiredMissing, op, FindFileLocation(serviceProvider)); } if (myParam == null && !target.GetDefinition().DefaultProperties.Any() && duplicates.Any(t => target.SelectProperty(t) != null)) { _errors.DuplicatePropertyName(duplicates, FindFileLocation(serviceProvider)); } // Try param array if (myParam != null) { var all = new List <object>(); var elementType = myParam.PropertyType.GetElementType(); foreach (var kvp in children) { // Bind child nodes so tha latebound applies var childrenList = NodeList.Create(PropertyTreeBinderImpl.SelectChildren(kvp)); var inline = BindChildNodes(PropertyTreeMetaObject.Create(elementType), kvp, childrenList); var inlineVal = inline.Component; all.Add(inlineVal); } children.Clear(); var array = Array.CreateInstance(elementType, all.Count); ((System.Collections.ICollection)all).CopyTo(array, 0); args[myParam.Name] = PropertyTreeMetaObject.Create(array); } return(args); }
public Dictionary<string, PropertyTreeMetaObject> ExtractParameterDictionary( OperatorDefinition op, PropertyTreeMetaObject target, IServiceProvider serviceProvider, NodeList children) { // Named constructor arguments var duplicates = new HashSet<QualifiedName>(); var mapped = new Dictionary<QualifiedName, PropertyTreeNavigator>(); foreach (var child in children) { // Implicitly map default NS to real var impliedName = ImpliedName(child, target); if (duplicates.Contains(impliedName)) { // Duplicates can't bind to parameters (only to param arrays) } else if (mapped.ContainsKey(impliedName)) { // Detected a duplicate duplicates.Add(impliedName); mapped.Remove(impliedName); } else { mapped.Add(impliedName, child); } } var args = new Dictionary<string, PropertyTreeMetaObject>(op.Parameters.Count); PropertyDefinition myParam = null; List<string> requiredMissing = new List<string>(); foreach (PropertyDefinition p in op.Parameters) { // Fallback to empty ns PropertyTreeNavigator nav; QualifiedName impliedName = p.QualifiedName; if (p.QualifiedName.Namespace.IsDefault) { impliedName = impliedName.ChangeNamespace(op.Namespace); } if (mapped.TryGetValue(impliedName, out nav)) { // Binds a parameter required for activating an instance // TODO Should we supply/use attributes from the parameter // and/or corresponding property descriptor? var childContext = target.CreateChild(p.PropertyType); args[p.Name] = Bind(childContext, nav, serviceProvider); children.Remove(nav); } else if (p.IsOptional) { PropertyTreeMetaObject defaultValue; if (p.DefaultValue == null) defaultValue = PropertyTreeMetaObject.Create(p.PropertyType); else defaultValue = PropertyTreeMetaObject.Create(p.DefaultValue); args[p.Name] = defaultValue; } else if (p.IsParamArray) myParam = p; else if (TypeHelper.IsParameterRequired(p.PropertyType)) { requiredMissing.Add(Utility.DisplayName(p.QualifiedName)); } } if (requiredMissing.Count > 0) errors.RequiredPropertiesMissing(requiredMissing, op, FindFileLocation(serviceProvider)); if (myParam == null && target.GetDefinition().DefaultProperty == null && duplicates.Any(t => target.SelectProperty(t) != null)) errors.DuplicatePropertyName(duplicates, FindFileLocation(serviceProvider)); // Try param array if (myParam != null) { var all = new List<object>(); var elementType = myParam.PropertyType.GetElementType(); foreach (var kvp in children) { // Bind child nodes so tha latebound applies var childrenList = NodeList.Create(PropertyTreeBinderImpl.SelectChildren(kvp)); var inline = BindChildNodes(PropertyTreeMetaObject.Create(elementType), kvp, childrenList); var inlineVal = inline.Component; all.Add(inlineVal); } children.Clear(); var array = Array.CreateInstance(elementType, all.Count); ((System.Collections.ICollection) all).CopyTo(array, 0); args[myParam.Name] = PropertyTreeMetaObject.Create(array); } return args; }