internal void Load(XElement xSection) { ClearAll(); if (xSection == null) { throw new ArgumentNullException("xSection"); } if (xSection.Name.LocalName != Fb2TextSectionElementName) { throw new ArgumentException("Element of wrong type passed", "xSection"); } XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName); if (xTitle != null) { Title = new TitleItem(); try { Title.Load(xTitle); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section title : {0}.", ex.Message)); } } IEnumerable<XElement> xEpigraphs = xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName); foreach (var xEpigraph in xEpigraphs) { EpigraphItem epigraph = new EpigraphItem(); try { epigraph.Load(xEpigraph); _epigraphs.Add(epigraph); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section epigraph : {0}.", ex.Message)); } } XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section annotation : {0}.", ex.Message)); } } IEnumerable<XElement> xElements = xSection.Elements(); foreach (var xElement in xElements) { switch (xElement.Name.LocalName) { case ParagraphItem.Fb2ParagraphElementName: ParagraphItem paragraph = new ParagraphItem(); try { paragraph.Load(xElement); _content.Add(paragraph); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section paragraph : {0}.", ex.Message)); } break; case PoemItem.Fb2PoemElementName: PoemItem poem = new PoemItem(); try { poem.Load(xElement); _content.Add(poem); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section poem : {0}.", ex.Message)); } break; case ImageItem.Fb2ImageElementName: ImageItem image = new ImageItem(); try { image.Load(xElement); AddImage(image); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section image : {0}.", ex.Message)); } break; case SubTitleItem.Fb2SubtitleElementName: SubTitleItem subtitle = new SubTitleItem(); try { subtitle.Load(xElement); _content.Add(subtitle); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section subtitle : {0}.", ex.Message)); } break; case CiteItem.Fb2CiteElementName: CiteItem cite = new CiteItem(); try { cite.Load(xElement); _content.Add(cite); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section citation : {0}.", ex.Message)); } break; case EmptyLineItem.Fb2EmptyLineElementName: EmptyLineItem eline = new EmptyLineItem(); _content.Add(eline); break; case TableItem.Fb2TableElementName: TableItem table = new TableItem(); try { table.Load(xElement); _content.Add(table); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section emptly line : {0}.", ex.Message)); } break; case Fb2TextSectionElementName: // internal <section> read recurive SectionItem section = new SectionItem(); try { section.Load(xElement); AddSection(section); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load sub-section : {0}.", ex.Message)); } break; case AnnotationItem.Fb2AnnotationItemName: // already processed break; case TitleItem.Fb2TitleElementName: // already processed break; case EpigraphItem.Fb2EpigraphElementName: // already processed break; default: if (!string.IsNullOrEmpty(xElement.Value)) { ParagraphItem tempParagraph = new ParagraphItem(); try { SimpleText text = new SimpleText {Text = xElement.Value}; tempParagraph.ParagraphData.Add(text); _content.Add(tempParagraph); } catch { // ignored } } Debug.WriteLine("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName); break; } } ID = null; XAttribute xID = xSection.Attribute("id"); if ((xID != null)) { ID = xID.Value; } Lang = null; XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang"); if ((xLang != null)) { Lang = xLang.Value; } }
internal void Load(XElement xSection) { ClearAll(); if (xSection == null) { throw new ArgumentNullException("xSection"); } if (xSection.Name.LocalName != Fb2TextSectionElementName) { throw new ArgumentException("Element of wrong type passed", "xSection"); } XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName); if (xTitle != null) { Title = new TitleItem(); try { Title.Load(xTitle); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section title : {0}.", ex.Message)); } } IEnumerable <XElement> xEpigraphs = xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName); foreach (var xEpigraph in xEpigraphs) { EpigraphItem epigraph = new EpigraphItem(); try { epigraph.Load(xEpigraph); _epigraphs.Add(epigraph); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section epigraph : {0}.", ex.Message)); } } XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section annotation : {0}.", ex.Message)); } } IEnumerable <XElement> xElements = xSection.Elements(); foreach (var xElement in xElements) { switch (xElement.Name.LocalName) { case ParagraphItem.Fb2ParagraphElementName: ParagraphItem paragraph = new ParagraphItem(); try { paragraph.Load(xElement); _content.Add(paragraph); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section paragraph : {0}.", ex.Message)); } break; case PoemItem.Fb2PoemElementName: PoemItem poem = new PoemItem(); try { poem.Load(xElement); _content.Add(poem); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section poem : {0}.", ex.Message)); } break; case ImageItem.Fb2ImageElementName: ImageItem image = new ImageItem(); try { image.Load(xElement); AddImage(image); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section image : {0}.", ex.Message)); } break; case SubTitleItem.Fb2SubtitleElementName: SubTitleItem subtitle = new SubTitleItem(); try { subtitle.Load(xElement); _content.Add(subtitle); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section subtitle : {0}.", ex.Message)); } break; case CiteItem.Fb2CiteElementName: CiteItem cite = new CiteItem(); try { cite.Load(xElement); _content.Add(cite); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section citation : {0}.", ex.Message)); } break; case EmptyLineItem.Fb2EmptyLineElementName: EmptyLineItem eline = new EmptyLineItem(); _content.Add(eline); break; case TableItem.Fb2TableElementName: TableItem table = new TableItem(); try { table.Load(xElement); _content.Add(table); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section emptly line : {0}.", ex.Message)); } break; case Fb2TextSectionElementName: // internal <section> read recurive SectionItem section = new SectionItem(); try { section.Load(xElement); AddSection(section); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load sub-section : {0}.", ex.Message)); } break; case AnnotationItem.Fb2AnnotationItemName: // already processed break; case TitleItem.Fb2TitleElementName: // already processed break; case EpigraphItem.Fb2EpigraphElementName: // already processed break; default: if (!string.IsNullOrEmpty(xElement.Value)) { ParagraphItem tempParagraph = new ParagraphItem(); try { SimpleText text = new SimpleText { Text = xElement.Value }; tempParagraph.ParagraphData.Add(text); _content.Add(tempParagraph); } catch { // ignored } } Debug.Print("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName); break; } } ID = null; XAttribute xID = xSection.Attribute("id"); if ((xID != null)) { ID = xID.Value; } Lang = null; XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang"); if ((xLang != null)) { Lang = xLang.Value; } }
private static void AppendAnnotation(AnnotationItem item, int itemDepth, List<AbstractTextPart> appendTo) { foreach (IFb2TextItem content in item.Content) { if (content is SectionItem) { AppendSectionItem(content as SectionItem, itemDepth + 1, appendTo); continue; } if (content is ParagraphItem) { ParagraphItem paragraph = content as ParagraphItem; foreach (StyleType styleType in paragraph.ParagraphData) { AppendStyleType(styleType, appendTo); } continue; } if (content is EmptyLineItem) { appendTo.Add(SimpleText.EmptyParagraph); continue; } } }
public void Load(XElement xTitleInfo) { if ( xTitleInfo == null ) { throw new ArgumentNullException("xTitleInfo"); } // Load genres _genres.Clear(); IEnumerable<XElement> xGenres = xTitleInfo.Elements(FileNameSpace + GenreElementName); foreach ( XElement xGenre in xGenres ) { if ( (xGenre != null) ) { var genre = new TitleGenreType {Genre = xGenre.Value}; XAttribute xMatch = xGenre.Attribute("match"); if (xMatch != null && !string.IsNullOrEmpty(xMatch.Value)) { int percentage; if (int.TryParse(xMatch.Value,out percentage)) { genre.Match = percentage; } } _genres.Add(genre); } } // Load authors _bookAuthors.Clear(); IEnumerable<XElement> xAuthors = xTitleInfo.Elements(FileNameSpace + AuthorType.AuthorElementName); foreach (XElement xAuthor in xAuthors ) { var author = new AuthorItem { Namespace = FileNameSpace }; try { author.Load(xAuthor); _bookAuthors.Add(author); } catch (Exception ex) { Debug.Fail(string.Format("Error reading author: {0}",ex.Message)); continue; } } // Load Title BookTitle = null; XElement xBookTitle = xTitleInfo.Element(FileNameSpace + BookTitleElementName); if (xBookTitle != null) { BookTitle = new TextFieldType(); try { BookTitle.Load(xBookTitle); } catch (Exception ex) { Debug.Fail(string.Format("Error reading book title: {0}", ex.Message)); } } // Load Annotation Annotation = null; XElement xAnnotation = xTitleInfo.Element(FileNameSpace + AnnotationElementName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.Fail(string.Format("Error reading annotation: {0}", ex.Message)); } } // Load keywords Keywords = null; XElement xKeywords = xTitleInfo.Element(FileNameSpace + KeywordsElementName); if (xKeywords != null) { Keywords = new TextFieldType(); try { Keywords.Load(xKeywords); } catch (Exception ex) { Debug.Fail(string.Format("Error reading keywords: {0}", ex.Message)); } } // Load Book date BookDate = null; XElement xBookDate = xTitleInfo.Element(FileNameSpace + DateItem.Fb2DateElementName); if (xBookDate != null) { BookDate = new DateItem(); try { BookDate.Load(xBookDate); } catch (Exception ex) { Debug.Fail(string.Format("Error reading book date: {0}", ex.Message)); } } Cover = null; // we should load coverpage images here but no use for them as for now XElement xCoverPage = xTitleInfo.Element(FileNameSpace + CoverPageElementName); if ( xCoverPage != null) { Cover = new CoverPage{Namespace = FileNameSpace}; try { Cover.Load(xCoverPage); } catch (Exception ex) { Debug.Fail(string.Format("Error reading cover: {0}", ex.Message)); } } // Load Language Language = null; XElement xLanguage = xTitleInfo.Element(FileNameSpace + LanguageElementName); if ( (xLanguage != null)) { Language = xLanguage.Value; } else { Debug.Write("Language not specified in title section"); } // Load source language SrcLanguage = null; XElement xSrcLanguage = xTitleInfo.Element(FileNameSpace + SourceLanguageElementName); if ( (xSrcLanguage != null) ) { SrcLanguage = xSrcLanguage.Value; } // Load translators _translators.Clear(); IEnumerable<XElement> xTranslators = xTitleInfo.Elements(FileNameSpace + AuthorType.TranslatorElementName); foreach ( XElement xTranslator in xTranslators ) { var translator = new TranslatorItem { Namespace = FileNameSpace }; try { translator.Load(xTranslator); _translators.Add(translator); } catch (Exception ex) { Debug.Fail(string.Format("Error reading translator: {0}", ex.Message)); continue; } } // Load sequences ItemSequences.Clear(); IEnumerable<XElement> xSequences = xTitleInfo.Elements(FileNameSpace + SequenceType.SequenceElementName); foreach (var xSequence in xSequences) { var sec = new SequenceType{ Namespace = FileNameSpace }; try { sec.Load(xSequence); ItemSequences.Add(sec); } catch (Exception ex) { Debug.Fail(string.Format("Error reading sequence data: {0}",ex.Message)); continue; } } }