Exemplo n.º 1
0
        private XElement GetOrAddStyleElement(string styleId, StyleType styleType)
        {
            var existingStyle = root.Elements(Namespaces.w + "style").FirstOrDefault(d => styleId.Equals(d.GetOwnAttribute("styleId")));

            if (existingStyle != null)
            {
                var existingStyleType = existingStyle.GetOwnAttribute("type");

                if (!styleType.ToCamelCase().Equals(existingStyleType))
                {
                    throw new ArgumentException($"Style {styleId} exists in the document yet it is not of style {styleType} but of type {existingStyleType}", nameof(styleId));
                }

                return(existingStyle);
            }

            var providedStyle = externalStyleProvider?.FindStyleElement(styleId) ?? internalDefaultStyleProvider.FindStyleElement(styleId);

            if (providedStyle != null)
            {
                var providedStyleType = providedStyle.GetOwnAttribute("type");

                if (!styleType.ToCamelCase().Equals(providedStyleType))
                {
                    throw new Exception($"Style {styleId} provided by External Style Provider is not of type {styleType} but of type {providedStyleType}");
                }

                root.Add(providedStyle);
                return(providedStyle);
            }

            throw new ArgumentException($"Style {styleId} does not exist in the document and was not provided by External Style Provider", nameof(styleId));
        }
Exemplo n.º 2
0
        private XElement CreateStyleXml(StyleType styleType, string styleId, string name, bool?isPrimary)
        {
            if (string.IsNullOrWhiteSpace(styleId))
            {
                throw new ArgumentException("Parameter styleId cannot be null", nameof(styleId));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Parameter name cannot be null", nameof(name));
            }

            var xml = new XElement(Namespaces.w + "style",
                                   new XAttribute(Namespaces.w + "type", styleType.ToCamelCase()),
                                   new XAttribute(Namespaces.w + "styleId", styleId),
                                   new XElement(Namespaces.w + "name", new XAttribute(Namespaces.w + "val", name))
                                   );

            if (isPrimary == true)
            {
                xml.Add(Namespaces.w + "qFormat");
            }

            root.Add(xml);
            return(xml);
        }