/// <summary> /// Go and 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> public static void Go(Option options, IFeedManager manager, Action<string> outputAction) { if (options == null) { throw new ArgumentNullException("options"); } if (manager == null) { throw new ArgumentNullException("manager"); } if (outputAction == null) { throw new ArgumentNullException("outputAction"); } Execute(options, manager, outputAction); }
/// <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(); } } }
/// <summary> /// Parses the arguments. /// </summary> /// <returns> /// The arguments in an option wrapper for operations /// </returns> /// <param name='arguments'> /// Arguments to parse. /// </param> public static Option ParseArguments(string[] arguments) { Option options = new Option(); if (arguments == null || arguments.Length > 0) { IList<string> foundArguments = new List<string>(); for (int index = 0; index < arguments.Length; index++) { string argument = arguments[index]; if (foundArguments.Contains(argument)) { throw new NotSupportedException("Argument can not be specified more than once: " + argument); } foundArguments.Add(argument); if (argument.Equals(DebugArgument)) { SimpleLogger.AttachLogger(); continue; } if (argument.Equals(IgnoreCertsArgumentValue)) { options.IgnoreCerts = true; continue; } if (index + 1 >= arguments.Length) { throw new ArgumentException("Invalid argument parameter for " + argument); } index++; string parameter = arguments[index]; switch (argument) { // Set the output format case FormatArgumentValue: Format formatOut; if (!Enum.TryParse<Format>(parameter, true, out formatOut)) { throw new ArgumentException("Invalid format " + parameter); } options.Format = formatOut; break; // Set the input file case InputArgumentValue: options.InputFile = parameter; break; // Set the date period since to retrieve articles for case SinceArgumentValue: DateTime timeOut; if (!DateTime.TryParse(parameter, out timeOut)) { throw new ArgumentException("Invalid date/time " + parameter); } options.Since = timeOut; break; default: throw new NotSupportedException("Invalid argument"); } } if (Logger.DoLog) { foreach (var arg in arguments) { Logger.Write(arg); } } } return options; }