Beispiel #1
0
        public List <AnnotationItem> ParseAnnotation(string text)
        {
            List <AnnotationItem> theAnnotations = new List <AnnotationItem>();

            string[] tmp = Regex.Split(text, "\n");

            try
            {
                foreach (string t in tmp)
                {
                    if (t.Length == 0)
                    {
                        continue;
                    }
                    string[]       cols = Regex.Split(t, "-");
                    AnnotationItem a    = new AnnotationItem(cols[0], int.Parse(cols[1]));
                    theAnnotations.Add(a);
                }
            } catch
            {
                MessageBox.Show("Anotation does not seem to be in a correct format.");
            }

            return(theAnnotations);
        }
Beispiel #2
0
        /// <summary>
        /// Exports annotation item to string
        /// </summary>
        /// <param name="annotationItem">Annotation item instance</param>
        /// <returns>Prepared annotation item data structure</returns>
        public string Export(AnnotationItem annotationItem, string host = "")
        {
            //init xml data serializer
            XmlSerializer serializer = new XmlSerializer(typeof(AnnotationItem));

            var sb = new StringBuilder();

            XmlWriterSettings settings = new XmlWriterSettings();

            //settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string
            settings.Encoding = Encoding.UTF8;
            //settings.Indent = false;
            //settings.OmitXmlDeclaration = false;


            foreach (var res in annotationItem.Object.Resources)
            {
                foreach (var onlineRes in res.OnlineResources)
                {
                    if (!onlineRes.DownloadUrl.EndsWith("/"))
                    {
                        onlineRes.DownloadUrl = host + "/" + onlineRes.DownloadUrl;
                    }
                    else
                    {
                        onlineRes.DownloadUrl = host + onlineRes.DownloadUrl;
                    }


                    if (!onlineRes.ReferenceUrl.EndsWith("/"))
                    {
                        onlineRes.ReferenceUrl = host + "/" + onlineRes.ReferenceUrl;
                    }
                    else
                    {
                        onlineRes.ReferenceUrl = host + onlineRes.ReferenceUrl;
                    }
                }
            }


            using (StringWriter textWriter = new StringWriter(sb))
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    serializer.Serialize(xmlWriter, annotationItem);
                }
            }

            return(sb.ToString());
        }
Beispiel #3
0
        private static void AnnotationJustification(AnnotationItem annotationItem, StringFormat format)
        {
            switch (annotationItem.Justification)
            {
            case AnnotationItem.HAlignment.Right:
                format.Alignment = StringAlignment.Far;
                break;

            case AnnotationItem.HAlignment.Center:
                format.Alignment = StringAlignment.Center;
                break;

            default:
                format.Alignment = StringAlignment.Near;
                break;
            }
        }
Beispiel #4
0
        private static void AnnotationVerticalAlignment(AnnotationItem annotationItem, StringFormat format)
        {
            switch (annotationItem.VerticalAlignment)
            {
            case AnnotationItem.VAlignment.Top:
                format.LineAlignment = StringAlignment.Near;
                break;

            case AnnotationItem.VAlignment.Center:
                format.LineAlignment = StringAlignment.Center;
                break;

            default:
                format.LineAlignment = StringAlignment.Far;
                break;
            }
        }
Beispiel #5
0
        public void Load(XElement xTitleInfo)
        {
            if (xTitleInfo == null)
            {
                throw new ArgumentNullException("xTitleInfo");
            }

            // Load genres
            genres.Clear();
            IEnumerable <XElement> xGenres = xTitleInfo.Elements(fileNameSpace + GenreElementName);

            if (xGenres != null)
            {
                foreach (XElement xGenre in xGenres)
                {
                    if ((xGenre != null) && (xGenre.Value != null))
                    {
                        TitleGenreType genre = new TitleGenreType();
                        genre.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);

            if (xAuthors != null)
            {
                foreach (XElement xAuthor in xAuthors)
                {
                    AuthorItem author = new AuthorItem {
                        Namespace = fileNameSpace
                    };
                    try
                    {
                        author.Load(xAuthor);
                        bookAuthors.Add(author);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(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.WriteLine(string.Format("Error reading cover: {0}", ex.Message));
                }
            }

            // Load Language
            Language = null;
            XElement xLanguage = xTitleInfo.Element(fileNameSpace + LanguageElementName);

            if ((xLanguage != null) && (xLanguage.Value != null))
            {
                Language = xLanguage.Value;
            }
            else
            {
                Debug.WriteLine("Language not specified in title section");
            }

            // Load source language
            SrcLanguage = null;
            XElement xSrcLanguage = xTitleInfo.Element(fileNameSpace + SourceLanguageElementName);

            if ((xSrcLanguage != null) && (xSrcLanguage.Value != null))
            {
                SrcLanguage = xSrcLanguage.Value;
            }

            // Load translators
            translators.Clear();
            IEnumerable <XElement> xTranslators = xTitleInfo.Elements(fileNameSpace + AuthorType.TranslatorElementName);

            if (xTranslators != null)
            {
                foreach (XElement xTranslator in xTranslators)
                {
                    TranslatorItem translator = new TranslatorItem()
                    {
                        Namespace = fileNameSpace
                    };
                    try
                    {
                        translator.Load(xTranslator);
                        translators.Add(translator);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(string.Format("Error reading translator: {0}", ex.Message));
                        continue;
                    }
                }
            }

            // Load sequences
            sequences.Clear();
            IEnumerable <XElement> xSequences = xTitleInfo.Elements(fileNameSpace + SequenceType.SequenceElementName);

            foreach (var xSequence in xSequences)
            {
                SequenceType sec = new SequenceType()
                {
                    Namespace = fileNameSpace
                };
                try
                {
                    sec.Load(xSequence);
                    if (!string.IsNullOrEmpty(sec.Name))
                    {
                        sequences.Add(sec);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Error reading sequence data: {0}", ex.Message));
                    //Debug.WriteLine(string.Format("Error reading sequence data: {0}",ex.Message));
                    continue;
                }
            }
        }