summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/Messaging/DictionaryXmlWriter.cs
blob: 3be043f558b8884421774e790fd60863b89926ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//-----------------------------------------------------------------------
// <copyright file="DictionaryXmlWriter.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOAuth.Messaging {
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Xml;

	/// <summary>
	/// An XmlWriter-looking object that actually saves data to a dictionary.
	/// </summary>
	internal class DictionaryXmlWriter {
		/// <summary>
		/// Creates an <see cref="XmlWriter"/> that actually writes to an IDictionary&lt;string, string&gt; instance.
		/// </summary>
		/// <param name="dictionary">The dictionary to save the written XML to.</param>
		/// <returns>The XmlWriter that will save data to the given dictionary.</returns>
		internal static XmlWriter Create(IDictionary<string, string> dictionary) {
			return new PseudoXmlWriter(dictionary);
		}

		/// <summary>
		/// Writes out a dictionary as if it were XML.
		/// </summary>
		private class PseudoXmlWriter : XmlWriter {
			/// <summary>
			/// The dictionary to write values to.
			/// </summary>
			private IDictionary<string, string> dictionary;

			/// <summary>
			/// The key being written at the moment.
			/// </summary>
			private string key;

			/// <summary>
			/// The value being written out at the moment.
			/// </summary>
			private StringBuilder value = new StringBuilder();

			/// <summary>
			/// Initializes a new instance of the <see cref="PseudoXmlWriter"/> class.
			/// </summary>
			/// <param name="dictionary">The dictionary that will be written to.</param>
			internal PseudoXmlWriter(IDictionary<string, string> dictionary) {
				if (dictionary == null) {
					throw new ArgumentNullException("dictionary");
				}

				this.dictionary = dictionary;
			}

			/// <summary>
			/// Gets the spoofed state of the <see cref="XmlWriter"/>.
			/// </summary>
			public override WriteState WriteState {
				get { return WriteState.Element; }
			}

			/// <summary>
			/// Prepares to write out a new key/value pair with the given key name to the dictionary.
			/// </summary>
			/// <param name="prefix">This parameter is ignored.</param>
			/// <param name="localName">The key to store in the dictionary.</param>
			/// <param name="ns">This parameter is ignored.</param>
			public override void WriteStartElement(string prefix, string localName, string ns) {
				this.key = localName;
				this.value.Length = 0;
			}

			/// <summary>
			/// Appends some text to the value that is to be stored in the dictionary.
			/// </summary>
			/// <param name="text">The text to append to the value.</param>
			public override void WriteString(string text) {
				if (!string.IsNullOrEmpty(this.key)) {
					this.value.Append(text);
				}
			}

			/// <summary>
			/// Writes out a completed key/value to the dictionary.
			/// </summary>
			public override void WriteEndElement() {
				if (this.key != null) {
					this.dictionary[this.key] = this.value.ToString();
					this.key = null;
					this.value.Length = 0;
				}
			}

			/// <summary>
			/// Clears the internal key/value building state.
			/// </summary>
			/// <param name="prefix">This parameter is ignored.</param>
			/// <param name="localName">This parameter is ignored.</param>
			/// <param name="ns">This parameter is ignored.</param>
			public override void WriteStartAttribute(string prefix, string localName, string ns) {
				this.key = null;
			}

			/// <summary>
			/// This method does not do anything.
			/// </summary>
			public override void WriteEndAttribute() { }

			/// <summary>
			/// This method does not do anything.
			/// </summary>
			public override void Close() { }

			#region Unimplemented methods

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			public override void Flush() {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="ns">This parameter is ignored.</param>
			/// <returns>None, since an exception is always thrown.</returns>
			public override string LookupPrefix(string ns) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="buffer">This parameter is ignored.</param>
			/// <param name="index">This parameter is ignored.</param>
			/// <param name="count">This parameter is ignored.</param>
			public override void WriteBase64(byte[] buffer, int index, int count) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="text">This parameter is ignored.</param>
			public override void WriteCData(string text) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="ch">This parameter is ignored.</param>
			public override void WriteCharEntity(char ch) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="buffer">This parameter is ignored.</param>
			/// <param name="index">This parameter is ignored.</param>
			/// <param name="count">This parameter is ignored.</param>
			public override void WriteChars(char[] buffer, int index, int count) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="text">This parameter is ignored.</param>
			public override void WriteComment(string text) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="name">This parameter is ignored.</param>
			/// <param name="pubid">This parameter is ignored.</param>
			/// <param name="sysid">This parameter is ignored.</param>
			/// <param name="subset">This parameter is ignored.</param>
			public override void WriteDocType(string name, string pubid, string sysid, string subset) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			public override void WriteEndDocument() {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="name">This parameter is ignored.</param>
			public override void WriteEntityRef(string name) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			public override void WriteFullEndElement() {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="name">This parameter is ignored.</param>
			/// <param name="text">This parameter is ignored.</param>
			public override void WriteProcessingInstruction(string name, string text) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="data">This parameter is ignored.</param>
			public override void WriteRaw(string data) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="buffer">This parameter is ignored.</param>
			/// <param name="index">This parameter is ignored.</param>
			/// <param name="count">This parameter is ignored.</param>
			public override void WriteRaw(char[] buffer, int index, int count) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="standalone">This parameter is ignored.</param>
			public override void WriteStartDocument(bool standalone) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			public override void WriteStartDocument() {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="lowChar">This parameter is ignored.</param>
			/// <param name="highChar">This parameter is ignored.</param>
			public override void WriteSurrogateCharEntity(char lowChar, char highChar) {
				throw new NotImplementedException();
			}

			/// <summary>
			/// Throws <see cref="NotImplementedException"/>.
			/// </summary>
			/// <param name="ws">This parameter is ignored.</param>
			public override void WriteWhitespace(string ws) {
				throw new NotImplementedException();
			}

			#endregion
		}
	}
}