コード例 #1
0
ファイル: Process.cs プロジェクト: enckse/rsscl
        /// <summary>
        /// Process the request, executing the output action
        /// </summary>
        /// <param name='options'>
        /// Options for processing
        /// </param>
        /// <param name="manager">
        /// The manager
        /// </param>
        /// <param name='outputAction'>
        /// Output action.
        /// </param>
        private static void Execute(Option options, IFeedManager manager, Action<string> outputAction)
        {
            XmlWriter writer = null;
            var serializer = new JavaScriptSerializer();
            try
            {
                var buffer = new System.Text.StringBuilder();
                if (options.Format == Format.Xml)
                {
                    writer = XmlWriter.Create(buffer);
                }

                PrintHeader(options.Format, writer, buffer);
                bool firstFeed = true;
                foreach (var feed in manager.Get(options.InputFile))
                {
                    bool hasChild = true;
                    bool firstItem = true;
                    foreach (var item in manager.GetItems(feed, options.Since, options.IgnoreCerts))
                    {
                        if (hasChild)
                        {
                            if (options.Format == Format.Json)
                            {
                                if (!firstFeed)
                                {
                                    buffer.Append(",");
                                }

                                firstFeed = false;
                            }

                            PrintFeed(options.Format, feed, outputAction, writer, buffer, serializer);
                            hasChild = false;
                        }

                        if (options.Format == Format.Json)
                        {
                            if (!firstItem)
                            {
                                buffer.Append(",");
                            }

                            firstItem = false;
                        }

                        PrintItem(options.Format, item, outputAction, writer, buffer, serializer);
                        if (buffer.Length >= FlushBuffer)
                        {
                            outputAction(buffer.ToString());
                            buffer.Clear();
                        }
                    }

                    if (!hasChild)
                    {
                        PrintFeedEnd(options.Format, writer, buffer);
                    }
                }

                PrintEnd(options.Format, writer, buffer);
                if (options.Format != Format.Console)
                {
                    outputAction(buffer.ToString());
                }
            }
            finally
            {
                if (writer != null)
                {
                    ((IDisposable)writer).Dispose();
                }
            }
        }