/// <summary>
 /// Converts FB2 Title object to XHTML Title 
 /// </summary>
 /// <param name="titleItem">title item to convert</param>
 /// <param name="titleConverterParams"></param>
 /// <returns></returns>
 public Div Convert(TitleItem titleItem,  TitleConverterParamsV2 titleConverterParams)
 {
     if (titleItem == null)
     {
         throw new ArgumentNullException("titleItem");
     }
     _level = titleConverterParams.TitleLevel;
     var title = new Div(HTMLElementType.XHTML11);
     foreach (var fb2TextItem in titleItem.TitleData)
     {
         if (fb2TextItem is ParagraphItem)
         {
             var paragraphStyle = GetParagraphStyleByLevel(_level);
             var paragraphConverter = new ParagraphConverterV2();
             title.Add(paragraphConverter.Convert(fb2TextItem as ParagraphItem,
                 new ParagraphConverterParamsV2 { ResultType = paragraphStyle, Settings = titleConverterParams.Settings, StartSection = false }));
         }
         else if (fb2TextItem is EmptyLineItem)
         {
             var emptyLineConverter = new EmptyLineConverterV2();
             title.Add(emptyLineConverter.Convert());
         }
         else
         {
             Debug.WriteLine("invalid type in Title - {0}", fb2TextItem.GetType());
         }
     }
     SetClassType(title, ElementStylesV2.PoemTitle);
     return title;
 }
Exemplo n.º 2
0
        public XNode ToXML()
        {
            XElement xTitle = new XElement(Fb2Const.fb2DefaultNamespace + Fb2TitleElementName);

            if (!string.IsNullOrEmpty(LangAttribute))
            {
                xTitle.Add(new XAttribute(XNamespace.Xml + "lang", LangAttribute));
            }

            foreach (IFb2TextItem TitleItem in titleData)
            {
                xTitle.Add(TitleItem.ToXML());
            }
            return(xTitle);
        }
Exemplo n.º 3
0
        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;
            }
        }
Exemplo n.º 4
0
        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;
            }
        }
Exemplo n.º 5
0
 private void ClearAll()
 {
     content.Clear();
     subSections.Clear();
     images.Clear();
     sectionImages.Clear();
     epigraphs.Clear();
     ID = null;
     Annotation = null;
     Title = null;
 }