Exemplo n.º 1
0
        /// <summary>
        /// Converts FB2 annotation element
        /// </summary>
        /// <param name="annotationItem">item to convert</param>
        /// <param name="converterParams"></param>
        /// <returns>XHTML representation</returns>
        public HTMLItem Convert(AnnotationType annotationItem, AnnotationConverterParamsV3 converterParams)
        {
            if (annotationItem == null)
            {
                throw new ArgumentNullException("annotationItem");
            }
            var resAnnotation = new Div(HTMLElementType.HTML5);

            foreach (var element in annotationItem.Content)
            {
                if (element is SubTitleItem)
                {
                    var subtitleConverter = new SubtitleConverterV3();
                    resAnnotation.Add(subtitleConverter.Convert(element as SubTitleItem,
                        new SubtitleConverterParamsV3 { Settings = converterParams.Settings }));
                }
                else if (element is ParagraphItem)
                {
                    var paragraphConverter = new ParagraphConverterV3();
                    resAnnotation.Add(paragraphConverter.Convert(element as ParagraphItem,
                        new ParagraphConverterParamsV3 { Settings = converterParams.Settings, ResultType = ParagraphConvTargetEnumV3.Paragraph, StartSection = false }));
                }
                else if (element is PoemItem)
                {
                    var poemConverter = new PoemConverterV3();
                    resAnnotation.Add(poemConverter.Convert(element as PoemItem,
                        new PoemConverterParamsV3 { Level = converterParams.Level + 1, Settings = converterParams.Settings }));
                }
                else if (element is CiteItem)
                {
                    var citationConverter = new CitationConverterV3();
                    resAnnotation.Add(citationConverter.Convert(element as CiteItem,
                        new CitationConverterParamsV3 { Level = converterParams.Level + 1, Settings = converterParams.Settings }));
                }
                else if (element is TableItem)
                {
                    var tableConverter = new TableConverterV3();
                    resAnnotation.Add(tableConverter.Convert(element as TableItem,
                        new TableConverterParamsV3 { Settings = converterParams.Settings }
                        ));
                }
                else if (element is EmptyLineItem)
                {
                    var emptyLineConverter = new EmptyLineConverterV3();
                    resAnnotation.Add(emptyLineConverter.Convert());
                }
            }

            resAnnotation.GlobalAttributes.ID.Value = converterParams.Settings.ReferencesManager.AddIdUsed(annotationItem.ID, resAnnotation);

            SetClassType(resAnnotation, ElementStylesV3.Annotation);
            return resAnnotation;
        }
Exemplo n.º 2
0
        internal void Load(XElement xDocumentInfo)
        {
            if (xDocumentInfo == null)
            {
                throw new ArgumentNullException("xDocumentInfo");
            }


            // Load document authors 
            _documentAuthors.Clear();
            IEnumerable<XElement> xAuthors = xDocumentInfo.Elements(_fileNameSpace + AuthorType.AuthorElementName);
            if ( xAuthors != null )
            {
                foreach ( XElement xAuthor in xAuthors)
                {
                    AuthorItem author = new AuthorItem{ Namespace = _fileNameSpace };
                    try
                    {
                        author.Load(xAuthor);
                        _documentAuthors.Add(author);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(string.Format("Error reading document authors : {0}",ex.Message));
                        continue;
                    }
                }
            }

            // load Program used to create
            ProgramUsed2Create = null;
            XElement xProgramUsed = xDocumentInfo.Element(_fileNameSpace + ProgramUsedElementName);
            if (xProgramUsed != null) 
            {
                ProgramUsed2Create = new TextFieldType();
                try
                {
                    ProgramUsed2Create.Load(xProgramUsed);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading program used to create : {0}", ex.Message));
                }
            }

            // Load creation date 
            DocumentDate = null;
            XElement xDate = xDocumentInfo.Element(_fileNameSpace + DateItem.Fb2DateElementName);
            if (xDate != null)
            {
                DocumentDate = new DateItem();
                try
                {
                    DocumentDate.Load(xDate);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading document date : {0}", ex.Message));
                }
            }

            // Load Source URLs
            _sourceUrLs.Clear();
            IEnumerable<XElement> xSrcURLs = xDocumentInfo.Elements(_fileNameSpace + SourceURLElementName);
            if ( xSrcURLs != null )
            {
                foreach ( XElement xSrcURL in xSrcURLs )
                {
                    if ( (xSrcURL != null) && (xSrcURL.Value != null) )
                    {
                        string srcURL = xSrcURL.Value;
                        _sourceUrLs.Add(srcURL);
                    }
                }
            }

            // Load SourceOCR
            SourceOCR = null;
            XElement xSrcOcr = xDocumentInfo.Element(_fileNameSpace + SourceOCRElementName);
            if  (xSrcOcr != null)
            {
                SourceOCR = new TextFieldType();
                try
                {
                    SourceOCR.Load(xSrcOcr);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading source OCR : {0}", ex.Message));
                }
            }

            // load document's ID
            ID = null;
            XElement xID = xDocumentInfo.Element(_fileNameSpace + IdElementName);
            if ( (xID != null) && (xID.Value != null) )
            {
                ID = xID.Value;
            }

            // load document's version
            DocumentVersion = null;
            XElement xVersion = xDocumentInfo.Element(_fileNameSpace + VersionElementName);
            if ( (xVersion != null) && (xVersion.Value != null))
            {
                string version = xVersion.Value;
                try
                {
                    var cult = new CultureInfo("");

                    DocumentVersion = float.Parse(version, cult.NumberFormat);
                }
                catch(FormatException ex)
                {
                    Debug.WriteLine(string.Format("Error reading document version : {0}", ex.Message));
                }
            }

            // Load change history 
            History = null;
            XElement xHistory = xDocumentInfo.Element(_fileNameSpace + HistoryElementName);
            if (xHistory != null) 
            {
                History = new AnnotationType() { ElementName = HistoryElementName };
                try
                {
                    History.Load(xHistory);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading document history : {0}", ex.Message));
                }
            }

            // Load copyright owners
            _documentPublishers.Clear();
            IEnumerable<XElement> xPublishers = xDocumentInfo.Elements(_fileNameSpace + AuthorType.PublisherElementName);
            if ( xPublishers != null )
            {
                foreach (XElement xPublisher in xPublishers )
                {
                    PublisherItem publisher = new PublisherItem { Namespace = _fileNameSpace };
                    try
                    {
                        publisher.Load(xPublisher);
                        _documentPublishers.Add(publisher);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(string.Format("Error reading document publishers : {0}", ex.Message));
                        continue;
                    }
                }
            }
        }