예제 #1
0
        public List <Resource> ExtractGlobalResources(XmlElement element)
        {
            List <Resource> result = new List <Resource>();

            if (ParsingHelper.IsResourceTag(element))
            {
                // parse content to get resources
                foreach (XmlElement resourceElement in element.Children)
                {
                    XmlAttribute keyAttribute = ParsingHelper.GetResourceKeyAttribute(resourceElement);
                    if (keyAttribute == null)
                    {
                        BindingPreprocess.Logger.LogError("You have a resource of type {0} with no key", string.IsNullOrWhiteSpace(resourceElement.NamespaceName) ? resourceElement.LocalName : string.Format("{0}:{1}", resourceElement.NamespaceName, resourceElement.LocalName));
                    }
                    else
                    {
                        Resource resource = new Resource(keyAttribute.Value)
                        {
                            ResourceElement = resourceElement,
                            Type            = resourceElement.LocalName,
                        };
                        foreach (XmlAttribute attribute in resourceElement.Attributes.Where(x => !ParsingHelper.IsResourceKeyAttribute(x)))
                        {
                            resource.Properties.Add(attribute.LocalName, attribute.Value);
                        }
                        result.Add(resource);
                    }
                }
            }
            else
            {
                throw new Exception("Global resource files which do not start with a resource tag");
            }

            return(result);
        }
예제 #2
0
        private void WriteSpecificAttribute(XmlWriter writer, XmlAttribute attribute, List <StyleResource> styleResources)
        {
            if (ParsingHelper.IsStyleAttribute(attribute))
            {
                BindingLanguageParser compiler = new BindingLanguageParser();
                bool       result;
                Expression resultExpression = compiler.Parse(attribute.Value, out result);

                if (!result)
                {
                    throw new CompileException(string.Format("Can not compile expression {0}", attribute.Value));
                }

                if (!resultExpression.IsOfType(ExpressionType.Resource))
                {
                    throw new CompileException(string.Format("Expecting resource expression for style, got {0}", attribute.Value));
                }

                string resourceKey = resultExpression.GetValue(ResourceExpression.KEY);
                //find correct resource
                StyleResource styleResource = styleResources.FirstOrDefault(x => resourceKey.Equals(x.Key, StringComparison.InvariantCultureIgnoreCase));

                if (styleResource == null)
                {
                    throw new IndexOutOfRangeException(string.Format("Resource with key {0} does not exists", resourceKey));
                }

                foreach (XmlAttribute attr in styleResource.ResourceElement.Attributes.Where(x => !ParsingHelper.IsResourceKeyAttribute(x)))
                {
                    // write all attributes embedded in the style
                    WriteAttribute(writer, attr, styleResources);
                }
            }
        }
예제 #3
0
        public List <Resource> ExtractResources(XmlElement element)
        {
            List <Resource>   result   = new List <Resource>();
            List <XmlElement> toRemove = new List <XmlElement>();

            foreach (XmlElement child in element.Children)
            {
                if (ParsingHelper.IsResourceTag(child))
                {
                    // parse content to get resources
                    foreach (XmlElement resourceElement in child.Children)
                    {
                        XmlAttribute keyAttribute = ParsingHelper.GetResourceKeyAttribute(resourceElement);
                        if (keyAttribute == null)
                        {
                            BindingPreprocess.Logger.LogError("You have a resource of type {0} with no key", string.IsNullOrWhiteSpace(resourceElement.NamespaceName) ? resourceElement.LocalName : string.Format("{0}:{1}", resourceElement.NamespaceName, resourceElement.LocalName));
                        }
                        else
                        {
                            Resource resource = new Resource(keyAttribute.Value)
                            {
                                ResourceElement = resourceElement,
                                Type            = resourceElement.LocalName,
                            };
                            foreach (XmlAttribute attribute in resourceElement.Attributes.Where(x => !ParsingHelper.IsResourceKeyAttribute(x)))
                            {
                                resource.Properties.Add(attribute.LocalName, attribute.Value);
                            }
                            result.Add(resource);
                        }
                    }

                    // and then mark it for remove from the tree since it does not need to be there anymore
                    toRemove.Add(child);
                }
                else
                {
                    result.AddRange(ExtractResources(child));
                }
            }

            foreach (XmlElement toRemoveChild in toRemove)
            {
                element.Children.Remove(toRemoveChild);
            }

            return(result);
        }