コード例 #1
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        public string GetFileName(FictionBook fictionbook)
        {
            if (fictionbook == null)
            {
                throw new ArgumentNullException("fictionbook");
            }

            return this.pattern.ApplyPattern(this.template, fictionbook);
        }
コード例 #2
0
ファイル: BatchFilesProcessor.cs プロジェクト: avgx/knigoskop
        private string GetFilename(string directory, string filename, FictionBook fictionBook)
        {
            filename = filename.ToLowerInvariant();

            return filename;
        }
コード例 #3
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #4
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
 public override string ApplyPattern(string pattern, FictionBook fictionBook)
 {
     return replaceRegex.Replace(pattern, String.Empty);
 }
コード例 #5
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #6
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #7
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #8
0
        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;
        }
コード例 #9
0
        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;
                    }
                }
            }
        }
コード例 #10
0
        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;
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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;
            }
        }
コード例 #13
0
ファイル: BatchFilesProcessor.cs プロジェクト: avgx/knigoskop
        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;
            }
        }
コード例 #14
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
 public abstract string ApplyPattern(string pattern, FictionBook fictionBook);
コード例 #15
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
            {
                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;
            }
        }
コード例 #16
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
 public abstract bool HasValue(FictionBook fictionBook);
コード例 #17
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #18
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #19
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #20
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #21
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #22
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #23
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #24
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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);
        }
コード例 #25
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        public override bool HasValue(FictionBook fictionBook)
        {
            if (fictionBook.TitleInfo != null)
            {
                if (!String.IsNullOrEmpty(fictionBook.TitleInfo.BookTitle))
                {
                    return true;
                }
            }

            return false;
        }
コード例 #26
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
 public override bool HasValue(FictionBook fictionBook)
 {
     return false;
 }
コード例 #27
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        public override string ApplyPattern(string pattern, FictionBook fictionBook)
        {
            string result = Template;

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

            return result;
        }
コード例 #28
0
ファイル: FileNameProvider.cs プロジェクト: Gremlin2/Fb2Fix
        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;
        }
コード例 #29
0
ファイル: BatchFilesProcessor.cs プロジェクト: avgx/knigoskop
 private void ChangeDocumentVersion(FictionBook fictionBook)
 {
     if ((fictionBook.ModificationType & ModificationType.DocumentInfo) == ModificationType.DocumentInfo)
     {
         fictionBook.Version = 1.0f;
         return;
     }
 }