41f528025c1047c053d23e236928be080d7af1fb
[tpot/pegasus/.git] / src / Pegasus / Common / XmlWriter.h
1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 // 
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Mike Brasher (mbrasher@bmc.com)
33 //
34 // Modified By:
35 //         Nitin Upasani, Hewlett-Packard Company (Nitin_Upasani@hp.com)
36 //         Nag Boranna, Hewlett-Packard Company (nagaraja_boranna@hp.com)
37 //         Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
38 //         Carol Ann Krug Graves, Hewlett-Packard Company
39 //             (carolann_graves@hp.com)
40 //         Brian G. Campbell, EMC (campbell_brian@emc.com) - PEP140/phase1
41 //         Willis White (whiwill@us.ibm.com) PEP 127 and 128
42 //         Brian G. Campbell, EMC (campbell_brian@emc.com) - PEP140/phase2
43 //         David Dillard, Symantec Corp. (david_dillard@symantec.com)
44 //              Vijay Eli, vijayeli@in.ibm.com, fix for #2571
45 //
46 //%/////////////////////////////////////////////////////////////////////////////
47
48 #ifndef Pegasus_XmlWriter_h
49 #define Pegasus_XmlWriter_h
50
51 #include <iostream>
52 #include <Pegasus/Common/Config.h>
53 #include <Pegasus/Common/InternalException.h>
54 #include <Pegasus/Common/ArrayInternal.h>
55 #include <Pegasus/Common/String.h>
56 #include <Pegasus/Common/Indentor.h>
57 #include <Pegasus/Common/CIMObject.h>
58 #include <Pegasus/Common/CIMClass.h>
59 #include <Pegasus/Common/CIMInstance.h>
60 #include <Pegasus/Common/CIMProperty.h>
61 #include <Pegasus/Common/CIMMethod.h>
62 #include <Pegasus/Common/CIMParameter.h>
63 #include <Pegasus/Common/CIMQualifier.h>
64 #include <Pegasus/Common/CIMQualifierDecl.h>
65 #include <Pegasus/Common/CIMValue.h>
66 #include <Pegasus/Common/CIMObjectPath.h>
67 #include <Pegasus/Common/CIMPropertyList.h>
68 #include <Pegasus/Common/CIMParamValue.h>
69 #include <Pegasus/Common/Message.h>
70 #include <Pegasus/Common/Linkage.h>
71 #include <Pegasus/Common/ContentLanguageList.h>
72 #include <Pegasus/Common/AcceptLanguageList.h>
73 #include <Pegasus/Common/Buffer.h>
74 #include <Pegasus/Common/StrLit.h>
75
76 PEGASUS_NAMESPACE_BEGIN
77
78 // l10n - added accept language and content language support below
79
80 class PEGASUS_COMMON_LINKAGE XmlWriter
81 {
82 public:
83
84     static void append(Buffer& out, const Char16& x);
85
86     static void append(Buffer& out, char x)
87     {
88       out.append(x);
89     }
90
91     static void append(Buffer& out, Boolean x);
92
93     static void append(Buffer& out, Uint32 x);
94
95     static void append(Buffer& out, Sint32 x);
96
97     static void append(Buffer& out, Uint64 x);
98
99     static void append(Buffer& out, Sint64 x);
100
101     static void append(Buffer& out, Real32 x);
102
103     static void append(Buffer& out, Real64 x);
104
105     static void append(Buffer& out, const char* str);
106
107     static void append(Buffer& out, const String& str);
108
109     static void append(Buffer& out, const Indentor& x);
110
111     static void appendSpecial(Buffer& out, const Char16& x);
112
113     static void appendSpecial(Buffer& out, char x);
114
115     static void appendSpecial(Buffer& out, const char* str);
116
117     static void appendSpecial(Buffer& out, const String& str);
118
119     static String encodeURICharacters(const Buffer& uriString);
120     static String encodeURICharacters(const String& uriString);
121
122     static void appendLocalNameSpacePathElement(
123         Buffer& out,
124         const CIMNamespaceName& nameSpace);
125
126     static void appendNameSpacePathElement(
127         Buffer& out,
128         const String& host,
129         const CIMNamespaceName& nameSpace);
130
131     static void appendClassNameElement(
132         Buffer& out,
133         const CIMName& className);
134
135     static void appendInstanceNameElement(
136         Buffer& out,
137         const CIMObjectPath& instanceName);
138
139     static void appendClassPathElement(
140         Buffer& out,
141         const CIMObjectPath& classPath);
142
143     static void appendInstancePathElement(
144         Buffer& out,
145         const CIMObjectPath& instancePath);
146
147     static void appendLocalClassPathElement(
148         Buffer& out,
149         const CIMObjectPath& classPath);
150
151     static void appendLocalInstancePathElement(
152         Buffer& out,
153         const CIMObjectPath& instancePath);
154
155     static void appendLocalObjectPathElement(
156         Buffer& out,
157         const CIMObjectPath& objectPath);
158
159     static void appendValueElement(
160         Buffer& out,
161         const CIMValue& value);
162
163     static void printValueElement(
164         const CIMValue& value,
165         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
166
167     static void appendValueObjectWithPathElement(
168         Buffer& out,
169         const CIMObject& objectWithPath);
170
171     static void appendValueReferenceElement(
172         Buffer& out,
173         const CIMObjectPath& reference,
174         Boolean putValueWrapper);
175
176     static void printValueReferenceElement(
177         const CIMObjectPath& reference,
178         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
179
180     static void appendValueNamedInstanceElement(
181         Buffer& out,
182         const CIMInstance& namedInstance);
183
184     static void appendClassElement(
185         Buffer& out,
186         const CIMConstClass& cimclass);
187
188     static void printClassElement(
189         const CIMConstClass& cimclass,
190         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
191
192     static void appendInstanceElement(
193         Buffer& out,
194         const CIMConstInstance& instance);
195
196     static void printInstanceElement(
197         const CIMConstInstance& instance,
198         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
199
200     static void appendObjectElement(
201         Buffer& out,
202         const CIMConstObject& object);
203
204     static void appendPropertyElement(
205         Buffer& out,
206         const CIMConstProperty& property);
207
208     static void printPropertyElement(
209         const CIMConstProperty& property,
210         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
211
212     static void appendMethodElement(
213         Buffer& out,
214         const CIMConstMethod& method);
215
216     static void printMethodElement(
217         const CIMConstMethod& method,
218         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
219
220     static void appendParameterElement(
221         Buffer& out,
222         const CIMConstParameter& parameter);
223
224     static void printParameterElement(
225         const CIMConstParameter& parameter,
226         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
227
228     static void appendParamValueElement(
229         Buffer& out,
230         const CIMParamValue& paramValue);
231
232     static void printParamValueElement(
233         const CIMParamValue& paramValue,
234         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
235
236     static void appendQualifierElement(
237         Buffer& out,
238         const CIMConstQualifier& qualifier);
239
240     static void printQualifierElement(
241         const CIMConstQualifier& qualifier,
242         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
243
244     static void appendQualifierDeclElement(
245         Buffer& out,
246         const CIMConstQualifierDecl& qualifierDecl);
247
248     static void printQualifierDeclElement(
249         const CIMConstQualifierDecl& qualifierDecl,
250         PEGASUS_STD(ostream)& os=PEGASUS_STD(cout));
251
252     static void appendQualifierFlavorEntity(
253         Buffer& out,
254         const CIMFlavor & flavor);
255
256     static void appendScopeElement(
257         Buffer& out,
258         const CIMScope & scope);
259
260     static void appendMethodCallHeader(
261         Buffer& out,
262         const char* host,
263         const CIMName& cimMethod,
264         const String& cimObject,
265         const String& authenticationHeader,
266         HttpMethod httpMethod,
267         const AcceptLanguageList& acceptLanguages,
268         const ContentLanguageList& contentLanguages,
269         Uint32 contentLength);
270
271     // added to accommodate sending WBEMServerResponseTime PEP #128
272     static void appendMethodResponseHeader(
273         Buffer& out,
274         HttpMethod httpMethod,
275         const ContentLanguageList& contentLanguages,
276         Uint32 contentLength,
277         Uint64 serverResponseTime = 0);
278
279     static void appendHttpErrorResponseHeader(
280         Buffer& out,
281         const String& status,
282         const String& cimError = String::EMPTY,
283         const String& errorDetail = String::EMPTY);
284
285     static void appendUnauthorizedResponseHeader(
286         Buffer& out,
287         const String& content);
288
289 #ifdef PEGASUS_KERBEROS_AUTHENTICATION
290     static void appendOKResponseHeader(
291         Buffer& out,
292         const String& content);
293 #endif
294
295     static void appendReturnValueElement(
296         Buffer& out,
297         const CIMValue& value);
298
299     static void appendBooleanIParameter(
300         Buffer& out,
301         const char* name,
302         Boolean flag);
303
304     static void appendStringIParameter(
305         Buffer& out,
306         const char* name,
307         const String& str);
308
309     static void appendClassNameIParameter(
310         Buffer& out,
311         const char* name,
312         const CIMName& className);
313
314     static void appendInstanceNameIParameter(
315         Buffer& out,
316         const char* name,
317         const CIMObjectPath& instanceName);
318
319     static void appendObjectNameIParameter(
320         Buffer& out,
321         const char* name,
322         const CIMObjectPath& objectName);
323
324     static void appendClassIParameter(
325         Buffer& out,
326         const char* name,
327         const CIMConstClass& cimClass);
328
329     static void appendInstanceIParameter(
330         Buffer& out,
331         const char* name,
332         const CIMConstInstance& instance);
333
334     static void appendNamedInstanceIParameter(
335         Buffer& out,
336         const char* name,
337         const CIMInstance& namedInstance) ;
338
339     static void appendPropertyNameIParameter(
340         Buffer& out,
341         const CIMName& propertyName);
342
343     static void appendPropertyValueIParameter(
344         Buffer& out,
345         const char* name,
346         const CIMValue& value);
347
348     static void appendPropertyListIParameter(
349         Buffer& out,
350         const CIMPropertyList& propertyList);
351
352     static void appendQualifierDeclarationIParameter(
353         Buffer& out,
354         const char* name,
355         const CIMConstQualifierDecl& qualifierDecl);
356
357     static Buffer formatHttpErrorRspMessage(
358         const String& status,
359         const String& cimError = String::EMPTY,
360         const String& errorDetail = String::EMPTY);
361
362     static Buffer formatSimpleMethodReqMessage(
363         const char* host,
364         const CIMNamespaceName& nameSpace,
365         const CIMObjectPath& path,
366         const CIMName& methodName,
367         const Array<CIMParamValue>& parameters,
368         const String& messageId,
369         HttpMethod httpMethod,
370         const String& authenticationHeader,
371         const AcceptLanguageList& httpAcceptLanguages,
372         const ContentLanguageList& httpContentLanguages);
373
374     //PEP 128 - sending serverResponseTime (WBEMServerResponseTime) in respons header
375     static Buffer formatSimpleMethodRspMessage(
376         const CIMName& methodName,
377         const String& messageId,
378         HttpMethod httpMethod,
379         const ContentLanguageList& httpContentLanguages,
380         const Buffer& body,
381         Uint64 serverResponseTime,
382         Boolean isFirst = true,
383         Boolean isLast = true);
384
385     static Buffer formatSimpleMethodErrorRspMessage(
386         const CIMName& methodName,
387         const String& messageId,
388         HttpMethod httpMethod,
389         const CIMException& cimException);
390
391     static Buffer formatSimpleIMethodReqMessage(
392         const char* host,
393         const CIMNamespaceName& nameSpace,
394         const CIMName& iMethodName,
395         const String& messageId,
396         HttpMethod httpMethod,
397         const String& authenticationHeader,
398         const AcceptLanguageList& httpAcceptLanguages,
399         const ContentLanguageList& httpContentLanguages,
400         const Buffer& body);
401
402     //PEP 128 - sending serverResponseTime (WBEMServerResponseTime) in respons header
403     static Buffer formatSimpleIMethodRspMessage(
404         const CIMName& iMethodName,
405         const String& messageId,
406         HttpMethod httpMethod,
407         const ContentLanguageList& httpContentLanguages,
408         const Buffer& body,
409         Uint64 serverResponseTime,
410         Boolean isFirst = true,
411         Boolean isLast = true);
412
413     static Buffer formatSimpleIMethodErrorRspMessage(
414         const CIMName& iMethodName,
415         const String& messageId,
416         HttpMethod httpMethod,
417         const CIMException& cimException);
418
419     static void appendInstanceEParameter(
420         Buffer& out,
421         const char* name,
422         const CIMInstance& instance);
423
424     static void appendEMethodRequestHeader(
425         Buffer& out,
426         const char* requestUri,
427         const char* host,
428         const CIMName& cimMethod,
429         HttpMethod httpMethod,
430         const String& authenticationHeader,
431         const AcceptLanguageList& acceptLanguages,
432         const ContentLanguageList& contentLanguages,
433         Uint32 contentLength);
434
435     static void appendEMethodResponseHeader(
436         Buffer& out,
437         HttpMethod httpMethod,
438         const ContentLanguageList& contentLanguages,
439         Uint32 contentLength);
440
441     static Buffer formatSimpleEMethodReqMessage(
442         const char* requestUri,
443         const char* host,
444         const CIMName& eMethodName,
445         const String& messageId,
446         HttpMethod httpMethod,
447         const String& authenticationHeader,
448         const AcceptLanguageList& httpAcceptLanguages,
449         const ContentLanguageList& httpContentLanguages,
450         const Buffer& body);
451
452     static Buffer formatSimpleEMethodRspMessage(
453         const CIMName& eMethodName,
454         const String& messageId,
455         HttpMethod httpMethod,
456         const ContentLanguageList& httpContentLanguages,
457         const Buffer& body);
458
459     static Buffer formatSimpleEMethodErrorRspMessage(
460         const CIMName& eMethodName,
461         const String& messageId,
462         HttpMethod httpMethod,
463         const CIMException& cimException);
464
465     static void indentedPrint(
466         PEGASUS_STD(ostream)& os,
467         const char* text,
468         Uint32 indentChars = 2);
469
470     static String getNextMessageId();
471
472     /** Converts the given CIMKeyBinding type to one of the following:
473         "boolean", "string", or "numeric"
474     */
475     static const char* keyBindingTypeToString (CIMKeyBinding::Type type);
476
477 private:
478
479     static void _appendMessageElementBegin(
480         Buffer& out,
481         const String& messageId);
482         static void _appendMessageElementEnd(
483         Buffer& out);
484
485     static void _appendSimpleReqElementBegin(Buffer& out);
486     static void _appendSimpleReqElementEnd(Buffer& out);
487
488     static void _appendMethodCallElementBegin(
489         Buffer& out,
490         const CIMName& name);
491
492     static void _appendMethodCallElementEnd(
493         Buffer& out);
494
495     static void _appendIMethodCallElementBegin(
496         Buffer& out,
497         const CIMName& name);
498         static void _appendIMethodCallElementEnd(
499         Buffer& out);
500
501     static void _appendIParamValueElementBegin(
502         Buffer& out,
503         const char* name);
504         static void _appendIParamValueElementEnd(
505         Buffer& out);
506
507     static void _appendSimpleRspElementBegin(Buffer& out);
508     static void _appendSimpleRspElementEnd(Buffer& out);
509
510     static void _appendMethodResponseElementBegin(
511         Buffer& out,
512         const CIMName& name);
513         static void _appendMethodResponseElementEnd(
514         Buffer& out);
515
516     static void _appendIMethodResponseElementBegin(
517         Buffer& out,
518         const CIMName& name);
519         static void _appendIMethodResponseElementEnd(
520         Buffer& out);
521
522     static void _appendErrorElement(
523         Buffer& out,
524         const CIMException& cimException);
525
526     static void _appendIReturnValueElementBegin(Buffer& out);
527     static void _appendIReturnValueElementEnd(Buffer& out);
528
529     static void _appendSimpleExportReqElementBegin(Buffer& out);
530     static void _appendSimpleExportReqElementEnd(Buffer& out);
531
532     static void _appendEMethodCallElementBegin(
533         Buffer& out,
534         const CIMName& name);
535
536     static void _appendEMethodCallElementEnd(
537         Buffer& out);
538
539     static void _appendEParamValueElementBegin(
540         Buffer& out,
541         const char* name);
542         static void _appendEParamValueElementEnd(
543         Buffer& out);
544
545     static void _appendSimpleExportRspElementBegin(Buffer& out);
546     static void _appendSimpleExportRspElementEnd(Buffer& out);
547
548     static void _appendEMethodResponseElementBegin(
549         Buffer& out,
550         const CIMName& name);
551
552     static void _appendEMethodResponseElementEnd(
553         Buffer& out);
554
555     XmlWriter() { }
556 };
557
558 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
559     Buffer& out,
560     const char* x);
561
562 inline Buffer& operator<<(Buffer& out, char x)
563 {
564     out.append(x);
565     return out;
566 }
567
568 inline Buffer& operator<<(Buffer& out, const char* s)
569 {
570     out.append(s, strlen(s));
571     return out;
572 }
573
574 PEGASUS_COMMON_LINKAGE Buffer& operator<<(Buffer& out, const Char16& x);
575
576 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
577     Buffer& out,
578     const String& x);
579
580 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
581     Buffer& out,
582     const Indentor& x);
583
584 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
585     Buffer& out,
586     const Buffer& x);
587
588 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
589     Buffer& out,
590     Uint32 x);
591
592 PEGASUS_COMMON_LINKAGE Buffer& operator<<(
593     Buffer& out,
594     const CIMName& name);
595
596 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
597     PEGASUS_STD(ostream)& os,
598     const CIMDateTime& x);
599
600 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
601     PEGASUS_STD(ostream)& os,
602     const CIMName& name);
603
604 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
605     PEGASUS_STD(ostream)& os,
606     const CIMNamespaceName& name);
607
608 PEGASUS_NAMESPACE_END
609
610 #endif /* Pegasus_XmlWriter_h */