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); }
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); }
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); }
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; } } }
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; } } }
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; }
public ElementDictionaryContentInitializer(XamlElement memberElement) { keyElementFactories = CreateElementsFactories(memberElement); }
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); }
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); }
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; }
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); }
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); } }
private static string GetClassFullName(XamlElement root) { XamlMember classDirective = root.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.ClassDirective); return classDirective != null ? (string)classDirective.GetSingleValue() : String.Empty; }
public static IElementInitializer FromXamlElement(IEventAdapter eventAdapter, XamlElement eventElement) { return new ElementEventMemberInitializer(eventAdapter, GetEventHandlerName(eventElement)); }
private static object GetKeyDirectiveValue(XamlElement element) { object value; return element.TryGetMemberValue(XamlLanguage.KeyDirective, out value) ? value : null; }
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); } }
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); }
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); }
public static object Load(XamlElement resource) { IElementFactory factory = ElementFactory.FromXamlElement(resource, null); return factory.CreateElement(new InitializeContext()); }
public ElementInitializer(XamlElement element) { elementType = element.GetElementType(); namespaces = element.Namespaces; memberInitializers = CreateMemberInitializers(element); contentInitializer = CreateContentInitializer(element); nameDirectiveValue = GetNameDirectiveValue(element); nameProperty = GetNameProperty(element.GetElementType()); }
public static void Load(object target, XamlElement resource) { IElementInitializer initializer = new ElementInitializer(resource); initializer.InitializeElement(target, new InitializeContext()); }
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; }
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; }
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; }
private static string GetNameDirectiveValue(XamlElement element) { object value; return element.TryGetMemberValue(XamlLanguage.NameDirective, out value) ? (string)value : null; }
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; }
public ElementListContentInitializer(XamlElement element) { elementsFactory = element.GetContentChildren().Select(contentChild => ElementFactory.FromXamlElement(contentChild, null)).ToArray(); }
private static string GetNameDirectiveValue(XamlElement element) { XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective); return nameDirective != null ? (string)nameDirective.GetSingleValue() : null; }
private static string GetClassFullName(XamlElement root) { XamlAttribute classAttribute = root.Attributes.FirstOrDefault(attribute => attribute.Name == XamlLanguage.ClassDirective); return classAttribute != null ? (string)classAttribute.Value : String.Empty; }