Esempio n. 1
0
        private XamlElement Parse()
        {
            this.tokens = new ReadOnlyStack <Token>(lexer.GetTokens(text));

            XamlElement root = MatchElement();

            if (!tokens.IsEmpty)
            {
                throw new Granular.Exception("Can't parse \"{0}\", end of stream is expected at index {1}", text, tokens.Peek().Start);
            }

            return(root);
        }
Esempio n. 2
0
        public static IElementInitializer FromXamlElement(XamlElement element, Type containingType)
        {
            if (IsList(containingType))
            {
                return new ElementListContentInitializer(element);
            }

            if (IsDictionary(containingType))
            {
                return new ElementDictionaryContentInitializer(element);
            }

            throw new Granular.Exception("Can't initialize type \"{0}\" content", containingType.Name);
        }
Esempio n. 3
0
        public static ClassDefinition Parse(XamlElement element, ITypeParser typeParser)
        {
            string fullName = GetClassFullName(element);

            if (fullName.IsNullOrEmpty())
            {
                return null;
            }

            string baseTypeName = typeParser.ParseTypeName(element.Name);

            IEnumerable<MemberDefinition> members = GetMembers(element, baseTypeName, typeParser).ToArray();

            return new ClassDefinition(GetTypeName(fullName), GetTypeNamespace(fullName), baseTypeName, members);
        }
Esempio n. 4
0
        private static IEnumerable<MemberDefinition> GetMembers(XamlElement element, string elementTypeName, ITypeParser typeParser)
        {
            if (elementTypeName == "System.Windows.ResourceDictionary")
            {
                yield break;
            }

            XamlAttribute nameAttribute = element.Attributes.FirstOrDefault(attribute => attribute.Name == XamlLanguage.NameDirective);

            if (nameAttribute != null)
            {
                yield return new MemberDefinition((string)nameAttribute.Value, elementTypeName);
            }

            foreach (XamlElement child in element.Children)
            {
                string childTypeName = typeParser.ParseTypeName(child.Name.IsMemberName ? child.Name.ContainingTypeName : child.Name);

                foreach (MemberDefinition member in GetMembers(child, childTypeName, typeParser))
                {
                    yield return member;
                }
            }
        }
Esempio n. 5
0
        private static IEnumerable<MemberDefinition> GetMembers(XamlElement element, string elementTypeName, ITypeParser typeParser)
        {
            if (elementTypeName == "System.Windows.ResourceDictionary")
            {
                yield break;
            }

            XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective);

            if (nameDirective != null)
            {
                yield return new MemberDefinition((string)nameDirective.GetSingleValue(), elementTypeName);
            }

            foreach (XamlElement child in element.Values.OfType<XamlElement>().Concat(element.Members.SelectMany(member => member.Values.OfType<XamlElement>())))
            {
                string childTypeName = typeParser.ParseTypeName(child.Name);

                foreach (MemberDefinition member in GetMembers(child, childTypeName, typeParser))
                {
                    yield return member;
                }
            }
        }
Esempio n. 6
0
        private static IEnumerable<KeyElementFactory> CreateElementsFactories(XamlElement memberElement)
        {
            List<KeyElementFactory> list = new List<KeyElementFactory>();

            foreach (XamlElement contentChild in memberElement.GetContentChildren())
            {
                bool isShared = contentChild.Attributes.All(attribute => attribute.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(attribute.Value, typeof(bool), XamlNamespaces.Empty));

                IElementFactory contentChildFactory = ElementFactory.FromXamlElement(contentChild, null);

                if (!isShared)
                {
                    contentChildFactory = new ValueProviderFactory(contentChildFactory);
                }

                list.Add(new KeyElementFactory(contentChildFactory, contentChild));
            }

            return list;
        }
Esempio n. 7
0
 public ElementDictionaryContentInitializer(XamlElement memberElement)
 {
     keyElementFactories = CreateElementsFactories(memberElement);
 }
Esempio n. 8
0
        public static IElementInitializer FromXamlElement(IPropertyAdapter propertyAdapter, XamlElement memberElement)
        {
            IEnumerable<XamlElement> children = memberElement.GetContentChildren();

            if (!children.Any())
            {
                if (!memberElement.TextValue.IsNullOrEmpty())
                {
                    object value = TypeConverter.ConvertValue(memberElement.TextValue, propertyAdapter.PropertyType, memberElement.Namespaces);
                    return new ElementPropertyMemberInitializer(propertyAdapter, new ConstantElementFactory(value));
                }

                return ElementInitializer.Empty;
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(propertyAdapter.PropertyType) &&
                !(children.Count() == 1 && propertyAdapter.PropertyType.IsAssignableFrom(children.First().GetElementType())))
            {
                IElementInitializer propertyContentInitializer = ElementCollectionContentInitailizer.FromXamlElement(memberElement, propertyAdapter.PropertyType);
                // use a factory that creates the property value before initializing its content (when its null)
                return new ElementPropertyMemberFactoryInitializer(propertyAdapter, propertyContentInitializer);
            }

            if (children.Count() == 1)
            {
                if (propertyAdapter.PropertyType == typeof(IFrameworkElementFactory))
                {
                    return new FrameworkElementFactoryInitializer(propertyAdapter, ElementFactory.FromXamlElement(children.First(), children.First().GetElementType()));
                }

                IElementFactory contentFactory = ElementFactory.FromXamlElement(children.First(), propertyAdapter.PropertyType);
                return new ElementPropertyMemberInitializer(propertyAdapter, contentFactory);
            }

            throw new Granular.Exception("Element \"{0}\" cannot have more than one child", memberElement.Name);
        }
Esempio n. 9
0
        public static IElementInitializer FromXamlElement(XamlName memberName, XamlElement memberElement, Type containingType)
        {
            IEventAdapter eventAdapter = EventAdapter.CreateAdapter(containingType, memberName);
            if (eventAdapter != null)
            {
                return ElementEventMemberInitializer.FromXamlElement(eventAdapter, memberElement);
            }

            IPropertyAdapter propertyAdapter = PropertyAdapter.CreateAdapter(containingType, memberName);
            if (propertyAdapter != null)
            {
                return ElementPropertyMemberInitializer.FromXamlElement(propertyAdapter, memberElement);
            }

            throw new Granular.Exception("Type \"{0}\" does not contain a member named \"{1}\"", containingType.Name, memberName);
        }
Esempio n. 10
0
        private static IEnumerable<IElementInitializer> CreateMemberInitializers(XamlElement element)
        {
            Type elementType = element.GetElementType();

            List<IElementInitializer> list = new List<IElementInitializer>();

            int index = 0;
            foreach (XamlMember member in element.Members)
            {
                // markup extensions may contain members with an empty name, the name should be resolved from the member index
                XamlName memberName = member.Name.IsEmpty ? GetParameterName(elementType, index) : member.Name;

                list.Add(ElementMemberInitializer.Create(memberName, elementType, member.Values, member.Namespaces));
                index++;
            }

            return list;
        }
Esempio n. 11
0
        private static IElementFactory FromXamlElementContent(XamlElement element)
        {
            if (element.Members.Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have members, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
            }

            if (!element.Values.Any())
            {
                throw new Granular.Exception("Element \"{0}\" must have a value, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
            }

            if (element.Values.Count() > 1)
            {
                throw new Granular.Exception("Element \"{0}\" can't have multiple children, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
            }

            return FromValue(element.Values.First(), element.GetElementType(), element.Namespaces);
        }
Esempio n. 12
0
            public KeyValueElementFactory(Type keyType, IElementFactory valueFactory, XamlElement xamlElement)
            {
                this.valueFactory = valueFactory;

                keyProperty = GetKeyProperty(valueFactory.ElementType);
                keyDirectiveFactory = GetKeyDirectiveFactory(xamlElement, keyType);

                if (keyDirectiveFactory == null && keyProperty == null)
                {
                    throw new Granular.Exception("Dictionary item \"{0}\" must have a key", xamlElement.Name);
                }
            }
Esempio n. 13
0
 private static string GetClassFullName(XamlElement root)
 {
     XamlMember classDirective = root.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.ClassDirective);
     return classDirective != null ? (string)classDirective.GetSingleValue() : String.Empty;
 }
Esempio n. 14
0
 public static IElementInitializer FromXamlElement(IEventAdapter eventAdapter, XamlElement eventElement)
 {
     return new ElementEventMemberInitializer(eventAdapter, GetEventHandlerName(eventElement));
 }
Esempio n. 15
0
 private static object GetKeyDirectiveValue(XamlElement element)
 {
     object value;
     return element.TryGetMemberValue(XamlLanguage.KeyDirective, out value) ? value : null;
 }
Esempio n. 16
0
            public KeyElementFactory(IElementFactory elementFactory, XamlElement xamlElement)
            {
                this.elementFactory = elementFactory;

                keyDirectiveValue = GetKeyDirectiveValue(xamlElement);
                keyProperty = GetKeyProperty(elementFactory.ElementType);

                if (keyDirectiveValue == null && keyProperty == null)
                {
                    throw new Granular.Exception("Dictionary item \"{0}\" must have a key", xamlElement.Name);
                }
            }
Esempio n. 17
0
        private static IElementFactory FromXamlElementContent(XamlElement element)
        {
            if (element.GetMemberNodes().Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have members, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
            }

            IEnumerable<XamlElement> contentChilren = element.GetContentChildren();

            if (contentChilren.Any() && !element.TextValue.IsNullOrEmpty())
            {
                throw new Granular.Exception("Element \"{0}\" cannot have both children and text value", element.Name);
            }

            if (!contentChilren.Any())
            {
                return new ConvertedElementFactory(new ConstantElementFactory(element.TextValue), element.GetElementType(), element.Namespaces);
            }

            if (contentChilren.Count() == 1)
            {
                return ElementFactory.FromXamlElement(contentChilren.First(), element.GetElementType());
            }

            throw new Granular.Exception("Element \"{0}\" can't have multiple children, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
        }
Esempio n. 18
0
        public static IElementFactory FromXamlElement(XamlElement element, Type targetType)
        {
            Type elementType = element.GetElementType();

            if (elementType.GetDefaultConstructor() == null)
            {
                return FromElementFactory(FromXamlElementContent(element), targetType, element.Namespaces);
            }

            IElementInitializer elementInitializer = new ElementInitializer(element);
            IElementFactory elementFactory = new ElementFactory(elementType, elementInitializer);

            return FromElementFactory(elementFactory, targetType, element.Namespaces);
        }
Esempio n. 19
0
 public static object Load(XamlElement resource)
 {
     IElementFactory factory = ElementFactory.FromXamlElement(resource, null);
     return factory.CreateElement(new InitializeContext());
 }
Esempio n. 20
0
        public ElementInitializer(XamlElement element)
        {
            elementType = element.GetElementType();
            namespaces = element.Namespaces;

            memberInitializers = CreateMemberInitializers(element);
            contentInitializer = CreateContentInitializer(element);

            nameDirectiveValue = GetNameDirectiveValue(element);
            nameProperty = GetNameProperty(element.GetElementType());
        }
Esempio n. 21
0
 public static void Load(object target, XamlElement resource)
 {
     IElementInitializer initializer = new ElementInitializer(resource);
     initializer.InitializeElement(target, new InitializeContext());
 }
Esempio n. 22
0
        private static string GetEventHandlerName(XamlElement element)
        {
            if (element.Children.Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have children", element.Name);
            }

            if (element.Attributes.Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have attributes", element.Name);
            }

            if (element.TextValue.IsNullOrEmpty())
            {
                throw new Granular.Exception("Element \"{0}\" doesn't contain an event handler name", element.Name);
            }

            return element.TextValue;
        }
Esempio n. 23
0
        private static IEnumerable<IElementInitializer> CreateMemberInitializers(XamlElement element)
        {
            Type elementType = element.GetElementType();

            List<IElementInitializer> list = new List<IElementInitializer>();

            int index = 0;
            foreach (XamlAttribute attribute in element.GetMemberAttributes())
            {
                // markup extensions can contain empty attributes, the member name is determined by its index
                XamlName memberName = attribute.Name.IsEmpty ? GetParameterName(elementType, index) : attribute.Name;
                list.Add(ElementMemberInitializer.FromXamlAttribute(memberName, attribute, elementType));

                index++;
            }

            foreach (XamlElement child in element.GetMemberChildren())
            {
                list.Add(ElementMemberInitializer.FromXamlElement(child.Name, child, elementType));
            }

            return list;
        }
Esempio n. 24
0
 private static IElementFactory GetKeyDirectiveFactory(XamlElement element, Type keyType)
 {
     XamlMember keyDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.KeyDirective);
     return keyDirective != null ? ElementFactory.FromValue(keyDirective.GetSingleValue(), keyType, element.Namespaces) : null;
 }
Esempio n. 25
0
 private static string GetNameDirectiveValue(XamlElement element)
 {
     object value;
     return element.TryGetMemberValue(XamlLanguage.NameDirective, out value) ? (string)value : null;
 }
Esempio n. 26
0
        private static IElementInitializer CreateContentInitializer(XamlElement element)
        {
            Type elementType = element.GetElementType();

            string contentPropertyName = PropertyAttribute.GetPropertyName<ContentPropertyAttribute>(elementType);
            if (!contentPropertyName.IsNullOrEmpty())
            {
                return ElementMemberInitializer.Create(new XamlName(contentPropertyName), elementType, element.Values, element.Namespaces);
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(elementType))
            {
                return ElementCollectionContentInitailizer.Create(element.Values, elementType);
            }

            return null;
        }
Esempio n. 27
0
 public ElementListContentInitializer(XamlElement element)
 {
     elementsFactory = element.GetContentChildren().Select(contentChild => ElementFactory.FromXamlElement(contentChild, null)).ToArray();
 }
Esempio n. 28
0
 private static string GetNameDirectiveValue(XamlElement element)
 {
     XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective);
     return nameDirective != null ? (string)nameDirective.GetSingleValue() : null;
 }
Esempio n. 29
0
 private static string GetClassFullName(XamlElement root)
 {
     XamlAttribute classAttribute = root.Attributes.FirstOrDefault(attribute => attribute.Name == XamlLanguage.ClassDirective);
     return classAttribute != null ? (string)classAttribute.Value : String.Empty;
 }