MessageFormatFactory.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Text;
  5. namespace Google.ProtocolBuffers.Serialization.Http
  6. {
  7. /// <summary>
  8. /// Extensions and helpers to abstract the reading/writing of messages by a client-specified content type.
  9. /// </summary>
  10. public static class MessageFormatFactory
  11. {
  12. /// <summary>
  13. /// Constructs an ICodedInputStream from the input stream based on the contentType provided
  14. /// </summary>
  15. /// <param name="options">Options specific to reading this message and/or content type</param>
  16. /// <param name="contentType">The mime type of the input stream content</param>
  17. /// <param name="input">The stream to read the message from</param>
  18. /// <returns>The ICodedInputStream that can be given to the IBuilder.MergeFrom(...) method</returns>
  19. public static ICodedInputStream CreateInputStream(MessageFormatOptions options, string contentType, Stream input)
  20. {
  21. ICodedInputStream codedInput = ContentTypeToInputStream(contentType, options, input);
  22. if (codedInput is XmlFormatReader)
  23. {
  24. XmlFormatReader reader = (XmlFormatReader)codedInput;
  25. reader.RootElementName = options.XmlReaderRootElementName;
  26. reader.Options = options.XmlReaderOptions;
  27. }
  28. return codedInput;
  29. }
  30. /// <summary>
  31. /// Writes the message instance to the stream using the content type provided
  32. /// </summary>
  33. /// <param name="options">Options specific to writing this message and/or content type</param>
  34. /// <param name="contentType">The mime type of the content to be written</param>
  35. /// <param name="output">The stream to write the message to</param>
  36. /// <remarks> If you do not dispose of ICodedOutputStream some formats may yield incomplete output </remarks>
  37. public static ICodedOutputStream CreateOutputStream(MessageFormatOptions options, string contentType, Stream output)
  38. {
  39. ICodedOutputStream codedOutput = ContentTypeToOutputStream(contentType, options, output);
  40. if (codedOutput is JsonFormatWriter)
  41. {
  42. JsonFormatWriter writer = (JsonFormatWriter)codedOutput;
  43. if (options.FormattedOutput)
  44. {
  45. writer.Formatted();
  46. }
  47. }
  48. else if (codedOutput is XmlFormatWriter)
  49. {
  50. XmlFormatWriter writer = (XmlFormatWriter)codedOutput;
  51. if (options.FormattedOutput)
  52. {
  53. XmlWriterSettings settings = new XmlWriterSettings()
  54. {
  55. CheckCharacters = false,
  56. NewLineHandling = NewLineHandling.Entitize,
  57. OmitXmlDeclaration = true,
  58. Encoding = new UTF8Encoding(false),
  59. Indent = true,
  60. IndentChars = " ",
  61. };
  62. // Don't know how else to change xml writer options?
  63. codedOutput = writer = XmlFormatWriter.CreateInstance(XmlWriter.Create(output, settings));
  64. }
  65. writer.RootElementName = options.XmlWriterRootElementName;
  66. writer.Options = options.XmlWriterOptions;
  67. }
  68. return codedOutput;
  69. }
  70. private static ICodedInputStream ContentTypeToInputStream(string contentType, MessageFormatOptions options, Stream input)
  71. {
  72. contentType = (contentType ?? String.Empty).Split(';')[0].Trim();
  73. CodedInputBuilder factory;
  74. if(!options.MimeInputTypesReadOnly.TryGetValue(contentType, out factory) || factory == null)
  75. {
  76. if(String.IsNullOrEmpty(options.DefaultContentType) ||
  77. !options.MimeInputTypesReadOnly.TryGetValue(options.DefaultContentType, out factory) || factory == null)
  78. {
  79. throw new ArgumentOutOfRangeException("contentType");
  80. }
  81. }
  82. return factory(input);
  83. }
  84. private static ICodedOutputStream ContentTypeToOutputStream(string contentType, MessageFormatOptions options, Stream output)
  85. {
  86. contentType = (contentType ?? String.Empty).Split(';')[0].Trim();
  87. CodedOutputBuilder factory;
  88. if (!options.MimeOutputTypesReadOnly.TryGetValue(contentType, out factory) || factory == null)
  89. {
  90. if (String.IsNullOrEmpty(options.DefaultContentType) ||
  91. !options.MimeOutputTypesReadOnly.TryGetValue(options.DefaultContentType, out factory) || factory == null)
  92. {
  93. throw new ArgumentOutOfRangeException("contentType");
  94. }
  95. }
  96. return factory(output);
  97. }
  98. }
  99. }