private void ChangeDocumentVersion(float version) { XmlNode versionInfoNode = document.SelectSingleNode("//FictionBook/description/document-info/version"); if (versionInfoNode != null && versionInfoNode.NodeType == XmlNodeType.Element) { versionInfoNode.InnerText = DocumentInfoNode.FormatVersion(version); } else { throw new Exception("InvalidFictionBookFormatException"); } }
public FictionBook(XmlDocument document) { if (document == null) { throw new ArgumentNullException("document"); } this.document = document; this.documentStatus = Fb2FixStatus.None; XmlNode statusInfoNode = document.SelectSingleNode("//FictionBook/description/custom-info[@info-type='fb2fix-status']"); if (statusInfoNode != null && statusInfoNode.NodeType == XmlNodeType.Element) { if (String.IsNullOrEmpty(statusInfoNode.InnerText)) { try { this.documentStatus = (Fb2FixStatus)Enum.Parse(typeof(Fb2FixStatus), statusInfoNode.InnerText, true); } catch (ArgumentException) { } } } this.documentInfoNode = document.SelectSingleNode("//FictionBook/description/document-info"); if (this.documentInfoNode != null && this.documentInfoNode.NodeType == XmlNodeType.Element) { documentInfo = new DocumentInfoNode(); documentInfo.Load(this.documentInfoNode as XmlElement); } this.titleInfoNode = document.SelectSingleNode("//FictionBook/description/title-info"); if (this.titleInfoNode != null && this.titleInfoNode.NodeType == XmlNodeType.Element) { titleInfo = new TitleInfoNode(); titleInfo.Load(this.titleInfoNode as XmlElement); } if (titleInfo == null) { throw new Exception("InvalidFictionBookFormatException"); } this.srcTitleInfoNode = document.SelectSingleNode("//FictionBook/description/src-title-info"); if (this.srcTitleInfoNode != null && this.srcTitleInfoNode.NodeType == XmlNodeType.Element) { srcTitleInfo = new TitleInfoNode(); srcTitleInfo.Load(this.srcTitleInfoNode as XmlElement); } this.publishInfoNode = document.SelectSingleNode("//FictionBook/description/publish-info"); if (this.publishInfoNode != null && this.publishInfoNode.NodeType == XmlNodeType.Element) { publishInfo = new PublishInfoNode(); publishInfo.Load(this.publishInfoNode as XmlElement); } this.descriptionNode = document.SelectSingleNode("//FictionBook/description"); if (this.descriptionNode == null) { throw new Exception("InvalidFictionBookFormatException"); } XmlNodeList nodes = document.SelectNodes("//FictionBook/description/custom-info"); customInfos = new List<CustomInfoNode>(nodes.Count); foreach (XmlNode node in nodes) { if (node.NodeType == XmlNodeType.Element) { CustomInfoNode item = new CustomInfoNode(); item.Load((XmlElement)node); if (!String.IsNullOrEmpty(item.InfoType)) { switch (item.InfoType) { case "fb2fix-status": case "librusec-id": case "previous-id": continue; } } item.XmlNode = node; customInfos.Add(item); } } this.modificationType = ModificationType.None; this.containerDateTime = DateTime.Now; }
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); } } }
public FictionBook(XmlDocument document) { if (document == null) { throw new ArgumentNullException("document"); } this.document = document; this.documentStatus = Fb2FixStatus.None; XmlNode statusInfoNode = document.SelectSingleNode("//FictionBook/description/custom-info[@info-type='fb2fix-status']"); if (statusInfoNode != null && statusInfoNode.NodeType == XmlNodeType.Element) { if (String.IsNullOrEmpty(statusInfoNode.InnerText)) { try { this.documentStatus = (Fb2FixStatus)Enum.Parse(typeof(Fb2FixStatus), statusInfoNode.InnerText, true); } catch (ArgumentException) { } } } this.documentInfoNode = document.SelectSingleNode("//FictionBook/description/document-info"); if (this.documentInfoNode != null && this.documentInfoNode.NodeType == XmlNodeType.Element) { documentInfo = new DocumentInfoNode(); documentInfo.Load(this.documentInfoNode as XmlElement); } this.titleInfoNode = document.SelectSingleNode("//FictionBook/description/title-info"); if (this.titleInfoNode != null && this.titleInfoNode.NodeType == XmlNodeType.Element) { titleInfo = new TitleInfoNode(); titleInfo.Load(this.titleInfoNode as XmlElement); } if (titleInfo == null) { throw new Exception("InvalidFictionBookFormatException"); } this.srcTitleInfoNode = document.SelectSingleNode("//FictionBook/description/src-title-info"); if (this.srcTitleInfoNode != null && this.srcTitleInfoNode.NodeType == XmlNodeType.Element) { srcTitleInfo = new TitleInfoNode(); srcTitleInfo.Load(this.srcTitleInfoNode as XmlElement); } this.publishInfoNode = document.SelectSingleNode("//FictionBook/description/publish-info"); if (this.publishInfoNode != null && this.publishInfoNode.NodeType == XmlNodeType.Element) { publishInfo = new PublishInfoNode(); publishInfo.Load(this.publishInfoNode as XmlElement); } this.descriptionNode = document.SelectSingleNode("//FictionBook/description"); if (this.descriptionNode == null) { throw new Exception("InvalidFictionBookFormatException"); } XmlNodeList nodes = document.SelectNodes("//FictionBook/description/custom-info"); customInfos = new List <CustomInfoNode>(nodes.Count); foreach (XmlNode node in nodes) { if (node.NodeType == XmlNodeType.Element) { CustomInfoNode item = new CustomInfoNode(); item.Load((XmlElement)node); if (!String.IsNullOrEmpty(item.InfoType)) { switch (item.InfoType) { case "fb2fix-status": case "librusec-id": case "previous-id": continue; } } item.XmlNode = node; customInfos.Add(item); } } this.modificationType = ModificationType.None; this.containerDateTime = DateTime.Now; }