protected FB2File LoadFB2StreamWithFix(Stream s, Func<Stream,FB2File> streamLoader)
        {
            var options = new Fb2FixArguments
            {
                incversion = true,
                regenerateId = false,
                indentBody = false,
                indentHeader = true,
                mapGenres = true,
                encoding = Encoding.UTF8
            };

            using (Stream output = Fb2Fix.Process(s, options))
            {
                return streamLoader(output);
            }
        }
Beispiel #2
0
        public BatchFilesProcessor(Fb2FixArguments options)
        {
            this.options = options;

            this.excludeList = new Dictionary<string, bool>(this.options.excludeList.Length, StringComparer.InvariantCultureIgnoreCase);

            foreach (string entry in this.options.excludeList)
            {
                this.excludeList.Add(entry, true);
            }

            if (String.IsNullOrEmpty(this.options.outputDirectory))
            {
                this.outputDirectoryRoot = Environment.CurrentDirectory;
            }
            else
            {
                this.outputDirectoryRoot = Path.GetFullPath(this.options.outputDirectory);
            }

            try
            {
                this.outputDirectoryGood = Path.Combine(this.outputDirectoryRoot, "Good");
                if (!Directory.Exists(this.outputDirectoryGood))
                {
                    Directory.CreateDirectory(this.outputDirectoryGood);
                }

                this.excludeList.Add(this.outputDirectoryGood, true);

                this.outputDirectoryBad = Path.Combine(this.outputDirectoryRoot, "Bad");
                if (!Directory.Exists(this.outputDirectoryBad))
                {
                    Directory.CreateDirectory(this.outputDirectoryBad);
                }

                this.excludeList.Add(this.outputDirectoryBad, true);

                if (options.validate)
                {
                    this.outputDirectoryNonValid = Path.Combine(this.outputDirectoryRoot, "NonValid");
                    if (!Directory.Exists(this.outputDirectoryNonValid))
                    {
                        Directory.CreateDirectory(this.outputDirectoryNonValid);
                    }

                    this.excludeList.Add(this.outputDirectoryNonValid, true);
                }
            }
            catch(IOException exp)
            {
                Logger.WriteLine(TraceEventType.Critical, exp.Message);
                Logger.WriteLine(TraceEventType.Verbose, exp);
                Logger.Flush();

                Environment.Exit(1);
            }
            catch (UnauthorizedAccessException exp)
            {
                Logger.WriteLine(TraceEventType.Critical, exp.Message);
                Logger.WriteLine(TraceEventType.Verbose, exp);
                Logger.Flush();

                Environment.Exit(1);
            }

            try
            {
                if (options.rename)
                {
                    options.pattern = options.pattern.Replace('/', Path.DirectorySeparatorChar);
                    options.pattern = options.pattern.Replace('\\', Path.DirectorySeparatorChar);

                    this.provider = new FileNameProvider(options.pattern);
                }

                if(!String.IsNullOrEmpty(options.encoding))
                {
                    Encoding prefered = Encoding.GetEncoding(options.encoding);
                    this.preferedCodepage = prefered.CodePage;
                }

                if(options.validate && !String.IsNullOrEmpty(options.xsdSchema))
                {
                    this.xsdSchema = new XmlSchemaSet();
                    this.xsdSchema.XmlResolver = new Fb2SchemaResolver(Path.GetDirectoryName(Path.GetFullPath(options.xsdSchema)));

                    using(FileStream stream = File.Open(options.xsdSchema, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        XmlSchema schema = XmlSchema.Read(stream, null);
                        this.xsdSchema.Add(schema);
                    }
                }
            }
            catch (Exception exp)
            {
                Logger.WriteLine(TraceEventType.Critical, exp.Message);
                Logger.WriteLine(TraceEventType.Verbose, exp);
                Logger.Flush();

                Environment.Exit(1);
            }
        }
Beispiel #3
0
        private static void Main(string[] args)
        {
            CustomTraceListener listener = null;

            Fb2FixArguments arguments = new Fb2FixArguments();

            ZipConstants.DefaultCodePage = 866;

            if (CommandLineParser.ParseArguments(args, arguments))
            {
                string workingDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

                //if(File.Exists(Path.Combine(workingDirectory, "fb2fix.rsp")) && !arguments.noconfig)
                //{
                //    arguments = new Fb2FixArguments();
                //    string[] argList = new string[args.Length + 1];

                //    argList[0] = "@" + Path.Combine(workingDirectory, "fb2fix.rsp");
                //    args.CopyTo(argList, 1);

                //    if(!CommandLineParser.ParseArguments(argList, arguments))
                //    {
                //        Console.WriteLine();
                //        PrintUsageInfo();
                //        Environment.Exit(1);
                //    }
                //}

                if (arguments.files.Length == 0)
                {
                    PrintUsageInfo();
                    Environment.Exit(1);
                }

                if(String.IsNullOrEmpty(arguments.dtdFile) || String.Compare(arguments.dtdFile, "fb2.dtd") == 0)
                {
                    arguments.dtdFile = Path.Combine(workingDirectory, "fb2.dtd");
                }

                if(String.IsNullOrEmpty(arguments.genres) || String.Compare(arguments.genres, "genrestransfer.xml") == 0)
                {
                    arguments.genres = Path.Combine(workingDirectory, "genrestransfer.xml");
                }

                if (String.IsNullOrEmpty(arguments.xsdSchema) || String.Compare(arguments.xsdSchema, "FictionBook.xsd") == 0)
                {
                    arguments.xsdSchema = Path.Combine(workingDirectory, "FictionBook.xsd");
                }

                CustomConsoleTraceListener infoConsoleListener = new CustomConsoleTraceListener();
                infoConsoleListener.Filter = new TraceEventTypeFilter(TraceEventTypes.Information, arguments.loglevel);
                Logger.Listeners.Add(infoConsoleListener);

                CustomConsoleTraceListener errorConsoleListener = new CustomConsoleTraceListener();
                errorConsoleListener.Filter = new TraceEventTypeFilter(TraceEventTypes.Critical | TraceEventTypes.Error | TraceEventTypes.Warning, arguments.loglevel);
                Logger.Listeners.Add(errorConsoleListener);

                if (!String.IsNullOrEmpty(arguments.logfile))
                {
                    listener = new CustomTraceListener(arguments.logfile);
                    listener.Filter = new EventTypeFilter((SourceLevels) arguments.loglevel);
                    Logger.Listeners.Add(listener);
                }

                try
                {
                    using (FileStream fileStream = File.Open(arguments.genres, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (XmlReader reader = XmlReader.Create(fileStream))
                        {
                            GenreTable.ReadGenreList(reader);
                        }
                    }

                    BatchFilesProcessor batch = new BatchFilesProcessor(arguments);
                    batch.Process(arguments.files);
                }
                catch (Exception exp)
                {
                    Logger.WriteLine(TraceEventType.Critical, exp.Message);
                    Logger.WriteLine(TraceEventType.Verbose, exp.ToString());
                }
                finally
                {
                    Logger.Flush();

                    if (listener != null)
                    {
                        listener.Flush();
                        listener.Close();
                    }
                }
            }
            else
            {
                Console.WriteLine();
                PrintUsageInfo();
                Environment.Exit(1);
            }
        }