コード例 #1
0
ファイル: FictionBook.cs プロジェクト: avgx/knigoskop
        public void CheckDocumentHeader()
        {
            foreach (TitleInfoNode infoNode in new TitleInfoNode[] { titleInfo, srcTitleInfo })
            {
                if (infoNode == null)
                {
                    continue;
                }

                if (infoNode.Genres.Count == 0)
                {
                    infoNode.Genres.Add(new GenreInfoNode("nonfiction"));
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(infoNode.Authors);

                if (infoNode.BookTitle == null)
                {
                    if (publishInfo != null && !String.IsNullOrEmpty(publishInfo.BookName))
                    {
                        infoNode.BookTitle = publishInfo.BookName;
                        this.modificationType |= ModificationType.Description;
                    }
                    else if (titleInfo != null && !String.IsNullOrEmpty(titleInfo.BookTitle))
                    {
                        infoNode.BookTitle = titleInfo.BookTitle;
                        this.modificationType |= ModificationType.Description;
                    }
                    else
                    {
                        throw new Exception("InvalidFictionBookFormatException");
                    }
                }

                if (infoNode.Lang == null)
                {
                    infoNode.Lang = "ru";
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(infoNode.Translators);
            }

            if (documentInfo == null)
            {
                documentInfo = new DocumentInfoNode();

                AuthorInfoNode documentAuthor = new AuthorInfoNode();
                documentAuthor.NickName = "FB2Fix";

                documentInfo.Authors.Add(documentAuthor);
                documentInfo.Id = ComputeDocumentId(document.DocumentElement.InnerText);
                documentInfo.Date = DateTime.Now;
                documentInfo.ProgramUsed = "FB2Fix";
                documentInfo.Version = 0.0f;

                this.modificationType |= ModificationType.DocumentInfo;
            }
            else
            {
                CheckAuthorInfo(documentInfo.Authors);

                if (documentInfo.Date == null)
                {
                    documentInfo.Date = DateTime.Now;
                    this.modificationType |= ModificationType.Description;
                }

                if (documentInfo.Version == null)
                {
                    documentInfo.Version = 0.0f;
                    this.modificationType |= ModificationType.DocumentInfo;
                }

                    if (String.IsNullOrEmpty(documentInfo.Id))
                    {
                        XmlElement xmlPreviousId = this.document.SelectSingleNode("//FictionBook/description/custom-info[@info-type='previous-id']") as XmlElement;

                        if (xmlPreviousId == null)
                        {
                            xmlPreviousId = document.CreateElement("custom-info");

                            XmlAttribute attr = document.CreateAttribute("info-type");
                            attr.Value = "previous-id";

                            xmlPreviousId.Attributes.Append(attr);
                            this.descriptionNode.AppendChild(xmlPreviousId);
                        }

                        xmlPreviousId.InnerText = documentInfo.Id;
                    }

                    documentInfo.Id = ComputeDocumentId(document.DocumentElement.InnerText);
                    this.modificationType |= ModificationType.Description;

                if (String.IsNullOrEmpty(documentInfo.Id))
                {
                    documentInfo.Id = ComputeDocumentId(document.DocumentElement.InnerText);
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(documentInfo.Publishers);
            }

            XmlElement xmlNewTitleInfo = document.CreateElement("title-info");
            xmlNewTitleInfo = titleInfo.Store(document, xmlNewTitleInfo);
            this.descriptionNode.ReplaceChild(xmlNewTitleInfo, titleInfoNode);
            titleInfoNode = xmlNewTitleInfo;

            if (srcTitleInfo != null)
            {
                XmlElement xmlNewSrcTitleInfo = document.CreateElement("src-title-info");
                xmlNewSrcTitleInfo = srcTitleInfo.Store(document, xmlNewSrcTitleInfo);
                this.descriptionNode.ReplaceChild(xmlNewSrcTitleInfo, srcTitleInfoNode);
                srcTitleInfoNode = xmlNewSrcTitleInfo;
            }

            XmlElement xmlNewDocumentInfo = document.CreateElement("document-info");
            xmlNewDocumentInfo = documentInfo.Store(document, xmlNewDocumentInfo);
            if (documentInfoNode == null)
            {
                if (srcTitleInfoNode == null)
                {
                    this.descriptionNode.InsertAfter(xmlNewDocumentInfo, titleInfoNode);
                }
                else
                {
                    this.descriptionNode.InsertAfter(xmlNewDocumentInfo, srcTitleInfoNode);
                }
            }
            else
            {
                this.descriptionNode.ReplaceChild(xmlNewDocumentInfo, documentInfoNode);
            }

            if (publishInfo != null)
            {
                XmlElement xmlNewPublishInfo = document.CreateElement("publish-info");
                xmlNewPublishInfo = publishInfo.Store(document, xmlNewPublishInfo);

                if (xmlNewPublishInfo != null)
                {
                    this.descriptionNode.ReplaceChild(xmlNewPublishInfo, publishInfoNode);
                }
                else
                {
                    this.descriptionNode.RemoveChild(publishInfoNode);
                }
            }

            foreach (CustomInfoNode customInfoNode in customInfos)
            {
                XmlElement element = document.CreateElement("custom-info");
                element = customInfoNode.Store(document, element);

                if (element != null)
                {
                    this.descriptionNode.ReplaceChild(customInfoNode.XmlNode, element);
                }
                else
                {
                    this.descriptionNode.RemoveChild(customInfoNode.XmlNode);
                }
            }
        }
コード例 #2
0
        public void CheckDocumentHeader()
        {
            foreach (TitleInfoNode infoNode in new TitleInfoNode[] { titleInfo, srcTitleInfo })
            {
                if (infoNode == null)
                {
                    continue;
                }

                if (infoNode.Genres.Count == 0)
                {
                    infoNode.Genres.Add(new GenreInfoNode("nonfiction"));
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(infoNode.Authors);

                if (infoNode.BookTitle == null)
                {
                    if (publishInfo != null && !String.IsNullOrEmpty(publishInfo.BookName))
                    {
                        infoNode.BookTitle     = publishInfo.BookName;
                        this.modificationType |= ModificationType.Description;
                    }
                    else if (titleInfo != null && !String.IsNullOrEmpty(titleInfo.BookTitle))
                    {
                        infoNode.BookTitle     = titleInfo.BookTitle;
                        this.modificationType |= ModificationType.Description;
                    }
                    else
                    {
                        throw new Exception("InvalidFictionBookFormatException");
                    }
                }

                if (infoNode.Lang == null)
                {
                    infoNode.Lang          = "ru";
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(infoNode.Translators);
            }

            if (documentInfo == null)
            {
                documentInfo = new DocumentInfoNode();

                AuthorInfoNode documentAuthor = new AuthorInfoNode();
                documentAuthor.NickName = "FB2Fix";

                documentInfo.Authors.Add(documentAuthor);
                documentInfo.Id          = ComputeDocumentId(document.DocumentElement.InnerText);
                documentInfo.Date        = DateTime.Now;
                documentInfo.ProgramUsed = "FB2Fix";
                documentInfo.Version     = 0.0f;

                this.modificationType |= ModificationType.DocumentInfo;
            }
            else
            {
                CheckAuthorInfo(documentInfo.Authors);

                if (documentInfo.Date == null)
                {
                    documentInfo.Date      = DateTime.Now;
                    this.modificationType |= ModificationType.Description;
                }

                if (documentInfo.Version == null)
                {
                    documentInfo.Version   = 0.0f;
                    this.modificationType |= ModificationType.DocumentInfo;
                }

                if (String.IsNullOrEmpty(documentInfo.Id))
                {
                    XmlElement xmlPreviousId = this.document.SelectSingleNode("//FictionBook/description/custom-info[@info-type='previous-id']") as XmlElement;

                    if (xmlPreviousId == null)
                    {
                        xmlPreviousId = document.CreateElement("custom-info");

                        XmlAttribute attr = document.CreateAttribute("info-type");
                        attr.Value = "previous-id";

                        xmlPreviousId.Attributes.Append(attr);
                        this.descriptionNode.AppendChild(xmlPreviousId);
                    }

                    xmlPreviousId.InnerText = documentInfo.Id;
                }

                documentInfo.Id        = ComputeDocumentId(document.DocumentElement.InnerText);
                this.modificationType |= ModificationType.Description;

                if (String.IsNullOrEmpty(documentInfo.Id))
                {
                    documentInfo.Id        = ComputeDocumentId(document.DocumentElement.InnerText);
                    this.modificationType |= ModificationType.Description;
                }

                CheckAuthorInfo(documentInfo.Publishers);
            }

            XmlElement xmlNewTitleInfo = document.CreateElement("title-info");

            xmlNewTitleInfo = titleInfo.Store(document, xmlNewTitleInfo);
            this.descriptionNode.ReplaceChild(xmlNewTitleInfo, titleInfoNode);
            titleInfoNode = xmlNewTitleInfo;

            if (srcTitleInfo != null)
            {
                XmlElement xmlNewSrcTitleInfo = document.CreateElement("src-title-info");
                xmlNewSrcTitleInfo = srcTitleInfo.Store(document, xmlNewSrcTitleInfo);
                this.descriptionNode.ReplaceChild(xmlNewSrcTitleInfo, srcTitleInfoNode);
                srcTitleInfoNode = xmlNewSrcTitleInfo;
            }

            XmlElement xmlNewDocumentInfo = document.CreateElement("document-info");

            xmlNewDocumentInfo = documentInfo.Store(document, xmlNewDocumentInfo);
            if (documentInfoNode == null)
            {
                if (srcTitleInfoNode == null)
                {
                    this.descriptionNode.InsertAfter(xmlNewDocumentInfo, titleInfoNode);
                }
                else
                {
                    this.descriptionNode.InsertAfter(xmlNewDocumentInfo, srcTitleInfoNode);
                }
            }
            else
            {
                this.descriptionNode.ReplaceChild(xmlNewDocumentInfo, documentInfoNode);
            }

            if (publishInfo != null)
            {
                XmlElement xmlNewPublishInfo = document.CreateElement("publish-info");
                xmlNewPublishInfo = publishInfo.Store(document, xmlNewPublishInfo);

                if (xmlNewPublishInfo != null)
                {
                    this.descriptionNode.ReplaceChild(xmlNewPublishInfo, publishInfoNode);
                }
                else
                {
                    this.descriptionNode.RemoveChild(publishInfoNode);
                }
            }

            foreach (CustomInfoNode customInfoNode in customInfos)
            {
                XmlElement element = document.CreateElement("custom-info");
                element = customInfoNode.Store(document, element);

                if (element != null)
                {
                    this.descriptionNode.ReplaceChild(customInfoNode.XmlNode, element);
                }
                else
                {
                    this.descriptionNode.RemoveChild(customInfoNode.XmlNode);
                }
            }
        }