Exemple #1
0
        public string GetFileName(FictionBook fictionbook)
        {
            if (fictionbook == null)
            {
                throw new ArgumentNullException("fictionbook");
            }

            return this.pattern.ApplyPattern(this.template, fictionbook);
        }
        private string GetFilename(string directory, string filename, FictionBook fictionBook)
        {
            filename = filename.ToLowerInvariant();

            return filename;
        }
Exemple #3
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (fictionBook.TitleInfo.Sequences != null && fictionBook.TitleInfo.Sequences.Count > 0)
            {
                SequenceInfoNode sequenceInfoNode = fictionBook.TitleInfo.Sequences[0];
                if (!String.IsNullOrEmpty(sequenceInfoNode.Name))
                {
                    value = CleanupValue(sequenceInfoNode.Name);
                }
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #4
0
 public override string ApplyPattern(string pattern, FictionBook fictionBook)
 {
     return replaceRegex.Replace(pattern, String.Empty);
 }
Exemple #5
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string result = Template;
            bool hasContent = false;

            foreach (FileNamePatternElement element in Elements)
            {
                if(element.HasValue(fictionBook))
                {
                    hasContent = true;
                }

                result = element.ApplyPattern(result, fictionBook);
            }

            if(!hasContent)
            {
                return replaceRegex.Replace(pattern, String.Empty);
            }

            return replaceRegex.Replace(pattern, result);
        }
Exemple #6
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Genres != null && fictionBook.TitleInfo.Genres.Count > 0)
            {
                GenreInfoNode genreInfoNode = fictionBook.TitleInfo.Genres[0];
                if (!String.IsNullOrEmpty(genreInfoNode.Genre))
                {
                    Genre genre = GenreTable.Table[genreInfoNode.Genre];
                    if (genre != null)
                    {
                        if(!String.IsNullOrEmpty(genre.GetDescription(this.lang)))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
Exemple #7
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Authors != null && fictionBook.TitleInfo.Authors.Count > 0)
            {
                AuthorInfoNode autorInfo = fictionBook.TitleInfo.Authors[0];

                if (!String.IsNullOrEmpty(autorInfo.MiddleName))
                {
                    foreach (char c in autorInfo.MiddleName)
                    {
                        if (Char.IsLetterOrDigit(c))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
        private FictionBook ReadFictionBook(XmlReader reader)
        {
            XmlDocument document = new XmlDocument();
            document.Load(reader);

            FictionBook fictionBook = new FictionBook(document);

            if (fictionBook.DocumentStatus == Fb2FixStatus.Passed && this.options.force == false)
            {
                return fictionBook;
            }

            fictionBook.CheckDocumentHeader(options);

            PostProcessDocument(fictionBook);

            fictionBook.DocumentStatus = Fb2FixStatus.Passed;

            return fictionBook;
        }
        private void PostProcessDocument(FictionBook fictionBook)
        {
            List<XmlElement> invalidNodes;

            if (fictionBook == null)
            {
                throw new ArgumentNullException("fictionBook");
            }

            XmlDocument document = fictionBook.Document;

            invalidNodes = new List<XmlElement>(64);
            ProcessElement(fictionBook, document.DocumentElement, invalidNodes);

            //foreach (XmlElement node in invalidNodes)
            //{
            //    if(node == null)
            //    {
            //        continue;
            //    }

            //    XmlElement parent = node.ParentNode as XmlElement;
            //    if (parent != null && parent.NodeType == XmlNodeType.Element)
            //    {
            //        XmlComment comment = parent.OwnerDocument.CreateComment(node.OuterXml);
            //        parent.ReplaceChild(comment, node);
            //    }
            //}

            XmlNodeList nodes = document.SelectNodes("//FictionBook/descendant::p");
            List<XmlElement> paragraphNodes = new List<XmlElement>(nodes.Count);

            foreach (XmlNode node in nodes)
            {
                XmlElement paragraph = node as XmlElement;
                if (paragraph != null)
                {
                    paragraphNodes.Add(paragraph);
                }
            }

            for (int index = paragraphNodes.Count - 1; index >= 0; index--)
            {
                XmlElement paragraphElement = paragraphNodes[index];
                XmlElement parentElement = paragraphElement.ParentNode as XmlElement;
                if (parentElement != null && String.Compare(parentElement.LocalName, "p") == 0)
                {
                    XmlElement precedingElement = parentElement.ParentNode as XmlElement;
                    if (precedingElement != null)
                    {
                        parentElement.RemoveChild(paragraphElement);
                        precedingElement.InsertAfter(paragraphElement, parentElement);
                        fictionBook.ModificationType = ModificationType.Body;
                    }
                }
            }
        }
        private void ProcessElement(FictionBook fictionBook, XmlNode node, List<XmlElement> invalidNodes)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {
                ProcessElement(fictionBook, childNode, invalidNodes);
            }

            switch (node.NodeType)
            {
                case XmlNodeType.Text:
                    string text = node.InnerText;

                    text = bullets.Replace(text, new MatchEvaluator(delegate(Match match)
                    {
                        fictionBook.ModificationType = ModificationType.Text;
                        return "-";
                    }));

                    text = invalidChars.Replace(text, new MatchEvaluator(delegate(Match match)
                    {
                        fictionBook.ModificationType = ModificationType.Text;
                        return " ";
                    }));

                    node.InnerText = text;

                    //node.InnerText = invalidChars.Replace(bullets.Replace(node.InnerText, "-"), " ");
                    break;

                case XmlNodeType.Element:
                    ElementDecl elementDecl = this.fb2Dtd.FindElement(node.LocalName);
                    if(elementDecl == null)
                    {
                        invalidNodes.Add(node as XmlElement);
                    }
                    break;
            }
        }
        private string GetFilename(string directory, string filename, FictionBook fictionBook)
        {
            if(this.options.rename)
            {
                filename = this.provider.GetFileName(fictionBook);
            }

            if(this.options.translify)
            {
                filename = StringUtils.Translify(filename);
            }

            filename = StringUtils.Dirify(filename, this.options.strict);

            if (!String.IsNullOrEmpty(this.options.replaceChar))
            {
                filename = Regex.Replace(filename, @"(\s)", this.options.replaceChar);

                if (this.options.replaceChar.Length == 1)
                {
                    filename = StringUtils.Squeeze(filename, this.options.replaceChar[0]);
                }
            }

            if(this.options.uppercaseFilenames)
            {
                filename = filename.ToUpperInvariant();
            }
            else if(this.options.lowercaseFilenames)
            {
                filename = filename.ToLowerInvariant();
            }

            string separator = Path.DirectorySeparatorChar.ToString();
            while(filename.StartsWith(separator))
            {
                filename = filename.Substring(1);
            }

            filename = StringUtils.Squeeze(filename, Path.DirectorySeparatorChar);

            return Path.Combine(directory, filename);
        }
        private void ChangeDocumentVersion(FictionBook fictionBook)
        {
            if((fictionBook.ModificationType & ModificationType.DocumentInfo) == ModificationType.DocumentInfo)
            {
                fictionBook.Version = 1.0f;
                return;
            }

            if (this.options.incversion)
            {
                float version = 0.0f;

                if ((fictionBook.ModificationType & ModificationType.Description) == ModificationType.Description)
                {
                    version = Math.Max(version, 0.01f);
                }

                if ((fictionBook.ModificationType & ModificationType.Body) == ModificationType.Body)
                {
                    version = Math.Max(version, 0.1f);
                }

                if ((fictionBook.ModificationType & ModificationType.Text) == ModificationType.Text)
                {
                    version = Math.Max(version, 0.5f);
                }

                fictionBook.Version += version;
            }
        }
Exemple #13
0
        private void SaveFictionBook(string directory, string filename, FictionBook fictionBook, Encoding encoding)
        {
            string outputFilename = String.Empty;
            XmlDocument document = fictionBook.Document;

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            try
            {
                outputFilename = GetOutputFileName(directory, filename, ".fb2");

                using (Fb2TextWriter writer = new Fb2TextWriter(outputFilename, encoding))
                {

                    writer.WriteStartDocument();

                    document.WriteTo(writer);
                    writer.Flush();
                }

                if (!String.IsNullOrEmpty(outputFilename))
                {
                    DateTime dt = fictionBook.ContainerDateTime;

                    if (!dt.IsDaylightSavingTime())
                    {
                        dt = dt.AddHours(-1);
                    }

                    File.SetCreationTime(outputFilename, dt);
                    File.SetLastAccessTime(outputFilename, dt);
                    File.SetLastWriteTime(outputFilename, dt);
                }
            }
            catch (Exception)
            {
                if (!String.IsNullOrEmpty(outputFilename))
                {
                    if (File.Exists(outputFilename))
                    {
                        try
                        {
                            File.Delete(outputFilename);
                        }
                        catch (Exception exp)
                        {
                            ApplicationLogger.WriteStringToLog(exp.Message);
                        }
                    }
                }
                throw;
            }
        }
Exemple #14
0
 public abstract string ApplyPattern(string pattern, FictionBook fictionBook);
        private void SaveFictionBook(string directory, string filename, FictionBook fictionBook, Encoding encoding)
        {
            string outputFilename = String.Empty;
            XmlDocument document = fictionBook.Document;

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            try
            {
                if (this.options.compress)
                {
                    outputFilename = GetOutputFileName(directory, filename, ".zip");

                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (Fb2TextWriter writer = new Fb2TextWriter(memoryStream, encoding))
                        {
                            writer.IndentHeader = this.options.indentHeader;
                            writer.IndentBody = this.options.indentBody;

                            writer.WriteStartDocument();

                            document.WriteTo(writer);
                            writer.Flush();

                            memoryStream.Capacity = (int) memoryStream.Length;
                            memoryStream.Seek(0, SeekOrigin.Begin);

                            using (ZipFile file = ZipFile.Create(outputFilename))
                            {
                                file.UseZip64 = UseZip64.Off;

                                ZipEntry entry = file.EntryFactory.MakeFileEntry(filename + ".fb2", false);

                                entry.DateTime = fictionBook.ContainerDateTime;

                                file.BeginUpdate();
                                file.Add(new StreamDataSource(memoryStream), entry);
                                file.CommitUpdate();
                            }
                        }
                    }
                }
                else
                {
                    outputFilename = GetOutputFileName(directory, filename, ".fb2");

                    using (Fb2TextWriter writer = new Fb2TextWriter(outputFilename, encoding))
                    {
                        writer.IndentHeader = this.options.indentHeader;
                        writer.IndentBody = this.options.indentBody;

                        writer.WriteStartDocument();

                        document.WriteTo(writer);
                        writer.Flush();
                    }
                }

                if(!String.IsNullOrEmpty(outputFilename))
                {
                    DateTime dt = fictionBook.ContainerDateTime;

                    if (!dt.IsDaylightSavingTime())
                    {
                        dt = dt.AddHours(-1);
                    }

                    File.SetCreationTime(outputFilename, dt);
                    File.SetLastAccessTime(outputFilename, dt);
                    File.SetLastWriteTime(outputFilename, dt);
                }
            }
            catch (Exception)
            {
                if(!String.IsNullOrEmpty(outputFilename))
                {
                    if(File.Exists(outputFilename))
                    {
                        try
                        {
                            File.Delete(outputFilename);
                        }
                        catch (Exception exp)
                        {
                            Logger.WriteLine(TraceEventType.Verbose, exp);
                        }
                    }
                }
                throw;
            }
        }
Exemple #16
0
 public abstract bool HasValue(FictionBook fictionBook);
Exemple #17
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (fictionBook.TitleInfo.Sequences != null && fictionBook.TitleInfo.Sequences.Count > 0)
            {
                SequenceInfoNode sequenceInfoNode = fictionBook.TitleInfo.Sequences[0];
                if (sequenceInfoNode.Number != null)
                {
                    value = sequenceInfoNode.Number.Value.ToString(this.numberFormat, CultureInfo.InvariantCulture);
                }
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #18
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (fictionBook.TitleInfo.Authors != null && fictionBook.TitleInfo.Authors.Count > 0)
            {
                AuthorInfoNode autorInfo = fictionBook.TitleInfo.Authors[0];

                if (!String.IsNullOrEmpty(autorInfo.MiddleName))
                {
                    foreach (char c in autorInfo.MiddleName)
                    {
                        if (Char.IsLetterOrDigit(c))
                        {
                            value = c.ToString().ToUpperInvariant();
                            break;
                        }
                    }
                }
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #19
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Sequences != null && fictionBook.TitleInfo.Sequences.Count > 0)
            {
                SequenceInfoNode sequenceInfoNode = fictionBook.TitleInfo.Sequences[0];
                if (sequenceInfoNode.Number != null)
                {
                    return true;
                }
            }

            return false;
        }
Exemple #20
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (fictionBook.TitleInfo.Genres != null && fictionBook.TitleInfo.Genres.Count > 0)
            {
                GenreInfoNode genreInfoNode = fictionBook.TitleInfo.Genres[0];
                if (!String.IsNullOrEmpty(genreInfoNode.Genre))
                {
                    Genre genre = GenreTable.Table[genreInfoNode.Genre];
                    if(genre != null)
                    {
                        value = CleanupValue(genre.GetDescription(this.lang));
                    }
                }
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #21
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Authors != null && fictionBook.TitleInfo.Authors.Count > 0)
            {
                AuthorInfoNode autorInfo = fictionBook.TitleInfo.Authors[0];
                if (!String.IsNullOrEmpty(autorInfo.NickName))
                {
                    return true;
                }
            }

            return false;
        }
Exemple #22
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Genres != null && fictionBook.TitleInfo.Genres.Count > 0)
            {
                GenreInfoNode genreInfoNode = fictionBook.TitleInfo.Genres[0];
                if (!String.IsNullOrEmpty(genreInfoNode.Genre))
                {
                    return true;
                }
            }

            return false;
        }
Exemple #23
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (!String.IsNullOrEmpty(fictionBook.TitleInfo.BookTitle))
            {
                value = CleanupValue(fictionBook.TitleInfo.BookTitle);
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #24
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string value = String.Empty;

            if (fictionBook.TitleInfo.Authors != null && fictionBook.TitleInfo.Authors.Count > 0)
            {
                AuthorInfoNode autorInfo = fictionBook.TitleInfo.Authors[0];
                if (!String.IsNullOrEmpty(autorInfo.FirstName))
                {
                    value = CleanupValue(autorInfo.FirstName);
                }
            }

            return replacePattern.Replace(pattern, value);
        }
Exemple #25
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo != null)
            {
                if (!String.IsNullOrEmpty(fictionBook.TitleInfo.BookTitle))
                {
                    return true;
                }
            }

            return false;
        }
Exemple #26
0
 public override bool HasValue(FictionBook fictionBook)
 {
     return false;
 }
Exemple #27
0
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string result = Template;

            foreach (FileNamePatternElement element in elements)
            {
                result = element.ApplyPattern(result, fictionBook);
            }

            return result;
        }
Exemple #28
0
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo.Sequences != null && fictionBook.TitleInfo.Sequences.Count > 0)
            {
                SequenceInfoNode sequenceInfoNode = fictionBook.TitleInfo.Sequences[0];
                if (!String.IsNullOrEmpty(sequenceInfoNode.Name))
                {
                    return true;
                }
            }

            return false;
        }
Exemple #29
0
 private void ChangeDocumentVersion(FictionBook fictionBook)
 {
     if ((fictionBook.ModificationType & ModificationType.DocumentInfo) == ModificationType.DocumentInfo)
     {
         fictionBook.Version = 1.0f;
         return;
     }
 }