comdlg32: Correct title of "Save As" dialog.
[wine] / dlls / msxml3 / stylesheet.c
1 /*
2  *    XSLTemplate/XSLProcessor support
3  *
4  * Copyright 2011 Nikolay Sivov for CodeWeavers
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 "config.h"
24
25 #include <stdarg.h>
26 #ifdef HAVE_LIBXML2
27 # include <libxml/parser.h>
28 # include <libxml/xmlerror.h>
29 #endif
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "ole2.h"
35 #include "msxml6.h"
36
37 #include "msxml_private.h"
38
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42
43 #ifdef HAVE_LIBXML2
44
45 typedef struct _xsltemplate
46 {
47     DispatchEx dispex;
48     IXSLTemplate IXSLTemplate_iface;
49     LONG ref;
50
51     IXMLDOMNode *node;
52 } xsltemplate;
53
54 typedef struct _xslprocessor
55 {
56     DispatchEx dispex;
57     IXSLProcessor IXSLProcessor_iface;
58     LONG ref;
59
60     xsltemplate *stylesheet;
61     IXMLDOMNode *input;
62
63     IStream     *output;
64     BSTR         outstr;
65 } xslprocessor;
66
67 static HRESULT XSLProcessor_create(xsltemplate*, IXSLProcessor**);
68
69 static inline xsltemplate *impl_from_IXSLTemplate( IXSLTemplate *iface )
70 {
71     return CONTAINING_RECORD(iface, xsltemplate, IXSLTemplate_iface);
72 }
73
74 static inline xslprocessor *impl_from_IXSLProcessor( IXSLProcessor *iface )
75 {
76     return CONTAINING_RECORD(iface, xslprocessor, IXSLProcessor_iface);
77 }
78
79 static void xsltemplate_set_node( xsltemplate *This, IXMLDOMNode *node )
80 {
81     if (This->node) IXMLDOMNode_Release(This->node);
82     This->node = node;
83     if (node) IXMLDOMNode_AddRef(node);
84 }
85
86 static HRESULT WINAPI xsltemplate_QueryInterface(
87     IXSLTemplate *iface,
88     REFIID riid,
89     void** ppvObject )
90 {
91     xsltemplate *This = impl_from_IXSLTemplate( iface );
92     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
93
94     if ( IsEqualGUID( riid, &IID_IXSLTemplate ) ||
95          IsEqualGUID( riid, &IID_IDispatch ) ||
96          IsEqualGUID( riid, &IID_IUnknown ) )
97     {
98         *ppvObject = iface;
99     }
100     else if (dispex_query_interface(&This->dispex, riid, ppvObject))
101     {
102         return *ppvObject ? S_OK : E_NOINTERFACE;
103     }
104     else
105     {
106         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
107         *ppvObject = NULL;
108         return E_NOINTERFACE;
109     }
110
111     IUnknown_AddRef((IUnknown*)*ppvObject);
112     return S_OK;
113 }
114
115 static ULONG WINAPI xsltemplate_AddRef( IXSLTemplate *iface )
116 {
117     xsltemplate *This = impl_from_IXSLTemplate( iface );
118     return InterlockedIncrement( &This->ref );
119 }
120
121 static ULONG WINAPI xsltemplate_Release( IXSLTemplate *iface )
122 {
123     xsltemplate *This = impl_from_IXSLTemplate( iface );
124     ULONG ref;
125
126     ref = InterlockedDecrement( &This->ref );
127     if ( ref == 0 )
128     {
129         if (This->node) IXMLDOMNode_Release( This->node );
130         release_dispex(&This->dispex);
131         heap_free( This );
132     }
133
134     return ref;
135 }
136
137 static HRESULT WINAPI xsltemplate_GetTypeInfoCount( IXSLTemplate *iface, UINT* pctinfo )
138 {
139     xsltemplate *This = impl_from_IXSLTemplate( iface );
140     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
141 }
142
143 static HRESULT WINAPI xsltemplate_GetTypeInfo(
144     IXSLTemplate *iface,
145     UINT iTInfo, LCID lcid,
146     ITypeInfo** ppTInfo )
147 {
148     xsltemplate *This = impl_from_IXSLTemplate( iface );
149     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
150         iTInfo, lcid, ppTInfo);
151 }
152
153 static HRESULT WINAPI xsltemplate_GetIDsOfNames(
154     IXSLTemplate *iface,
155     REFIID riid, LPOLESTR* rgszNames,
156     UINT cNames, LCID lcid, DISPID* rgDispId )
157 {
158     xsltemplate *This = impl_from_IXSLTemplate( iface );
159     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
160         riid, rgszNames, cNames, lcid, rgDispId);
161 }
162
163 static HRESULT WINAPI xsltemplate_Invoke(
164     IXSLTemplate *iface,
165     DISPID dispIdMember, REFIID riid, LCID lcid,
166     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
167     EXCEPINFO* pExcepInfo, UINT* puArgErr )
168 {
169     xsltemplate *This = impl_from_IXSLTemplate( iface );
170     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
171         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
172 }
173
174 static HRESULT WINAPI xsltemplate_putref_stylesheet( IXSLTemplate *iface,
175     IXMLDOMNode *node)
176 {
177     xsltemplate *This = impl_from_IXSLTemplate( iface );
178
179     TRACE("(%p)->(%p)\n", This, node);
180
181     if (!node)
182     {
183         xsltemplate_set_node(This, NULL);
184         return S_OK;
185     }
186
187     /* FIXME: test for document type */
188     xsltemplate_set_node(This, node);
189
190     return S_OK;
191 }
192
193 static HRESULT WINAPI xsltemplate_get_stylesheet( IXSLTemplate *iface,
194     IXMLDOMNode **node)
195 {
196     xsltemplate *This = impl_from_IXSLTemplate( iface );
197
198     FIXME("(%p)->(%p): stub\n", This, node);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI xsltemplate_createProcessor( IXSLTemplate *iface,
203     IXSLProcessor **processor)
204 {
205     xsltemplate *This = impl_from_IXSLTemplate( iface );
206
207     TRACE("(%p)->(%p)\n", This, processor);
208
209     if (!processor) return E_INVALIDARG;
210
211     return XSLProcessor_create(This, processor);
212 }
213
214 static const struct IXSLTemplateVtbl XSLTemplateVtbl =
215 {
216     xsltemplate_QueryInterface,
217     xsltemplate_AddRef,
218     xsltemplate_Release,
219     xsltemplate_GetTypeInfoCount,
220     xsltemplate_GetTypeInfo,
221     xsltemplate_GetIDsOfNames,
222     xsltemplate_Invoke,
223
224     xsltemplate_putref_stylesheet,
225     xsltemplate_get_stylesheet,
226     xsltemplate_createProcessor
227 };
228
229 static const tid_t xsltemplate_iface_tids[] = {
230     IXSLTemplate_tid,
231     0
232 };
233
234 static dispex_static_data_t xsltemplate_dispex = {
235     NULL,
236     IXSLTemplate_tid,
237     NULL,
238     xsltemplate_iface_tids
239 };
240
241 HRESULT XSLTemplate_create(IUnknown *outer, void **ppObj)
242 {
243     xsltemplate *This;
244
245     TRACE("(%p, %p)\n", outer, ppObj);
246
247     if(outer) FIXME("support aggregation, outer\n");
248
249     This = heap_alloc( sizeof (*This) );
250     if(!This)
251         return E_OUTOFMEMORY;
252
253     This->IXSLTemplate_iface.lpVtbl = &XSLTemplateVtbl;
254     This->ref = 1;
255     This->node = NULL;
256     init_dispex(&This->dispex, (IUnknown*)&This->IXSLTemplate_iface, &xsltemplate_dispex);
257
258     *ppObj = &This->IXSLTemplate_iface;
259
260     TRACE("returning iface %p\n", *ppObj);
261
262     return S_OK;
263 }
264
265 /*** IXSLProcessor ***/
266 static HRESULT WINAPI xslprocessor_QueryInterface(
267     IXSLProcessor *iface,
268     REFIID riid,
269     void** ppvObject )
270 {
271     xslprocessor *This = impl_from_IXSLProcessor( iface );
272     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
273
274     if ( IsEqualGUID( riid, &IID_IXSLProcessor ) ||
275          IsEqualGUID( riid, &IID_IDispatch ) ||
276          IsEqualGUID( riid, &IID_IUnknown ) )
277     {
278         *ppvObject = iface;
279     }
280     else if (dispex_query_interface(&This->dispex, riid, ppvObject))
281     {
282         return *ppvObject ? S_OK : E_NOINTERFACE;
283     }
284     else
285     {
286         FIXME("Unsupported interface %s\n", debugstr_guid(riid));
287         *ppvObject = NULL;
288         return E_NOINTERFACE;
289     }
290
291     IUnknown_AddRef((IUnknown*)*ppvObject);
292     return S_OK;
293 }
294
295 static ULONG WINAPI xslprocessor_AddRef( IXSLProcessor *iface )
296 {
297     xslprocessor *This = impl_from_IXSLProcessor( iface );
298     return InterlockedIncrement( &This->ref );
299 }
300
301 static ULONG WINAPI xslprocessor_Release( IXSLProcessor *iface )
302 {
303     xslprocessor *This = impl_from_IXSLProcessor( iface );
304     ULONG ref;
305
306     ref = InterlockedDecrement( &This->ref );
307     if ( ref == 0 )
308     {
309         if (This->input) IXMLDOMNode_Release(This->input);
310         if (This->output) IStream_Release(This->output);
311         SysFreeString(This->outstr);
312         IXSLTemplate_Release(&This->stylesheet->IXSLTemplate_iface);
313         release_dispex(&This->dispex);
314         heap_free( This );
315     }
316
317     return ref;
318 }
319
320 static HRESULT WINAPI xslprocessor_GetTypeInfoCount( IXSLProcessor *iface, UINT* pctinfo )
321 {
322     xslprocessor *This = impl_from_IXSLProcessor( iface );
323     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
324 }
325
326 static HRESULT WINAPI xslprocessor_GetTypeInfo(
327     IXSLProcessor *iface,
328     UINT iTInfo, LCID lcid,
329     ITypeInfo** ppTInfo )
330 {
331     xslprocessor *This = impl_from_IXSLProcessor( iface );
332     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface,
333         iTInfo, lcid, ppTInfo);
334 }
335
336 static HRESULT WINAPI xslprocessor_GetIDsOfNames(
337     IXSLProcessor *iface,
338     REFIID riid, LPOLESTR* rgszNames,
339     UINT cNames, LCID lcid, DISPID* rgDispId )
340 {
341     xslprocessor *This = impl_from_IXSLProcessor( iface );
342     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface,
343         riid, rgszNames, cNames, lcid, rgDispId);
344 }
345
346 static HRESULT WINAPI xslprocessor_Invoke(
347     IXSLProcessor *iface,
348     DISPID dispIdMember, REFIID riid, LCID lcid,
349     WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult,
350     EXCEPINFO* pExcepInfo, UINT* puArgErr )
351 {
352     xslprocessor *This = impl_from_IXSLProcessor( iface );
353     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface,
354         dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
355 }
356
357 static HRESULT WINAPI xslprocessor_put_input( IXSLProcessor *iface, VARIANT input )
358 {
359     xslprocessor *This = impl_from_IXSLProcessor( iface );
360     IXMLDOMNode *input_node;
361     HRESULT hr;
362
363     TRACE("(%p)->(%s)\n", This, debugstr_variant(&input));
364
365     /* try IXMLDOMNode directly first */
366     if (V_VT(&input) == VT_UNKNOWN)
367         hr = IUnknown_QueryInterface(V_UNKNOWN(&input), &IID_IXMLDOMNode, (void**)&input_node);
368     else if (V_VT(&input) == VT_DISPATCH)
369         hr = IDispatch_QueryInterface(V_DISPATCH(&input), &IID_IXMLDOMNode, (void**)&input_node);
370     else
371     {
372         IXMLDOMDocument *doc;
373
374         hr = DOMDocument_create(MSXML_DEFAULT, NULL, (void**)&doc);
375         if (hr == S_OK)
376         {
377             VARIANT_BOOL b;
378
379             hr = IXMLDOMDocument_load(doc, input, &b);
380             if (hr == S_OK)
381                 hr = IXMLDOMDocument_QueryInterface(doc, &IID_IXMLDOMNode, (void**)&input_node);
382             IXMLDOMDocument_Release(doc);
383         }
384     }
385
386     if (hr == S_OK)
387     {
388         if (This->input) IXMLDOMNode_Release(This->input);
389         This->input = input_node;
390     }
391
392     return hr;
393 }
394
395 static HRESULT WINAPI xslprocessor_get_input( IXSLProcessor *iface, VARIANT *input )
396 {
397     xslprocessor *This = impl_from_IXSLProcessor( iface );
398
399     FIXME("(%p)->(%p): stub\n", This, input);
400     return E_NOTIMPL;
401 }
402
403 static HRESULT WINAPI xslprocessor_get_ownerTemplate(
404     IXSLProcessor *iface,
405     IXSLTemplate **template)
406 {
407     xslprocessor *This = impl_from_IXSLProcessor( iface );
408
409     FIXME("(%p)->(%p): stub\n", This, template);
410     return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI xslprocessor_setStartMode(
414     IXSLProcessor *iface,
415     BSTR p,
416     BSTR uri)
417 {
418     xslprocessor *This = impl_from_IXSLProcessor( iface );
419
420     FIXME("(%p)->(%s %s): stub\n", This, wine_dbgstr_w(p), wine_dbgstr_w(uri));
421     return E_NOTIMPL;
422 }
423
424 static HRESULT WINAPI xslprocessor_get_startMode(
425     IXSLProcessor *iface,
426     BSTR *p)
427 {
428     xslprocessor *This = impl_from_IXSLProcessor( iface );
429
430     FIXME("(%p)->(%p): stub\n", This, p);
431     return E_NOTIMPL;
432 }
433
434 static HRESULT WINAPI xslprocessor_get_startModeURI(
435     IXSLProcessor *iface,
436     BSTR *uri)
437 {
438     xslprocessor *This = impl_from_IXSLProcessor( iface );
439
440     FIXME("(%p)->(%p): stub\n", This, uri);
441     return E_NOTIMPL;
442 }
443
444 static HRESULT WINAPI xslprocessor_put_output(
445     IXSLProcessor *iface,
446     VARIANT output)
447 {
448     xslprocessor *This = impl_from_IXSLProcessor( iface );
449     IStream *stream;
450     HRESULT hr;
451
452     FIXME("(%p)->(%s): semi-stub\n", This, debugstr_variant(&output));
453
454     switch (V_VT(&output))
455     {
456       case VT_EMPTY:
457         stream = NULL;
458         hr = S_OK;
459         break;
460       case VT_UNKNOWN:
461         hr = IUnknown_QueryInterface(V_UNKNOWN(&output), &IID_IStream, (void**)&stream);
462         break;
463       default:
464         hr = E_FAIL;
465     }
466
467     if (hr == S_OK)
468     {
469         if (This->output) IStream_Release(This->output);
470         This->output = stream;
471     }
472
473     return hr;
474 }
475
476 static HRESULT WINAPI xslprocessor_get_output(
477     IXSLProcessor *iface,
478     VARIANT *output)
479 {
480     xslprocessor *This = impl_from_IXSLProcessor( iface );
481
482     TRACE("(%p)->(%p)\n", This, output);
483
484     if (!output) return E_INVALIDARG;
485
486     if (This->output)
487     {
488         V_VT(output) = VT_UNKNOWN;
489         V_UNKNOWN(output) = (IUnknown*)This->output;
490         IStream_AddRef(This->output);
491     }
492     else if (This->outstr)
493     {
494         V_VT(output) = VT_BSTR;
495         V_BSTR(output) = SysAllocString(This->outstr);
496     }
497     else
498         V_VT(output) = VT_EMPTY;
499
500     return S_OK;
501 }
502
503 static HRESULT WINAPI xslprocessor_transform(
504     IXSLProcessor *iface,
505     VARIANT_BOOL  *ret)
506 {
507     xslprocessor *This = impl_from_IXSLProcessor( iface );
508     HRESULT hr;
509
510     TRACE("(%p)->(%p)\n", This, ret);
511
512     if (!ret) return E_INVALIDARG;
513
514     SysFreeString(This->outstr);
515     hr = IXMLDOMNode_transformNode(This->input, This->stylesheet->node, &This->outstr);
516     if (hr == S_OK)
517     {
518         if (This->output)
519         {
520             ULONG len = 0;
521
522             /* output to stream */
523             hr = IStream_Write(This->output, This->outstr, SysStringByteLen(This->outstr), &len);
524             *ret = len == SysStringByteLen(This->outstr) ? VARIANT_TRUE : VARIANT_FALSE;
525         }
526     }
527     else
528         *ret = VARIANT_FALSE;
529
530     return hr;
531 }
532
533 static HRESULT WINAPI xslprocessor_reset( IXSLProcessor *iface )
534 {
535     xslprocessor *This = impl_from_IXSLProcessor( iface );
536
537     FIXME("(%p): stub\n", This);
538     return E_NOTIMPL;
539 }
540
541 static HRESULT WINAPI xslprocessor_get_readyState(
542     IXSLProcessor *iface,
543     LONG *state)
544 {
545     xslprocessor *This = impl_from_IXSLProcessor( iface );
546
547     FIXME("(%p)->(%p): stub\n", This, state);
548     return E_NOTIMPL;
549 }
550
551 static HRESULT WINAPI xslprocessor_addParameter(
552     IXSLProcessor *iface,
553     BSTR p,
554     VARIANT var,
555     BSTR uri)
556 {
557     xslprocessor *This = impl_from_IXSLProcessor( iface );
558
559     FIXME("(%p)->(%s %s %s): stub\n", This, wine_dbgstr_w(p), debugstr_variant(&var),
560         wine_dbgstr_w(uri));
561     return E_NOTIMPL;
562 }
563
564 static HRESULT WINAPI xslprocessor_addObject(
565     IXSLProcessor *iface,
566     IDispatch *obj,
567     BSTR uri)
568 {
569     xslprocessor *This = impl_from_IXSLProcessor( iface );
570
571     FIXME("(%p)->(%p %s): stub\n", This, obj, wine_dbgstr_w(uri));
572     return E_NOTIMPL;
573 }
574
575 static HRESULT WINAPI xslprocessor_get_stylesheet(
576     IXSLProcessor *iface,
577     IXMLDOMNode  **node)
578 {
579     xslprocessor *This = impl_from_IXSLProcessor( iface );
580
581     FIXME("(%p)->(%p): stub\n", This, node);
582     return E_NOTIMPL;
583 }
584
585 static const struct IXSLProcessorVtbl XSLProcessorVtbl =
586 {
587     xslprocessor_QueryInterface,
588     xslprocessor_AddRef,
589     xslprocessor_Release,
590     xslprocessor_GetTypeInfoCount,
591     xslprocessor_GetTypeInfo,
592     xslprocessor_GetIDsOfNames,
593     xslprocessor_Invoke,
594
595     xslprocessor_put_input,
596     xslprocessor_get_input,
597     xslprocessor_get_ownerTemplate,
598     xslprocessor_setStartMode,
599     xslprocessor_get_startMode,
600     xslprocessor_get_startModeURI,
601     xslprocessor_put_output,
602     xslprocessor_get_output,
603     xslprocessor_transform,
604     xslprocessor_reset,
605     xslprocessor_get_readyState,
606     xslprocessor_addParameter,
607     xslprocessor_addObject,
608     xslprocessor_get_stylesheet
609 };
610
611 static const tid_t xslprocessor_iface_tids[] = {
612     IXSLProcessor_tid,
613     0
614 };
615
616 static dispex_static_data_t xslprocessor_dispex = {
617     NULL,
618     IXSLProcessor_tid,
619     NULL,
620     xslprocessor_iface_tids
621 };
622
623 HRESULT XSLProcessor_create(xsltemplate *template, IXSLProcessor **ppObj)
624 {
625     xslprocessor *This;
626
627     TRACE("(%p)\n", ppObj);
628
629     This = heap_alloc( sizeof (*This) );
630     if(!This)
631         return E_OUTOFMEMORY;
632
633     This->IXSLProcessor_iface.lpVtbl = &XSLProcessorVtbl;
634     This->ref = 1;
635     This->input = NULL;
636     This->output = NULL;
637     This->outstr = NULL;
638     This->stylesheet = template;
639     IXSLTemplate_AddRef(&template->IXSLTemplate_iface);
640     init_dispex(&This->dispex, (IUnknown*)&This->IXSLProcessor_iface, &xslprocessor_dispex);
641
642     *ppObj = &This->IXSLProcessor_iface;
643
644     TRACE("returning iface %p\n", *ppObj);
645
646     return S_OK;
647 }
648
649 #else
650
651 HRESULT XSLTemplate_create(IUnknown *pUnkOuter, void **ppObj)
652 {
653     MESSAGE("This program tried to use a XSLTemplate object, but\n"
654             "libxml2 support was not present at compile time.\n");
655     return E_NOTIMPL;
656 }
657
658 #endif