private void DoPropertyBind(PropertyTreeBinderImpl parent,
                                        PropertyTreeMetaObject target,
                                        PropertyTreeNavigator navigator,
                                        PropertyDefinition property)
            {
                object ancestor = null;
                PropertyTreeMetaObject ancestorMeta = null;

                if (property.IsExtender) {
                    var ancestorType = property.DeclaringTreeDefinition.SourceClrType;
                    ancestorMeta = target.GetAncestors().Cast<PropertyTreeMetaObject>().FirstOrDefault(
                        t => ancestorType.IsAssignableFrom(t.ComponentType));

                    if (ancestorMeta != null)
                        ancestor = ancestorMeta.Component;
                }

                var component = target.Component;
                PropertyTreeMetaObject propertyTarget = target.CreateChild(property, navigator.QualifiedName, ancestorMeta);

                var services = new PropertyBindContext(
                    component,
                    property,
                    ServiceProvider.Compose(ServiceProvider.FromValue(navigator), parent))
                {
                    LineNumber = navigator.LineNumber,
                    LinePosition = navigator.LinePosition,
                };

                propertyTarget = parent.Bind(propertyTarget, navigator, services);
                target.BindSetMember(property, navigator.QualifiedName, propertyTarget, ancestorMeta, services);
            }
            public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent,
                                                           PropertyTreeMetaObject target,
                                                           PropertyTreeNavigator self,
                                                           NodeList children)
            {
                Type type = target.ComponentType ?? typeof(object);

                // Select providers
                if (type.IsProviderType())
                {
                    var node = children.FindAndRemove(ImplicitDirective(target, "provider")).FirstOrDefault();

                    if (node != null)
                    {
                        var serviceProvider = parent.GetBasicServices(node);
                        var pro             = parent.DirectiveFactory.CreateTargetProvider(type, node);
                        if (pro != null)
                        {
                            target = target.BindTargetProvider(pro, serviceProvider);
                            return(target);
                        }
                    }
                }

                return(target);
            }
Beispiel #3
0
        public override PropertyTreeMetaObject BindInitializer(Expression expression, IExpressionContext context, IServiceProvider serviceProvider)
        {
            // No need to evaluate if the parent can accept expressions
            // TODO There will be other conditions for allowing expressions
            if (CanAllowExpressionInitializer(Parent))
            {
                return(PropertyTreeMetaObject.Create(expression));
            }

            var result = expression.Evaluate(context ?? ExpressionContext.Empty);

            if (result == Undefined.Value)
            {
                return(PropertyTreeMetaObject.Null);
            }
            var items = result as object[];

            if (items != null)
            {
                if (items.Length == 0 || items.All(t => t == Undefined.Value))
                {
                    return(PropertyTreeMetaObject.Null);
                }
                result = string.Concat(items);
            }

            return(BindInitializeValue(result.ToString(), serviceProvider));
        }
            public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                foreach (var child in children.Rest()) {

                    string msg;
                    if (target.ComponentType.IsHiddenUX()) {
                        msg = SR.BinderMissingPropertyNoType(child.QualifiedName);
                    } else {
                        msg = SR.BinderMissingProperty(child.QualifiedName, target.ComponentType);
                    }

                    try {
                        var info = new InterfaceUsageInfo(InterfaceUsage.Missing, msg, null, true);
                        Parent.Callback.OnPropertyAnnotation(child.QualifiedName.ToString(), info);

                    } catch (Exception ex) {
                        if (ex.IsCriticalException())
                            throw;

                        throw PropertyTreesFailure.UnmatchedMembersGenericError(ex, child.FileLocation);
                    }
                }

                return target;
            }
Beispiel #5
0
        public override PropertyTreeMetaObject BindInitializeValue(string text, IServiceProvider serviceProvider)
        {
            object value;

            TryConvertFromText(text, serviceProvider, out value);
            return(PropertyTreeMetaObject.Create(value));
        }
            public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                Predicate<PropertyTreeNavigator> predicate = ImplicitDirective(target, "source");

                var node = children.FindAndRemove(predicate).FirstOrDefault();
                if (node != null) {
                    IServiceProvider serviceProvider = Parent.GetBasicServices(node);
                    var uriContext = node as IUriContext;
                    TargetSourceDirective ss;
                    ss = this.DirectiveFactory.CreateTargetSource(node, uriContext);

                    if (ss != null) {
                        try {
                            target = target.BindStreamingSource(ss, serviceProvider);
                        } catch (Exception ex) {
                            if (ex.IsCriticalException())
                                throw;

                            Parent.errors.FailedToLoadFromSource(ss.Uri, ex, node.FileLocation);
                        }
                    }
                }

                return target;
            }
            private PropertyTreeMetaObject DoOperatorBind(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator navigator, OperatorDefinition op)
            {
                OperatorDefinition addon = op;

                if (addon.DefaultParameter != null) {
                    Type itemType = ((PropertyTreeFactoryDefinition) addon).OutputType;
                    // TODO Use service activation (we have the output type)

                    var item = target.CreateChild(itemType);
                    var model = parent.Bind(item, navigator, null);
                    var args = new Dictionary<string, PropertyTreeMetaObject>
                    {
                        { addon.DefaultParameter.Name, model }
                    };

                    try {
                        target.BindAddChild(op, args);

                    } catch (Exception ex) {
                        if (ex.IsCriticalException())
                            throw;

                        parent.errors.BadAddChild(target.ComponentType, ex, navigator.FileLocation);
                    }

                } else {
                    // TODO The number and kinds of arguments are constrained.  This should probably
                    // be enforced within schema operator reflection (spec)

                    Action<IReadOnlyDictionary<string, PropertyTreeMetaObject>> func;
                    var children = NodeList.Create(SelectChildren(navigator));

                    switch (addon.OperatorType) {
                        case OperatorType.Add:
                            func = args => {
                                var child = target.BindAddChild(addon, args);

                                if (child.ShouldBindChildren) {
                                    parent.BindChildNodes(child, navigator, children);
                                }
                            };
                            break;

                        case OperatorType.Remove:
                            func = args => target.BindRemoveChild(addon, args);
                            break;

                        case OperatorType.Clear:
                        default:
                            func = args => target.BindClearChildren(addon, args);
                            break;
                    }

                    var services = parent.GetBasicServices(navigator);
                    var args2 = parent.ExtractParameterDictionary(op, target, services, children);
                    func(args2);
                }

                return target;
            }
            public override PropertyTreeMetaObject StartStep(
                PropertyTreeMetaObject target,
                PropertyTreeNavigator self,
                NodeList children)
            {
                if (!(target is UntypedToTypedMetaObject))
                    return target;

                if (!children.Any())
                    return target;

                try {
                    // TODO Only supports one child (lame spec)
                    var rootType = target.Root.ComponentType;
                    var types = children.Select(t => ConvertToType(t, rootType)).ToArray();

                    target = target.BindGenericParameters(types);

                } catch (Exception ex) {
                    if (ex.IsCriticalException())
                        throw;

                    Parent.errors.CouldNotBindGenericParameters(target.ComponentType, ex, self.FileLocation);
                }

                Parent.Bind(target, children.First(), null);
                children.Clear();
                return target;
            }
Beispiel #9
0
        public override PropertyTreeMetaObject BindStreamingSource(StreamContext input, IServiceProvider serviceProvider)
        {
            var ss   = StreamingSource.Create(this.ComponentType);
            var comp = ss.Load(input, this.ComponentType);

            return(PropertyTreeMetaObject.Create(comp));
        }
Beispiel #10
0
        public override PropertyTreeMetaObject BindInitializeValue(string text, IServiceProvider serviceProvider)
        {
            object value = text;

            if (ComponentType != typeof(string) && ComponentType != typeof(object))
            {
                TryConvertFromText(text, serviceProvider, out value);
            }
            return(PropertyTreeMetaObject.Create(value, this.ComponentType));
        }
Beispiel #11
0
        public override PropertyTreeMetaObject BindInitializer(Expression expression, IExpressionContext context, IServiceProvider serviceProvider)
        {
            if (Parent.IsExpressionBag)
            {
                return(PropertyTreeMetaObject.Create(expression));
            }
            object value = expression.Evaluate(context ?? ExpressionContext.Empty);

            return(PropertyTreeMetaObject.Create(value));
        }
            public override PropertyTreeMetaObject EndStep(PropertyTreeMetaObject target)
            {
                if (target.ShouldConstruct) {
                    var ctor = target.GetDefinition().Constructor;

                    if (ctor != null)
                        return target.BindConstructor(ctor, Empty<string, PropertyTreeMetaObject>.ReadOnlyDictionary);
                }
                return target;
            }
            private void ApplyType(PropertyTreeMetaObject target,
                                   PropertyTreeNavigator node)
            {
                if (node == null)
                    return;

                Type type = target.ComponentType ?? typeof(object);
                var serviceProvider = Parent.GetBasicServices(node);
                target.BindTargetType(this.DirectiveFactory.CreateTargetType(node), serviceProvider);
            }
            private PropertyTreeMetaObject PickBuilderTypeIfAvailable(PropertyTreeMetaObject target)
            {
                if (target.Component == null && target.ComponentType != null) {

                    Type builderType = target.ComponentType.GetBuilderType();
                    if (builderType != null)
                        return new BuilderMetaObject(new PreactivationMetaObject(builderType));
                }

                return target;
            }
        internal PropertyTreeMetaObject BindChildNodes(PropertyTreeMetaObject target,
                                                       PropertyTreeNavigator self,
                                                       NodeList children)
        {
            foreach (var step in Pipeline)
            {
                target = step.Process(this, target, self, children);
            }

            return(target);
        }
            bool IApplyMemberStep.Apply(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator node)
            {
                var member = target.SelectOperator(ImpliedName(node, target));

                if (member != null) {
                    DoOperatorBind(parent, target, node, member);
                    return true;
                }

                return false;
            }
Beispiel #17
0
        public override PropertyTreeMetaObject CreateChild(PropertyDefinition property,
                                                           QualifiedName name,
                                                           PropertyTreeMetaObject ancestor)
        {
            if (property.IsReadOnly)
            {
                AppendCommand(new PushPropertyCommand(property, name));
                return(FromInstanceType(property.PropertyType));
            }

            return(CreateChild(property.PropertyType));
        }
            bool IApplyMemberStep.Apply(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator node)
            {
                var member = target.SelectOperator(ImpliedName(node, target));

                if (member != null)
                {
                    DoOperatorBind(parent, target, node, member);
                    return(true);
                }

                return(false);
            }
Beispiel #19
0
        public override PropertyTreeMetaObject BindInitializeValue(string text, IServiceProvider serviceProvider)
        {
            object value;

            if (TryConvertFromText(text, serviceProvider, out value))
            {
                return(PropertyTreeMetaObject.Create(value));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #20
0
            private PropertyTreeMetaObject PickBuilderTypeIfAvailable(PropertyTreeMetaObject target)
            {
                if (target.Component == null && target.ComponentType != null)
                {
                    Type builderType = target.ComponentType.GetBuilderType();
                    if (builderType != null)
                    {
                        return(new BuilderMetaObject(new PreactivationMetaObject(builderType)));
                    }
                }

                return(target);
            }
Beispiel #21
0
            private void ApplyType(PropertyTreeBinderImpl parent,
                                   PropertyTreeMetaObject target,
                                   PropertyTreeNavigator node)
            {
                if (node == null)
                {
                    return;
                }

                var serviceProvider = parent.GetBasicServices(node);

                target.BindTargetType(parent.DirectiveFactory.CreateTargetType(node), serviceProvider);
            }
            public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                QualifiedName name = self.QualifiedName;
                var ctor = PropertyTreeDefinition.FromType(target.ComponentType).Constructor;

                if (target.ShouldConstruct && ctor != null) {
                    OperatorDefinition op = ctor;
                    var args = ExtractParameterDictionary(op, target, Parent.GetBasicServices(self), children);

                    target = target.BindConstructor(ctor, args);
                }

                return target;
            }
        private void DoPropertyBind(PropertyTreeMetaObject target,
                                    PropertyTreeNavigator navigator,
                                    PropertyDefinition property)
        {
            object ancestor = null;
            PropertyTreeMetaObject ancestorMeta = null;

            if (property.IsExtender)
            {
                var ancestorType = property.DeclaringTreeDefinition.SourceClrType;
                ancestorMeta = target.Ancestors.FirstOrDefault(
                    t => ancestorType.IsAssignableFrom(t.ComponentType));

                if (ancestorMeta != null)
                {
                    ancestor = ancestorMeta.Component;
                }
            }

            var component = target.Component;
            PropertyTreeMetaObject propertyTarget = target.CreateChild(property, navigator.QualifiedName, ancestorMeta);

            var services = new PropertyBindContext(
                component,
                property,
                ServiceProvider.Compose(ServiceProvider.FromValue(navigator), this))
            {
                LineNumber   = navigator.LineNumber,
                LinePosition = navigator.LinePosition,
            };

            try {
                propertyTarget = Bind(propertyTarget, navigator, services);
                target.BindSetMember(property, navigator.QualifiedName, propertyTarget, ancestorMeta, services);
            } catch (NullReferenceException nre) {
                // Normally a "critical" exception, consider it a conversion error
                _errors.BinderConversionError(property.Name, target.ComponentType, nre, navigator.FileLocation);
            } catch (ArgumentException a) {
                _errors.BinderConversionError(property.Name, target.ComponentType, a, navigator.FileLocation);
            } catch (PropertyTreeException) {
                throw;
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                _errors.BinderConversionError(property.Name, target.ComponentType, ex, navigator.FileLocation);
            }
        }
        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);
            }
        }
        public override PropertyTreeMetaObject Bind(PropertyTreeMetaObject target,
                                                    PropertyTreeNavigator navigator,
                                                    IServiceProvider serviceProvider)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (navigator == null)
            {
                throw new ArgumentNullException("navigator");
            }

            if (navigator.IsProperty)
            {
                var textValue = navigator.Value as string;
                if (textValue != null)
                {
                    return(BindInitializeValue(
                               target,
                               navigator,
                               serviceProvider,
                               () => target.BindInitializeValue(textValue, serviceProvider)));
                }

                var exprValue = navigator.Value as Expression;
                if (exprValue != null)
                {
                    var exprContext = ExpressionContext.Compose(
                        CurrentExpressionContext,
                        ExpressionContext.FromNameScope(FindNameScope(target))
                        );
                    exprValue = ExpressionUtility.LiftToCall(exprValue, exprContext);
                    return(BindInitializeValue(
                               target,
                               navigator,
                               serviceProvider,
                               () => target.BindInitializer(exprValue, exprContext, serviceProvider)));
                }

                throw new NotImplementedException();
            }

            var children = SelectChildren(navigator);

            return(BindChildNodes(target, navigator, NodeList.Create(children)));
        }
            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;
            }
        private PropertyTreeMetaObject BindInitializeValue(PropertyTreeMetaObject target,
                                                           PropertyTreeNavigator navigator,
                                                           IServiceProvider serviceProvider,
                                                           Func <PropertyTreeMetaObject> thunk)
        {
            try {
                return(thunk());
            } catch (PropertyTreeException) {
                throw;
            } catch (Exception ex) {
                var sp = ServiceProvider.Compose(ServiceProvider.FromValue(navigator), serviceProvider, this);
                // Throw critical exceptions if they originate within PT; otherwise, allow
                // callback to decide how to handle them.
                if (ex.IsCriticalException())
                {
                    throw;
                }

                var    descriptor = serviceProvider.GetServiceOrDefault <IValueSerializerContext>();
                string property   = navigator.QualifiedName.ToString();
                Type   componentType;

                if (descriptor == null || descriptor.Instance == null)
                {
                    componentType = target.ComponentType;
                }
                else if (descriptor.Property == null)
                {
                    componentType = descriptor.Instance.GetType();
                }
                else
                {
                    property      = descriptor.Property.Name;
                    componentType = descriptor.Property.DeclaringType;
                }

                FileLocation loc   = navigator.FileLocation;
                object       value = navigator.Value;

                _errors.BinderConversionError(property, componentType, ex, loc);
            }

            return(target);
        }
        public override StreamingSource CreateStreamingSource(PropertyTreeMetaObject target, StreamContext input, IServiceProvider serviceProvider)
        {
            Type componentType = target.ComponentType;
            var  uri           = serviceProvider.GetRequiredService <IUriContext>();

            if (input.Uri == uri.BaseUri)
            {
                string name = input.Uri.Fragment.TrimStart('#');
                var    obj  = FindNameScope(target).FindName(name);
                if (obj == null)
                {
                    throw new NotImplementedException();
                }
                return(new ObjectCacheStreamingSource(input.Uri, obj));
            }

            // TODO Should cache streaming sources so that the output objects can immediately be retrieved
            return(StreamingSource.Create(componentType, input.ContentType) ?? new PropertyTreeSource());
        }
        public virtual PropertyTreeMetaObject CreateChild(PropertyDefinition property,
                                                          QualifiedName name,
                                                          PropertyTreeMetaObject ancestor)
        {
            object value;

            property.TryGetValue(this.Component, ancestor, name, out value);
            var propertyType = property.PropertyType;

            // Refine property type if possible
            if (value == null)
            {
                return(CreateChild(propertyType));
            }
            else
            {
                return(CreateChild(value, propertyType));
            }
        }
Beispiel #30
0
        private bool TryAggregation(PropertyTreeMetaObject value,
                                    QualifiedName name,
                                    PropertyDefinition property,
                                    IServiceProvider serviceProvider)
        {
            object current;

            if (!property.TryGetValue(component, null, name, out current))
            {
                return(false);
            }

            var enumerable = value.Component as IEnumerable;

            if (current != null && enumerable != null)
            {
                var items = enumerable;
                if (!ReferenceEquals(current, items) && enumerable.GetEnumerator().MoveNext())
                {
                    MethodInfo mi = FindAddonMethod(current.GetType(), enumerable);

                    if (mi == null)
                    {
                        // Error because aggregation will be needed on read-only properties
                        if (property.IsReadOnly)
                        {
                            var errors = serviceProvider.GetServiceOrDefault(PropertyTreeBinderErrors.Default);
                            errors.NoAddMethodSupported(component.GetType(), PropertyTreeBinderImpl.FindFileLocation(serviceProvider));
                        }
                        return(false);
                    }

                    foreach (var item in items)
                    {
                        mi.Invoke(current, new object[] { item });
                    }
                    // Success because aggregation was applied
                    return(true);
                }
            }
            return(false);
        }
        private static QualifiedName ImpliedName(PropertyTreeNavigator nav, PropertyTreeMetaObject target)
        {
            QualifiedName qualifiedName = nav.QualifiedName;

            if (nav.IsExpressNamespace)
            {
                return(qualifiedName);
            }

            NamespaceUri impliedNS = Utility.GetXmlnsNamespaceSafe(target.ComponentType);

            if (impliedNS == null)
            {
                return(qualifiedName);
            }
            else
            {
                return(qualifiedName.ChangeNamespace(impliedNS));
            }
        }
            public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                Type type = target.ComponentType ?? typeof(object);
                Type concreteClass = type.GetConcreteClass();
                QualifiedName name = self.QualifiedName;
                IServiceProvider serviceProvider = Parent;

                if (concreteClass != null && type != concreteClass) {
                    target.BindTargetType(TypeReference.FromType(concreteClass), serviceProvider);

                } else if (type.IsComposable()) {
                    target.BindTargetProvider(name, null, serviceProvider);
                }

                if (target.Component == null && !target.ComponentType.IsSealed) {
                    // TODO This predicate is probably too loose
                    Predicate<PropertyTreeNavigator> predicate = t => t.Name == "type";
                    var node = children.FindAndRemove(predicate).FirstOrDefault();
                    ApplyType(target, node);
                }
                return target;
            }
            public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target,
                                                             PropertyTreeNavigator self,
                                                             NodeList children)
            {
                Type type = target.ComponentType ?? typeof(object);

                // Select providers
                if (type.IsProviderType()) {
                    var node = children.FindAndRemove(ImplicitDirective(target, "provider")).FirstOrDefault();

                    if (node != null) {
                        var serviceProvider = Parent.GetBasicServices(node);
                        var pro = DirectiveFactory.CreateTargetProvider(type, node);
                        if (pro != null) {
                            target = target.BindTargetProvider(pro, serviceProvider);
                            return target;
                        }
                    }
                }

                return target;
            }
            public override PropertyTreeMetaObject Process(
                PropertyTreeBinderImpl parent,
                PropertyTreeMetaObject target,
                PropertyTreeNavigator self,
                NodeList children)
            {
                if (!(target is UntypedToTypedMetaObject))
                {
                    return(target);
                }

                if (!children.Any())
                {
                    return(target);
                }

                try {
                    // TODO Only supports one child (lame spec)
                    var rootType = target.Root.ComponentType;
                    var types    = children.Select(t => ConvertToType(t, rootType)).ToArray();

                    target = target.BindGenericParameters(types);
                } catch (PropertyTreeException) {
                    throw;
                } catch (Exception ex) {
                    if (ex.IsCriticalException())
                    {
                        throw;
                    }

                    parent._errors.CouldNotBindGenericParameters(target.ComponentType, ex, self.FileLocation);
                }

                parent.Bind(target, children.First(), null);
                children.Clear();
                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 virtual PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target,
                                                 PropertyTreeNavigator self,
                                                 NodeList children)
 {
     return target;
 }
 public PropertyTreeMetaObject Process(
     PropertyTreeMetaObject target,
     PropertyTreeNavigator self,
     NodeList children)
 {
     target = StartStep(target, self, children);
     return EndStep(target);
 }
        public override void BindSetMember(PropertyDefinition property, QualifiedName name, PropertyTreeMetaObject value, PropertyTreeMetaObject ancestor, IServiceProvider serviceProvider)
        {
            if (property.IsReadOnly) {
                TryAggregation(value, name, property, serviceProvider);
                return;
            }

            object outputValue = value.Component;
            object anc = ancestor == null ? null : ancestor.Component;
            SetValueCore(property, name, anc, value, outputValue, serviceProvider);
        }
        public override PropertyTreeMetaObject Bind(PropertyTreeMetaObject target,
                                                    PropertyTreeNavigator navigator,
                                                    IServiceProvider serviceProvider)
        {
            if (target == null)
                throw new ArgumentNullException("target");
            if (navigator == null)
                throw new ArgumentNullException("navigator");

            if (navigator.IsProperty) {
                string value = Convert.ToString(navigator.Value);
                var sp = ServiceProvider.Compose(ServiceProvider.FromValue(navigator), serviceProvider, this);
                var xml = (IXmlNamespaceResolver) sp.GetService(typeof(IXmlNamespaceResolver));
                try {
                    return target.BindInitializeValue(value, sp);

                } catch (Exception ex) {
                    // Throw critical exceptions if they originate within PT; otherwise, allow
                    // callback to decide how to handle them.
                    if (ex.IsCriticalException())
                        throw;

                    var descriptor = serviceProvider.GetService<ITypeDescriptorContext>();
                    string property;
                    Type componentType;
                    if (descriptor == null || descriptor.PropertyDescriptor == null) {
                        property = navigator.QualifiedName.ToString();
                        componentType = descriptor.Instance.GetType();
                    } else {
                        property = descriptor.PropertyDescriptor.Name;
                        componentType = descriptor.PropertyDescriptor.ComponentType;
                    }

                    FileLocation loc = navigator.FileLocation;

                    try {
                        Callback.OnConversionException(property, value, ex);

                    } catch (Exception ex2) {
                        if (Require.IsCriticalException(ex2))
                            throw;

                        throw PropertyTreesFailure.BinderConversionError(value, property, componentType, ex2, loc);
                    }
                }
            }

            var children = SelectChildren(navigator);
            return BindChildNodes(target, navigator, NodeList.Create(children));
        }
Beispiel #40
0
 public abstract PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent,
                                                PropertyTreeMetaObject target,
                                                PropertyTreeNavigator self,
                                                NodeList children);
Beispiel #41
0
            public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                if (target.ShouldConstruct)
                {
                    var ctor = target.GetDefinition().Constructor;

                    if (ctor != null)
                    {
                        return(target.BindConstructor(ctor, Empty <string, PropertyTreeMetaObject> .ReadOnlyDictionary));
                    }
                }
                return(target);
            }
 public override PropertyTreeMetaObject StartStep(PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
 {
     children.FindAndRemove(t => Apply(target, t)).All();
     return target;
 }
 public virtual void BindSetMember(PropertyDefinition property, QualifiedName name, PropertyTreeMetaObject value, PropertyTreeMetaObject ancestor, IServiceProvider serviceProvider)
 {
 }
 public abstract PropertyTreeMetaObject Bind(PropertyTreeMetaObject target, PropertyTreeNavigator navigator, IServiceProvider serviceProvider);
            public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                Predicate <PropertyTreeNavigator> predicate = ImplicitDirective(target, "source");

                var node = children.FindAndRemove(predicate).FirstOrDefault();

                if (node != null)
                {
                    IServiceProvider serviceProvider = parent.GetBasicServices(node);
                    var uriContext = node as IUriContext;
                    TargetSourceDirective ss;
                    ss = parent.DirectiveFactory.CreateTargetSource(node, uriContext);

                    if (ss != null)
                    {
                        try {
                            target = target.BindStreamingSource(ss, serviceProvider);
                        } catch (PropertyTreeException) {
                            throw;
                        } catch (Exception ex) {
                            if (ex.IsCriticalException())
                            {
                                throw;
                            }

                            parent._errors.FailedToLoadFromSource(ss.Uri, ex, node.FileLocation);
                        }
                    }
                }

                return(target);
            }
Beispiel #46
0
            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);
            }
 // TODO Implement property ordering based on [DependsOn]
 private bool Apply(PropertyTreeMetaObject target, PropertyTreeNavigator node)
 {
     return _items.Any(t => t.Apply(Parent, target, node));
 }
 public virtual PropertyTreeMetaObject EndStep(PropertyTreeMetaObject target)
 {
     return target;
 }
            public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
            {
                IServiceProvider serviceProvider = parent.GetBasicServices(self);
                int lineNumber   = self.LineNumber;
                int linePosition = self.LinePosition;

                return(target.BindFileLocation(lineNumber, linePosition, serviceProvider));
            }
        public virtual PropertyTreeMetaObject CreateChild(PropertyDefinition property,
                                                          QualifiedName name,
                                                          PropertyTreeMetaObject ancestor)
        {
            var value = property.GetValue(this.Component, ancestor, name);
            var propertyType = property.PropertyType;

            // Refine property type if possible
            if (value == null) {
                return CreateChild(propertyType);
            }
            else {
                return CreateChild(value, propertyType);
            }
        }
 public override PropertyTreeMetaObject Process(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator self, NodeList children)
 {
     return(target.BindEndObject(parent));
 }
 protected PropertyTreeMetaObject(PropertyTreeMetaObject parent)
 {
     this.Parent = parent;
 }
Beispiel #53
0
        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);
        }
        private void SetValueCore(PropertyDefinition property,
                                  QualifiedName name,
                                  object ancestor,
                                  PropertyTreeMetaObject value,
                                  object outputValue,
                                  IServiceProvider serviceProvider)
        {
            var callback = serviceProvider.TryGetService(PopulateComponentCallback.Null);
            try {
                property.SetValue(component, ancestor, name, value.Component);

            } catch (NullReferenceException nre) {
                // Normally a "critical" exception, consider it a conversion error
                callback.OnConversionException(property.Name, outputValue, nre);

            } catch (ArgumentException a) {
                callback.OnConversionException(property.Name, outputValue, a);

            } catch (Exception ex) {
                if (Require.IsCriticalException(ex))
                    throw;
                callback.OnConversionException(property.Name, outputValue, ex);
            }
        }
 internal Dictionary<string, PropertyTreeMetaObject> ExtractParameterDictionary(
     OperatorDefinition op,
     PropertyTreeMetaObject target,
     IServiceProvider serviceProvider,
     NodeList children)
 {
     return Parent.ExtractParameterDictionary(op, target, serviceProvider, children);
 }
        private void TryAggregation(PropertyTreeMetaObject value,
                                    QualifiedName name,
                                    PropertyDefinition property,
                                    IServiceProvider serviceProvider)
        {
            var current = property.GetValue(component, name);

            var enumerable = value.Component as IEnumerable;
            if (enumerable != null) {

                var items = enumerable;
                if (!ReferenceEquals(current, items) && enumerable.GetEnumerator().MoveNext()) {
                    MethodInfo mi = FindAddonMethod(current.GetType(), enumerable);

                    if (mi == null) {
                        var errors = serviceProvider.TryGetService(PropertyTreeBinderErrors.Default);
                        errors.NoAddMethodSupported(component.GetType(), PropertyTreeBinderImpl.FindFileLocation(serviceProvider));
                        return;
                    }

                    foreach (var item in items) {
                        mi.Invoke(current, new object[] { item });
                    }
                }
            }
        }
Beispiel #57
0
 public abstract StreamingSource CreateStreamingSource(PropertyTreeMetaObject target, StreamContext input, IServiceProvider serviceProvider);
        private static QualifiedName ImpliedName(PropertyTreeNavigator nav, PropertyTreeMetaObject target)
        {
            QualifiedName qualifiedName = nav.QualifiedName;
            if (nav.IsExpressNamespace)
                return qualifiedName;

            NamespaceUri impliedNS = Utility.GetXmlnsNamespaceSafe(target.ComponentType);
            if (impliedNS == null)
                return qualifiedName;
            else
                return qualifiedName.ChangeNamespace(impliedNS);
        }
 public override PropertyTreeMetaObject EndStep(PropertyTreeMetaObject target)
 {
     return target.BindEndObject(Parent);
 }
        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;
        }