Пример #1
0
 public TocItem(TocItem source)
     : base(source)
 {
     _name        = source._name;
     _tocId       = source._tocId;
     _sourceId    = source._sourceId;
     _sourceType  = source._sourceType;
     _listItems   = source._listItems;
     _isRecursive = source._isRecursive;
 }
Пример #2
0
        public TocItem(string name, string tocItemId)
        {
            BuildExceptions.NotNullNotEmpty(name, "name");

            if (String.IsNullOrEmpty(tocItemId))
            {
                tocItemId = String.Format("tcid{0:x}", Guid.NewGuid().ToString().GetHashCode());
            }

            _isRecursive = true;
            _name        = name;
            _tocId       = tocItemId;
            _sourceId    = String.Empty;
            _sourceType  = TocItemSourceType.None;
        }
Пример #3
0
        private void CustomMergeToc(BuildTopicTocInfo topicParent, TocItem tocItem)
        {
            TocItemSourceType sourceType = tocItem.SourceType;

            if (sourceType == TocItemSourceType.None)
            {
                return;
            }

            IBuildNamedList <BuildGroupTocInfo> groupTocItems = _tocContext.Items;
            BuildGroupTocInfo groupToc = null;
            BuildTopicTocInfo tocInfo  = null;

            switch (sourceType)
            {
            case TocItemSourceType.None:
                break;

            case TocItemSourceType.Group:
                groupToc = groupTocItems[tocItem.SourceId];
                Debug.Assert(groupToc != null);
                break;

            case TocItemSourceType.Namespace:
                tocInfo = _tocContext[tocItem.SourceId];
                break;

            case TocItemSourceType.NamespaceRoot:
                groupToc = groupTocItems[tocItem.SourceId];
                Debug.Assert(groupToc != null);
                if (groupToc != null)
                {
                    if (groupToc.IsRooted)
                    {
                        tocInfo = groupToc[0];
                    }
                    else
                    {
                        throw new BuildException(
                                  "The specified reference group does not have a root container.");
                    }
                    groupToc = null;
                }
                break;

            case TocItemSourceType.Topic:
                tocInfo = _tocContext[tocItem.SourceId];
                break;
            }

            if (groupToc != null)
            {
                if (!groupToc.Exclude)
                {
                    topicParent.AddRange(groupToc.Items);
                }
                return;
            }

            if (tocInfo == null)
            {
                if (_logger != null)
                {
                    _logger.WriteLine(String.Format(
                                          "The TOC topic for the item '{0}' cannot be found.", tocItem.Name),
                                      BuildLoggerLevel.Warn);
                }

                return;
            }

            BuildTopicTocInfo topicToc = null;

            if (tocItem.SourceRecursive)
            {
                topicToc = tocInfo;
            }
            else
            {
                topicToc = new BuildTopicTocInfo(tocInfo.Name,
                                                 tocInfo.Source, topicParent);
                topicToc.Container = tocInfo.Container;
            }

            topicParent.Add(topicToc);

            for (int j = 0; j < tocItem.ItemCount; j++)
            {
                this.CustomMergeToc(topicToc, tocItem[j]);
            }
        }
Пример #4
0
        /// <summary>
        /// This reads and sets its state or attributes stored in a <c>XML</c> format
        /// with the given reader.
        /// </summary>
        /// <param name="reader">
        /// The reader with which the <c>XML</c> attributes of this object are accessed.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If the <paramref name="reader"/> is <see langword="null"/>.
        /// </exception>
        public override void ReadXml(XmlReader reader)
        {
            BuildExceptions.NotNull(reader, "reader");

            Debug.Assert(reader.NodeType == XmlNodeType.Element);
            if (reader.NodeType != XmlNodeType.Element)
            {
                return;
            }

            if (!String.Equals(reader.Name, TagName,
                               StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            _name  = reader.GetAttribute("name");
            _tocId = reader.GetAttribute("id");

            if (reader.IsEmptyElement)
            {
                return;
            }

            if (_listItems == null)
            {
                _listItems = new BuildKeyedList <TocItem>();
            }

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (String.Equals(reader.Name, "source",
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        _sourceId = reader.GetAttribute("id");
                        switch (reader.GetAttribute("type").ToLower())
                        {
                        case "topic":
                            _sourceType = TocItemSourceType.Topic;
                            break;

                        case "group":
                            _sourceType = TocItemSourceType.Group;
                            break;

                        case "namespace":
                            _sourceType = TocItemSourceType.Namespace;
                            break;

                        case "namespaceroot":
                            _sourceType = TocItemSourceType.NamespaceRoot;
                            break;

                        default:
                            _sourceType = TocItemSourceType.None;
                            break;
                        }
                        _isRecursive = Convert.ToBoolean(reader.GetAttribute("recursive"));
                    }
                    else if (String.Equals(reader.Name, TagName,
                                           StringComparison.OrdinalIgnoreCase))
                    {
                        TocItem item = new TocItem();

                        item.ReadXml(reader);

                        if (!item.IsEmpty)
                        {
                            this.Add(item);
                        }
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (String.Equals(reader.Name, TagName,
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                }
            }
        }
Пример #5
0
        private bool CustomMergeToc(string mergedToc, TocContent tocContent)
        {
            _tocContext.LoadAll();

            IBuildNamedList <BuildGroupTocInfo> groupTocItems = _tocContext.Items;

            Debug.Assert(groupTocItems != null && groupTocItems.Count != 0);

            int itemCount = tocContent.Count;

            List <BuildTopicTocInfo> listToc = new List <BuildTopicTocInfo>();

            for (int i = 0; i < itemCount; i++)
            {
                TocItem           tocItem    = tocContent[i];
                TocItemSourceType sourceType = tocItem.SourceType;
                if (sourceType == TocItemSourceType.None)
                {
                    continue;
                }

                BuildGroupTocInfo groupToc = null;
                BuildTopicTocInfo tocInfo  = null;
                switch (sourceType)
                {
                case TocItemSourceType.None:
                    break;

                case TocItemSourceType.Group:
                    groupToc = groupTocItems[tocItem.SourceId];
                    Debug.Assert(groupToc != null);
                    break;

                case TocItemSourceType.Namespace:
                    tocInfo = _tocContext[tocItem.SourceId];
                    break;

                case TocItemSourceType.NamespaceRoot:
                    groupToc = groupTocItems[tocItem.SourceId];
                    Debug.Assert(groupToc != null);
                    if (groupToc != null)
                    {
                        if (groupToc.IsRooted)
                        {
                            tocInfo = groupToc[0];
                        }
                        else
                        {
                            throw new BuildException(
                                      "The specified reference group does not have a root container.");
                        }
                        groupToc = null;
                    }
                    break;

                case TocItemSourceType.Topic:
                    tocInfo = _tocContext[tocItem.SourceId];
                    break;
                }

                if (groupToc != null)
                {
                    if (!groupToc.Exclude && groupToc.Count != 0)
                    {
                        listToc.AddRange(groupToc.Items);
                    }
                    continue;
                }

                if (tocInfo == null)
                {
                    if (_logger != null)
                    {
                        _logger.WriteLine(String.Format(
                                              "The TOC topic for the item '{0}' cannot be found.", tocItem.Name),
                                          BuildLoggerLevel.Warn);
                    }

                    continue;
                }

                BuildTopicTocInfo topicToc = null;
                if (tocItem.SourceRecursive)
                {
                    topicToc = tocInfo;
                }
                else
                {
                    topicToc = new BuildTopicTocInfo(tocInfo.Name,
                                                     tocInfo.Source, null);
                    topicToc.Container = tocInfo.Container;
                }

                listToc.Add(topicToc);

                for (int j = 0; j < tocItem.ItemCount; j++)
                {
                    this.CustomMergeToc(topicToc, tocItem[j]);
                }
            }

            if (listToc.Count == 0)
            {
                if (_logger != null)
                {
                    _logger.WriteLine("The custom merging of the table of contents failed.",
                                      BuildLoggerLevel.Error);
                }

                return(false);
            }

            XmlWriter xmlWriter = null;

            try
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent             = true;
                writerSettings.OmitXmlDeclaration = false;
                xmlWriter = XmlWriter.Create(mergedToc, writerSettings);

                xmlWriter.WriteStartElement("topics");

                for (int i = 0; i < listToc.Count; i++)
                {
                    listToc[i].WriteXml(xmlWriter);
                }

                xmlWriter.WriteEndElement();

                xmlWriter.Close();
                xmlWriter = null;

                return(true);
            }
            catch (Exception ex)
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Close();
                    xmlWriter = null;
                }

                if (_logger != null)
                {
                    _logger.WriteLine(ex);
                }

                return(false);
            }
        }