1eb462eb7f5320e283ef17f76dd644f34b8ea6b0
[reactos.git] / reactos / dll / win32 / xmllite / reader.c
1 /*
2 * IXmlReader implementation
3 *
4 * Copyright 2010 Nikolay Sivov
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define COBJMACROS
22
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "initguid.h"
27 #include "objbase.h"
28 #include "xmllite.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(xmllite);
33
34 /* not defined in public headers */
35 DEFINE_GUID(IID_IXmlReaderInput, 0x0b3ccc9b, 0x9214, 0x428b, 0xa2, 0xae, 0xef, 0x3a, 0xa8, 0x71, 0xaf, 0xda);
36
37 static HRESULT xmlreaderinput_query_for_stream(IXmlReaderInput *iface, void **pObj);
38
39 typedef struct _xmlreader
40 {
41 IXmlReader IXmlReader_iface;
42 LONG ref;
43 IXmlReaderInput *input;
44 ISequentialStream *stream;/* stored as sequential stream, cause currently
45 optimizations possible with IStream aren't implemented */
46 XmlReadState state;
47 UINT line, pos; /* reader position in XML stream */
48 } xmlreader;
49
50 typedef struct _xmlreaderinput
51 {
52 IXmlReaderInput IXmlReaderInput_iface;
53 LONG ref;
54 IUnknown *input; /* reference passed on IXmlReaderInput creation */
55 } xmlreaderinput;
56
57 static inline xmlreader *impl_from_IXmlReader(IXmlReader *iface)
58 {
59 return CONTAINING_RECORD(iface, xmlreader, IXmlReader_iface);
60 }
61
62 static inline xmlreaderinput *impl_from_IXmlReaderInput(IXmlReaderInput *iface)
63 {
64 return CONTAINING_RECORD(iface, xmlreaderinput, IXmlReaderInput_iface);
65 }
66
67 static HRESULT WINAPI xmlreader_QueryInterface(IXmlReader *iface, REFIID riid, void** ppvObject)
68 {
69 xmlreader *This = impl_from_IXmlReader(iface);
70
71 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
72
73 if (IsEqualGUID(riid, &IID_IUnknown) ||
74 IsEqualGUID(riid, &IID_IXmlReader))
75 {
76 *ppvObject = iface;
77 }
78 else
79 {
80 FIXME("interface %s not implemented\n", debugstr_guid(riid));
81 return E_NOINTERFACE;
82 }
83
84 IXmlReader_AddRef(iface);
85
86 return S_OK;
87 }
88
89 static ULONG WINAPI xmlreader_AddRef(IXmlReader *iface)
90 {
91 xmlreader *This = impl_from_IXmlReader(iface);
92 TRACE("%p\n", This);
93 return InterlockedIncrement(&This->ref);
94 }
95
96 static ULONG WINAPI xmlreader_Release(IXmlReader *iface)
97 {
98 xmlreader *This = impl_from_IXmlReader(iface);
99 LONG ref;
100
101 TRACE("%p\n", This);
102
103 ref = InterlockedDecrement(&This->ref);
104 if (ref == 0)
105 {
106 if (This->input) IUnknown_Release(This->input);
107 if (This->stream) IUnknown_Release(This->stream);
108 HeapFree(GetProcessHeap(), 0, This);
109 }
110
111 return ref;
112 }
113
114 static HRESULT WINAPI xmlreader_SetInput(IXmlReader* iface, IUnknown *input)
115 {
116 xmlreader *This = impl_from_IXmlReader(iface);
117 HRESULT hr;
118
119 TRACE("(%p %p)\n", This, input);
120
121 if (This->input)
122 {
123 IUnknown_Release(This->input);
124 This->input = NULL;
125 }
126
127 if (This->stream)
128 {
129 IUnknown_Release(This->stream);
130 This->stream = NULL;
131 }
132
133 This->line = This->pos = 0;
134
135 /* just reset current input */
136 if (!input)
137 {
138 This->state = XmlReadState_Initial;
139 return S_OK;
140 }
141
142 /* now try IXmlReaderInput, ISequentialStream, IStream */
143 hr = IUnknown_QueryInterface(input, &IID_IXmlReaderInput, (void**)&This->input);
144 if (hr != S_OK)
145 {
146 /* create IXmlReaderInput basing on supplied interface */
147 hr = CreateXmlReaderInputWithEncodingName(input,
148 NULL, NULL, FALSE, NULL, &This->input);
149 if (hr != S_OK) return hr;
150 }
151
152 /* set stream for supplied IXmlReaderInput */
153 hr = xmlreaderinput_query_for_stream(This->input, (void**)&This->stream);
154 if (hr == S_OK)
155 This->state = XmlReadState_Initial;
156
157 return hr;
158 }
159
160 static HRESULT WINAPI xmlreader_GetProperty(IXmlReader* iface, UINT property, LONG_PTR *value)
161 {
162 xmlreader *This = impl_from_IXmlReader(iface);
163
164 TRACE("(%p %u %p)\n", This, property, value);
165
166 if (!value) return E_INVALIDARG;
167
168 switch (property)
169 {
170 case XmlReaderProperty_ReadState:
171 *value = This->state;
172 break;
173 default:
174 FIXME("Unimplemented property (%u)\n", property);
175 return E_NOTIMPL;
176 }
177
178 return S_OK;
179 }
180
181 static HRESULT WINAPI xmlreader_SetProperty(IXmlReader* iface, UINT property, LONG_PTR value)
182 {
183 FIXME("(%p %u %lu): stub\n", iface, property, value);
184 return E_NOTIMPL;
185 }
186
187 static HRESULT WINAPI xmlreader_Read(IXmlReader* iface, XmlNodeType *node_type)
188 {
189 FIXME("(%p %p): stub\n", iface, node_type);
190 return E_NOTIMPL;
191 }
192
193 static HRESULT WINAPI xmlreader_GetNodeType(IXmlReader* iface, XmlNodeType *node_type)
194 {
195 FIXME("(%p %p): stub\n", iface, node_type);
196 return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI xmlreader_MoveToFirstAttribute(IXmlReader* iface)
200 {
201 FIXME("(%p): stub\n", iface);
202 return E_NOTIMPL;
203 }
204
205 static HRESULT WINAPI xmlreader_MoveToNextAttribute(IXmlReader* iface)
206 {
207 FIXME("(%p): stub\n", iface);
208 return E_NOTIMPL;
209 }
210
211 static HRESULT WINAPI xmlreader_MoveToAttributeByName(IXmlReader* iface,
212 LPCWSTR local_name,
213 LPCWSTR namespaceUri)
214 {
215 FIXME("(%p %p %p): stub\n", iface, local_name, namespaceUri);
216 return E_NOTIMPL;
217 }
218
219 static HRESULT WINAPI xmlreader_MoveToElement(IXmlReader* iface)
220 {
221 FIXME("(%p): stub\n", iface);
222 return E_NOTIMPL;
223 }
224
225 static HRESULT WINAPI xmlreader_GetQualifiedName(IXmlReader* iface, LPCWSTR *qualifiedName,
226 UINT *qualifiedName_length)
227 {
228 FIXME("(%p %p %p): stub\n", iface, qualifiedName, qualifiedName_length);
229 return E_NOTIMPL;
230 }
231
232 static HRESULT WINAPI xmlreader_GetNamespaceUri(IXmlReader* iface,
233 LPCWSTR *namespaceUri,
234 UINT *namespaceUri_length)
235 {
236 FIXME("(%p %p %p): stub\n", iface, namespaceUri, namespaceUri_length);
237 return E_NOTIMPL;
238 }
239
240 static HRESULT WINAPI xmlreader_GetLocalName(IXmlReader* iface,
241 LPCWSTR *local_name,
242 UINT *local_name_length)
243 {
244 FIXME("(%p %p %p): stub\n", iface, local_name, local_name_length);
245 return E_NOTIMPL;
246 }
247
248 static HRESULT WINAPI xmlreader_GetPrefix(IXmlReader* iface,
249 LPCWSTR *prefix,
250 UINT *prefix_length)
251 {
252 FIXME("(%p %p %p): stub\n", iface, prefix, prefix_length);
253 return E_NOTIMPL;
254 }
255
256 static HRESULT WINAPI xmlreader_GetValue(IXmlReader* iface,
257 LPCWSTR *value,
258 UINT *value_length)
259 {
260 FIXME("(%p %p %p): stub\n", iface, value, value_length);
261 return E_NOTIMPL;
262 }
263
264 static HRESULT WINAPI xmlreader_ReadValueChunk(IXmlReader* iface,
265 WCHAR *buffer,
266 UINT chunk_size,
267 UINT *read)
268 {
269 FIXME("(%p %p %u %p): stub\n", iface, buffer, chunk_size, read);
270 return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI xmlreader_GetBaseUri(IXmlReader* iface,
274 LPCWSTR *baseUri,
275 UINT *baseUri_length)
276 {
277 FIXME("(%p %p %p): stub\n", iface, baseUri, baseUri_length);
278 return E_NOTIMPL;
279 }
280
281 static BOOL WINAPI xmlreader_IsDefault(IXmlReader* iface)
282 {
283 FIXME("(%p): stub\n", iface);
284 return E_NOTIMPL;
285 }
286
287 static BOOL WINAPI xmlreader_IsEmptyElement(IXmlReader* iface)
288 {
289 FIXME("(%p): stub\n", iface);
290 return E_NOTIMPL;
291 }
292
293 static HRESULT WINAPI xmlreader_GetLineNumber(IXmlReader* iface, UINT *lineNumber)
294 {
295 xmlreader *This = impl_from_IXmlReader(iface);
296
297 TRACE("(%p %p)\n", This, lineNumber);
298
299 if (!lineNumber) return E_INVALIDARG;
300
301 *lineNumber = This->line;
302
303 return S_OK;
304 }
305
306 static HRESULT WINAPI xmlreader_GetLinePosition(IXmlReader* iface, UINT *linePosition)
307 {
308 xmlreader *This = impl_from_IXmlReader(iface);
309
310 TRACE("(%p %p)\n", This, linePosition);
311
312 if (!linePosition) return E_INVALIDARG;
313
314 *linePosition = This->pos;
315
316 return S_OK;
317 }
318
319 static HRESULT WINAPI xmlreader_GetAttributeCount(IXmlReader* iface, UINT *attributeCount)
320 {
321 FIXME("(%p %p): stub\n", iface, attributeCount);
322 return E_NOTIMPL;
323 }
324
325 static HRESULT WINAPI xmlreader_GetDepth(IXmlReader* iface, UINT *depth)
326 {
327 FIXME("(%p %p): stub\n", iface, depth);
328 return E_NOTIMPL;
329 }
330
331 static BOOL WINAPI xmlreader_IsEOF(IXmlReader* iface)
332 {
333 FIXME("(%p): stub\n", iface);
334 return E_NOTIMPL;
335 }
336
337 static const struct IXmlReaderVtbl xmlreader_vtbl =
338 {
339 xmlreader_QueryInterface,
340 xmlreader_AddRef,
341 xmlreader_Release,
342 xmlreader_SetInput,
343 xmlreader_GetProperty,
344 xmlreader_SetProperty,
345 xmlreader_Read,
346 xmlreader_GetNodeType,
347 xmlreader_MoveToFirstAttribute,
348 xmlreader_MoveToNextAttribute,
349 xmlreader_MoveToAttributeByName,
350 xmlreader_MoveToElement,
351 xmlreader_GetQualifiedName,
352 xmlreader_GetNamespaceUri,
353 xmlreader_GetLocalName,
354 xmlreader_GetPrefix,
355 xmlreader_GetValue,
356 xmlreader_ReadValueChunk,
357 xmlreader_GetBaseUri,
358 xmlreader_IsDefault,
359 xmlreader_IsEmptyElement,
360 xmlreader_GetLineNumber,
361 xmlreader_GetLinePosition,
362 xmlreader_GetAttributeCount,
363 xmlreader_GetDepth,
364 xmlreader_IsEOF
365 };
366
367 /** IXmlReaderInput **/
368
369 /* Queries already stored interface for IStream/ISequentialStream.
370 Interface supplied on creation will be overwritten */
371 static HRESULT xmlreaderinput_query_for_stream(IXmlReaderInput *iface, void **pObj)
372 {
373 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
374 HRESULT hr;
375
376 hr = IUnknown_QueryInterface(This->input, &IID_IStream, pObj);
377 if (hr != S_OK)
378 hr = IUnknown_QueryInterface(This->input, &IID_ISequentialStream, pObj);
379
380 return hr;
381 }
382
383 static HRESULT WINAPI xmlreaderinput_QueryInterface(IXmlReaderInput *iface, REFIID riid, void** ppvObject)
384 {
385 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
386
387 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
388
389 if (IsEqualGUID(riid, &IID_IXmlReaderInput) ||
390 IsEqualGUID(riid, &IID_IUnknown))
391 {
392 *ppvObject = iface;
393 }
394 else
395 {
396 FIXME("interface %s not implemented\n", debugstr_guid(riid));
397 return E_NOINTERFACE;
398 }
399
400 IUnknown_AddRef(iface);
401
402 return S_OK;
403 }
404
405 static ULONG WINAPI xmlreaderinput_AddRef(IXmlReaderInput *iface)
406 {
407 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
408 TRACE("%p\n", This);
409 return InterlockedIncrement(&This->ref);
410 }
411
412 static ULONG WINAPI xmlreaderinput_Release(IXmlReaderInput *iface)
413 {
414 xmlreaderinput *This = impl_from_IXmlReaderInput(iface);
415 LONG ref;
416
417 TRACE("%p\n", This);
418
419 ref = InterlockedDecrement(&This->ref);
420 if (ref == 0)
421 {
422 if (This->input) IUnknown_Release(This->input);
423 HeapFree(GetProcessHeap(), 0, This);
424 }
425
426 return ref;
427 }
428
429 static const struct IUnknownVtbl xmlreaderinput_vtbl =
430 {
431 xmlreaderinput_QueryInterface,
432 xmlreaderinput_AddRef,
433 xmlreaderinput_Release
434 };
435
436 HRESULT WINAPI CreateXmlReader(REFIID riid, void **pObject, IMalloc *pMalloc)
437 {
438 xmlreader *reader;
439
440 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid), pObject, pMalloc);
441
442 if (pMalloc) FIXME("custom IMalloc not supported yet\n");
443
444 if (!IsEqualGUID(riid, &IID_IXmlReader))
445 {
446 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid));
447 return E_FAIL;
448 }
449
450 reader = HeapAlloc(GetProcessHeap(), 0, sizeof (*reader));
451 if(!reader) return E_OUTOFMEMORY;
452
453 reader->IXmlReader_iface.lpVtbl = &xmlreader_vtbl;
454 reader->ref = 1;
455 reader->stream = NULL;
456 reader->input = NULL;
457 reader->state = XmlReadState_Closed;
458 reader->line = reader->pos = 0;
459
460 *pObject = &reader->IXmlReader_iface;
461
462 TRACE("returning iface %p\n", *pObject);
463
464 return S_OK;
465 }
466
467 HRESULT WINAPI CreateXmlReaderInputWithEncodingName(IUnknown *stream,
468 IMalloc *pMalloc,
469 LPCWSTR encoding,
470 BOOL hint,
471 LPCWSTR base_uri,
472 IXmlReaderInput **ppInput)
473 {
474 xmlreaderinput *readerinput;
475
476 FIXME("%p %p %s %d %s %p: stub\n", stream, pMalloc, wine_dbgstr_w(encoding),
477 hint, wine_dbgstr_w(base_uri), ppInput);
478
479 if (!stream || !ppInput) return E_INVALIDARG;
480
481 readerinput = HeapAlloc(GetProcessHeap(), 0, sizeof (*readerinput));
482 if(!readerinput) return E_OUTOFMEMORY;
483
484 readerinput->IXmlReaderInput_iface.lpVtbl = &xmlreaderinput_vtbl;
485 readerinput->ref = 1;
486 IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&readerinput->input);
487
488 *ppInput = &readerinput->IXmlReaderInput_iface;
489
490 TRACE("returning iface %p\n", *ppInput);
491
492 return S_OK;
493 }