コード例 #1
0
        public TreeNode GetLastDescendantWithXmlElement(string localName)
        {
            if (mChildren.Count == 0)
            {
                return(null);
            }

            for (int i = Children.Count - 1; i >= 0; i--)
            {
                TreeNode child = Children.Get(i);

                if (child.HasXmlProperty && child.GetXmlElementLocalName() == localName)
                {
                    return(child);
                }

                TreeNode childIn = child.GetLastDescendantWithXmlElement(localName);
                if (childIn != null)
                {
                    return(childIn);
                }
            }

            return(null);
        }
コード例 #2
0
            public TreeNode GetRealHeading()
            {
                if (RealHeading != NoRealHeading)
                {
                    return(RealHeading);
                }

                if (Heading != null)
                {
                    string headingName = Heading.GetXmlElementLocalName();
                    if (!string.IsNullOrEmpty(headingName) &&
                        headingName.Equals(@"hgroup", StringComparison.OrdinalIgnoreCase))
                    {
                        TreeNode highestRanked = null;
                        int      highestRank   = int.MaxValue;

                        foreach (TreeNode child in Heading.Children.ContentsAs_Enumerable)
                        {
                            string name = child.GetXmlElementLocalName();

                            int rank = GetHeadingRank(name);
                            if (rank >= 0 && rank < highestRank)
                            {
                                highestRank   = rank;
                                highestRanked = child;
                            }
                        }

                        DebugFix.Assert(highestRanked != null);

                        if (highestRanked != null)
                        {
                            RealHeading = highestRanked;
                            return(RealHeading);
                        }
                    }

                    RealHeading = Heading;
                    return(RealHeading);
                }

                RealHeading = null;
                return(RealHeading);
            }
コード例 #3
0
        public TreeNode GetNextSiblingWithXmlElement(string localName)
        {
            if (Parent == null)
            {
                return(null);
            }
            TreeNode next = this;

            while ((next = next.NextSibling) != null)
            {
                if (next.HasXmlProperty && next.GetXmlElementLocalName() == localName)
                {
                    return(next);
                }

                TreeNode nextIn = next.GetFirstDescendantWithXmlElement(localName);
                if (nextIn != null)
                {
                    return(nextIn);
                }
            }

            return(Parent.GetNextSiblingWithXmlElement(localName));
        }
コード例 #4
0
        public TreeNode GetPreviousSiblingWithXmlElement(string localName)
        {
            if (Parent == null)
            {
                return(null);
            }
            TreeNode previous = this;

            while ((previous = previous.PreviousSibling) != null)
            {
                if (previous.HasXmlProperty && previous.GetXmlElementLocalName() == localName)
                {
                    return(previous);
                }

                TreeNode previousIn = previous.GetLastDescendantWithXmlElement(localName);
                if (previousIn != null)
                {
                    return(previousIn);
                }
            }

            return(Parent.GetPreviousSiblingWithXmlElement(localName));
        }
コード例 #5
0
        protected void GetXmlFragment_(XmlWriter xmlWriter, bool absoluteLinks)
        {
            if (HasXmlProperty)
            {
                string name = GetXmlElementPrefixedLocalName();
                string NSPrefix;
                string localName;
                XmlProperty.SplitLocalName(name, out NSPrefix, out localName);

                //DebugFix.Assert(string.IsNullOrEmpty(NSPrefix));

                string nsUri = GetXmlNamespaceUri(NSPrefix);

                XmlProperty xmlProp = GetXmlProperty();

                if (String.IsNullOrEmpty(NSPrefix))
                {
                    if (!String.IsNullOrEmpty(nsUri))
                    {
                        xmlWriter.WriteStartElement(name, nsUri);
                    }
                    else
                    {
#if DEBUG
                        Debugger.Break();
#endif //DEBUG
                        xmlWriter.WriteStartElement(name);
                    }
                }
                else
                {
                    //#if DEBUG
                    //                    Debugger.Break();
                    //#endif //DEBUG
                    if (!String.IsNullOrEmpty(nsUri))
                    {
                        xmlWriter.WriteStartElement(NSPrefix, localName, nsUri);
                    }
                    else
                    {
#if DEBUG
                        Debugger.Break();
#endif //DEBUG
                        xmlWriter.WriteStartElement(name);
                    }
                }

                ManagedImageMedia manImageMedia = GetImageMedia() as ManagedImageMedia;
                ManagedVideoMedia manVideoMedia = GetVideoMedia() as ManagedVideoMedia;

                foreach (XmlAttribute xmlAttr in xmlProp.Attributes.ContentsAs_Enumerable)
                {
                    string value = xmlAttr.Value;

                    string nsUriAttr = xmlAttr.GetNamespaceUri();

                    string attrNSPrefix      = xmlAttr.Prefix;
                    string nameWithoutPrefix = xmlAttr.PrefixedLocalName != null ? xmlAttr.PrefixedLocalName : xmlAttr.LocalName;

                    if (manImageMedia != null &&
                        (nameWithoutPrefix == "href" || nameWithoutPrefix == "src"))
                    {
                        DebugFix.Assert(manImageMedia.ImageMediaData.OriginalRelativePath == value);
                        if (absoluteLinks)
                        {
                            value = ((FileDataProvider)manImageMedia.ImageMediaData.DataProvider).DataFileFullPath;
                        }
                    }

                    if (manVideoMedia != null &&
                        (nameWithoutPrefix == "href" || nameWithoutPrefix == "src"))
                    {
                        DebugFix.Assert(manVideoMedia.VideoMediaData.OriginalRelativePath == value);
                        if (absoluteLinks)
                        {
                            value = ((FileDataProvider)manVideoMedia.VideoMediaData.DataProvider).DataFileFullPath;
                        }
                    }


                    if (String.IsNullOrEmpty(attrNSPrefix))
                    {
                        if (nameWithoutPrefix.Equals(XmlReaderWriterHelper.NS_PREFIX_XMLNS))
                        {
                            DebugFix.Assert(XmlReaderWriterHelper.NS_URL_XMLNS.Equals(nsUriAttr));

                            xmlWriter.WriteAttributeString(XmlReaderWriterHelper.NS_PREFIX_XMLNS, XmlReaderWriterHelper.NS_URL_XMLNS, value);
                        }
                        else if (!String.IsNullOrEmpty(nsUriAttr) && nsUriAttr != nsUri)
                        {
#if DEBUG
                            Debugger.Break();
#endif //DEBUG
                            xmlWriter.WriteAttributeString(nameWithoutPrefix, nsUriAttr, value);
                        }
                        else
                        {
                            xmlWriter.WriteAttributeString(nameWithoutPrefix, value);
                        }
                    }
                    else
                    {
                        if (attrNSPrefix.Equals(XmlReaderWriterHelper.NS_PREFIX_XMLNS))
                        {
                            DebugFix.Assert(nsUriAttr == XmlReaderWriterHelper.NS_URL_XMLNS);
                            xmlWriter.WriteAttributeString(XmlReaderWriterHelper.NS_PREFIX_XMLNS, nameWithoutPrefix, XmlReaderWriterHelper.NS_URL_XMLNS,
                                                           value);
                        }
                        else if (attrNSPrefix.Equals(XmlReaderWriterHelper.NS_PREFIX_XML))
                        {
                            DebugFix.Assert(nsUriAttr == XmlReaderWriterHelper.NS_URL_XML);
                            xmlWriter.WriteAttributeString(XmlReaderWriterHelper.NS_PREFIX_XML, nameWithoutPrefix, XmlReaderWriterHelper.NS_URL_XML,
                                                           value);
                        }
                        else if (!String.IsNullOrEmpty(nsUriAttr))
                        {
#if DEBUG
                            //DebugFix.Assert(nsUriAttr != nsUri);

                            string uriCheck = xmlProp.GetNamespaceUri(attrNSPrefix);
                            DebugFix.Assert(nsUriAttr == uriCheck);
#endif //DEBUG
                            xmlWriter.WriteAttributeString(attrNSPrefix, nameWithoutPrefix, nsUriAttr, value);
                        }
                        else if (!String.IsNullOrEmpty(nsUri))
                        {
#if DEBUG
                            Debugger.Break();
#endif //DEBUG
                            xmlWriter.WriteAttributeString(attrNSPrefix, nameWithoutPrefix, nsUri, value);
                        }
                        else
                        {
#if DEBUG
                            Debugger.Break();
#endif //DEBUG
                            xmlWriter.WriteAttributeString(nameWithoutPrefix, value);
                        }
                    }
                }
            }

            if (mChildren.Count > 0)
            {
                for (int i = 0; i < mChildren.Count; i++)
                {
                    TreeNode child = mChildren.Get(i);

                    child.GetXmlFragment_(xmlWriter, absoluteLinks);
                }
            }
            else
            {
                AbstractTextMedia txt = GetTextMedia();
                if (txt != null)
                {
                    DebugFix.Assert(!HasXmlProperty || mChildren.Count == 0);

                    bool     needsCData = false;
                    TreeNode node       = null;
                    if (HasXmlProperty)
                    {
                        node = this;
                    }
                    else if (Parent != null && Parent.HasXmlProperty)
                    {
                        node = Parent;
                    }
                    if (node != null)
                    {
                        string name = node.GetXmlElementLocalName();
                        if (name == "script" || name == "style")
                        {
                            needsCData = true;
                        }
                    }

                    if (needsCData)
                    {
                        xmlWriter.WriteCData(txt.Text);
                    }
                    else
                    {
                        xmlWriter.WriteString(txt.Text);
                    }
                }
            }

            if (HasXmlProperty)
            {
                xmlWriter.WriteEndElement();
            }
        }
コード例 #6
0
        public List <Section> GetOrCreateOutline()
        {
            string localXmlName = GetXmlElementLocalName();

            if (string.IsNullOrEmpty(localXmlName) ||
                (
                    !IsSectioningContent(localXmlName) && !IsSectioningRoot(localXmlName)
                ))
            {
                Outline = null;
                return(null);
            }

            if (Outline != null)
            {
                return(Outline);
            }

            TreeNode         currentOutlinee = null;
            Section          currentSection  = null;
            Stack <TreeNode> stack           = new Stack <TreeNode>();

            PreVisitDelegate enterNode = delegate(TreeNode node)
            {
                node.Outline = null;

                if (stack.Count > 0)
                {
                    TreeNode top = stack.Peek();
                    if (IsHeading(top.GetXmlElementLocalName()))
                    {
                        return(true);
                    }
                }

                string name = node.GetXmlElementLocalName();
                if (IsSectioningContent(name) || IsSectioningRoot(name))
                {
                    //if (currentOutlinee != null && currentSection != null && currentSection.Heading == null)
                    //{
                    //    currentSection.HeadingImplied = true;
                    //}

                    if (currentOutlinee != null)
                    {
                        stack.Push(currentOutlinee);
                    }

                    currentOutlinee = node;

                    currentSection = new Section();
                    currentSection.RealSectioningRootOrContent = currentOutlinee;

                    currentOutlinee.Outline = new List <Section>();
                    currentOutlinee.Outline.Add(currentSection);

                    return(true);
                }

                if (currentOutlinee == null)
                {
                    return(true);
                }

                if (IsHeading(name))
                {
                    Section section = null;

                    if (currentSection != null && currentSection.Heading == null)
                    {
                        currentSection.Heading = node;
                    }
                    else if (
                        currentOutlinee.Outline == null ||
                        (
                            currentOutlinee.Outline.Count > 0 &&
                            (section = currentOutlinee.Outline[currentOutlinee.Outline.Count - 1]).Heading != null &&
                            node.GetHeadingRank() <= section.Heading.GetHeadingRank()
                        )
                        )
                    {
                        Section newSection = new Section();
                        newSection.Heading = node;

                        if (currentOutlinee.Outline == null)
                        {
                            currentOutlinee.Outline = new List <Section>();
                        }
                        currentOutlinee.Outline.Add(newSection);

                        currentSection = newSection;
                    }
                    else
                    {
                        Section candidate = currentSection;

                        while (candidate != null)
                        {
                            if (candidate.Heading != null &&
                                node.GetHeadingRank() > candidate.Heading.GetHeadingRank())
                            {
                                Section newSection = new Section();
                                newSection.Heading = node;
                                candidate.SubSections.Add(newSection);
                                newSection.ParentSection = candidate;

                                currentSection = newSection;

                                break;
                            }

                            candidate = candidate.ParentSection;
                        }
                    }

                    stack.Push(node);
                }

                return(true);
            };

            PostVisitDelegate exitNode = delegate(TreeNode node)
            {
                string name = node.GetXmlElementLocalName();

                if (stack.Count > 0)
                {
                    TreeNode top = stack.Peek();
                    if (top != null && top == node)
                    {
                        DebugFix.Assert(IsHeading(name));

                        stack.Pop();
                        return;
                    }
                }

                bool isSectioningContent = IsSectioningContent(name);
                bool isSectioningRoot    = IsSectioningRoot(name);

                if (isSectioningContent || isSectioningRoot)
                {
                    if (stack.Count > 0)
                    {
                        currentOutlinee = stack.Pop();

                        if (currentOutlinee.Outline != null && currentOutlinee.Outline.Count > 0)
                        {
                            currentSection = currentOutlinee.Outline[currentOutlinee.Outline.Count - 1];
                        }

                        if (isSectioningContent)
                        {
                            if (currentSection != null && node.Outline != null)
                            {
                                for (int i = 0; i < node.Outline.Count; i++)
                                {
                                    Section section = node.Outline[i];

                                    currentSection.SubSections.Add(section);
                                    section.ParentSection = currentSection;
                                }
                            }
                        }
                        else if (isSectioningRoot)
                        {
                            while (currentSection != null && currentSection.SubSections != null && currentSection.SubSections.Count > 0)
                            {
                                currentSection = currentSection.SubSections[currentSection.SubSections.Count - 1];
                            }
                        }
                    }
                    else
                    {
                        if (currentOutlinee.Outline != null && currentOutlinee.Outline.Count > 0)
                        {
                            currentSection = currentOutlinee.Outline[0];
                        }
                    }
                }
            };

            AcceptDepthFirst(enterNode, exitNode);

            if (currentOutlinee != null)
            {
                DebugFix.Assert(Object.ReferenceEquals(currentOutlinee, this));
            }

            return(currentOutlinee != null ? currentOutlinee.Outline : null);
        }