예제 #1
0
        /// <summary>
        /// Wrap an existing <see cref="XDocument"/> as an <see cref="XdmNode"/>. This is the <see cref="XDocument"/>
        /// equivalent of <see cref="DocumentBuilder.Wrap(XmlDocument)"/>.
        /// </summary>
        /// <remarks>
        /// PoC:
        /// 
        /// Creates wrapper objects for all nodes in the document graph and stores them using 
        /// <see cref="XObject.AddAnnotation(object)"/>. Will throw if any node has already been wrapped.
        /// 
        /// Idealy this would be an extension to <see cref="DocumentBuilder"/>, but DocumentBuilder does not expose
        /// its Configuration object publically.
        /// </remarks>
        public static XdmNode Wrap(this Processor processor, XDocument doc)
        {
            if (doc.Annotation<XObjectWrapper>() != null)
                throw new InvalidOperationException("XDocument is already annotated with a wrapper.");
            var docWrapper = (XDocumentWrapper)XObjectWrapper.MakeWrapper(doc);
            docWrapper.setConfiguration(processor.Implementation);
            doc.AddAnnotation(docWrapper);
            foreach (var node in doc.DescendantNodes())
            {
                if (node.Annotation<XObjectWrapper>() != null)
                    throw new InvalidOperationException(string.Format("{0} is already annotated with a wrapper.", node.GetType().Name));

                node.AddAnnotation(XObjectWrapper.MakeWrapper(node));

                if (node.NodeType == XmlNodeType.Element)
                {
                    foreach (var attr in ((XElement)node).Attributes())
                    {
                        if (attr.Annotation<XObjectWrapper>() != null)
                            throw new InvalidOperationException("Attribute is already annotated with a wrapper.");

                        attr.AddAnnotation(XObjectWrapper.MakeWrapper(attr));
                    }
                }
            }

            return (XdmNode)XdmValue.Wrap(docWrapper);
        }
        private static string GetDefaultParagraphStyleName(XDocument stylesXDoc)
        {
            XElement defaultParagraphStyle;
            string defaultParagraphStyleName = null;

            StylesInfo stylesInfo = stylesXDoc.Annotation<StylesInfo>();

            if (stylesInfo != null)
                defaultParagraphStyleName = stylesInfo.DefaultParagraphStyleName;
            else
            {
                defaultParagraphStyle = stylesXDoc
                    .Root
                    .Elements(W.style)
                    .FirstOrDefault(s =>
                    {
                        if ((string)s.Attribute(W.type) != "paragraph")
                            return false;
                        var defaultAttribute = s.Attribute(W._default);
                        var isDefault = false;
                        if (defaultAttribute != null &&
                            (bool)s.Attribute(W._default).ToBoolean())
                            isDefault = true;
                        return isDefault;
                    });
                defaultParagraphStyleName = null;
                if (defaultParagraphStyle != null)
                    defaultParagraphStyleName = (string)defaultParagraphStyle.Attribute(W.styleId);
                stylesInfo = new StylesInfo()
                {
                    DefaultParagraphStyleName = defaultParagraphStyleName,
                };
                stylesXDoc.AddAnnotation(stylesInfo);
            }
            return defaultParagraphStyleName;
        }