-
Notifications
You must be signed in to change notification settings - Fork 9
/
XmlRpcSerializer.cs
executable file
·2038 lines (1962 loc) · 65.4 KB
/
XmlRpcSerializer.cs
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
XML-RPC.NET library
Copyright (c) 2001-2006, Charles Cook <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// TODO: overriding default mapping action in a struct should not affect nested structs
namespace CookComputing.XmlRpc
{
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
struct Fault
{
public int faultCode;
public string faultString;
}
public class XmlRpcSerializer
{
// public properties
public int Indentation
{
get { return m_indentation; }
set { m_indentation = value; }
}
int m_indentation = 2;
public XmlRpcNonStandard NonStandard
{
get { return m_nonStandard; }
set { m_nonStandard = value; }
}
XmlRpcNonStandard m_nonStandard = XmlRpcNonStandard.None;
public bool UseEmptyParamsTag
{
get { return m_bUseEmptyParamsTag; }
set { m_bUseEmptyParamsTag = value; }
}
bool m_bUseEmptyParamsTag = true;
public bool UseIndentation
{
get { return m_bUseIndentation; }
set { m_bUseIndentation = value; }
}
bool m_bUseIndentation = true;
public bool UseIntTag
{
get { return m_useIntTag; }
set { m_useIntTag = value; }
}
bool m_useIntTag;
public bool UseStringTag
{
get { return m_useStringTag; }
set { m_useStringTag = value; }
}
bool m_useStringTag = true;
public Encoding XmlEncoding
{
get { return m_encoding; }
set { m_encoding = value; }
}
Encoding m_encoding = null;
// private properties
bool AllowInvalidHTTPContent
{
get { return (m_nonStandard & XmlRpcNonStandard.AllowInvalidHTTPContent) != 0; }
}
bool AllowNonStandardDateTime
{
get { return (m_nonStandard & XmlRpcNonStandard.AllowNonStandardDateTime) != 0; }
}
bool AllowStringFaultCode
{
get { return (m_nonStandard & XmlRpcNonStandard.AllowStringFaultCode) != 0; }
}
bool IgnoreDuplicateMembers
{
get { return (m_nonStandard & XmlRpcNonStandard.IgnoreDuplicateMembers) != 0; }
}
bool MapEmptyDateTimeToMinValue
{
get { return (m_nonStandard & XmlRpcNonStandard.MapEmptyDateTimeToMinValue) != 0; }
}
bool MapZerosDateTimeToMinValue
{
get { return (m_nonStandard & XmlRpcNonStandard.MapZerosDateTimeToMinValue) != 0; }
}
public void SerializeRequest(Stream stm, XmlRpcRequest request)
{
XmlTextWriter xtw = new XmlTextWriter(stm, m_encoding);
ConfigureXmlFormat(xtw);
xtw.WriteStartDocument();
xtw.WriteStartElement("", "methodCall", "");
{
// TODO: use global action setting
MappingAction mappingAction = MappingAction.Error;
if (request.xmlRpcMethod == null)
xtw.WriteElementString("methodName", request.method);
else
xtw.WriteElementString("methodName", request.xmlRpcMethod);
if (request.args.Length > 0 || UseEmptyParamsTag)
{
xtw.WriteStartElement("", "params", "");
try
{
if (!IsStructParamsMethod(request.mi))
SerializeParams(xtw, request, mappingAction);
else
SerializeStructParams(xtw, request, mappingAction);
}
catch (XmlRpcUnsupportedTypeException ex)
{
throw new XmlRpcUnsupportedTypeException(ex.UnsupportedType,
String.Format("A parameter is of, or contains an instance of, "
+ "type {0} which cannot be mapped to an XML-RPC type",
ex.UnsupportedType));
}
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
xtw.Flush();
}
void SerializeParams(XmlTextWriter xtw, XmlRpcRequest request,
MappingAction mappingAction)
{
ParameterInfo[] pis = null;
if (request.mi != null)
{
pis = request.mi.GetParameters();
}
for (int i = 0; i < request.args.Length; i++)
{
if (pis != null)
{
if (i >= pis.Length)
throw new XmlRpcInvalidParametersException("Number of request "
+ "parameters greater than number of proxy method parameters.");
if (i == pis.Length - 1
&& Attribute.IsDefined(pis[i], typeof(ParamArrayAttribute)))
{
Array ary = (Array)request.args[i];
foreach (object o in ary)
{
if (o == null)
throw new XmlRpcNullParameterException(
"Null parameter in params array");
xtw.WriteStartElement("", "param", "");
Serialize(xtw, o, mappingAction);
xtw.WriteEndElement();
}
break;
}
}
if (request.args[i] == null)
{
throw new XmlRpcNullParameterException(String.Format(
"Null method parameter #{0}", i + 1));
}
xtw.WriteStartElement("", "param", "");
Serialize(xtw, request.args[i], mappingAction);
xtw.WriteEndElement();
}
}
void SerializeStructParams(XmlTextWriter xtw, XmlRpcRequest request,
MappingAction mappingAction)
{
ParameterInfo[] pis = request.mi.GetParameters();
if (request.args.Length > pis.Length)
throw new XmlRpcInvalidParametersException("Number of request "
+ "parameters greater than number of proxy method parameters.");
if (Attribute.IsDefined(pis[request.args.Length - 1],
typeof(ParamArrayAttribute)))
{
throw new XmlRpcInvalidParametersException("params parameter cannot "
+ "be used with StructParams.");
}
xtw.WriteStartElement("", "param", "");
xtw.WriteStartElement("", "value", "");
xtw.WriteStartElement("", "struct", "");
for (int i = 0; i < request.args.Length; i++)
{
if (request.args[i] == null)
{
throw new XmlRpcNullParameterException(String.Format(
"Null method parameter #{0}", i + 1));
}
xtw.WriteStartElement("", "member", "");
xtw.WriteElementString("name", pis[i].Name);
Serialize(xtw, request.args[i], mappingAction);
xtw.WriteEndElement();
}
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();
}
#if (!COMPACT_FRAMEWORK)
public XmlRpcRequest DeserializeRequest(Stream stm, Type svcType)
{
if (stm == null)
throw new ArgumentNullException("stm",
"XmlRpcSerializer.DeserializeRequest");
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
try
{
using (XmlTextReader xmlRdr = new XmlTextReader(stm))
{
xmlRdr.ProhibitDtd = true;
xdoc.Load(xmlRdr);
}
}
catch (Exception ex)
{
throw new XmlRpcIllFormedXmlException(
"Request from client does not contain valid XML.", ex);
}
return DeserializeRequest(xdoc, svcType);
}
public XmlRpcRequest DeserializeRequest(TextReader txtrdr, Type svcType)
{
if (txtrdr == null)
throw new ArgumentNullException("txtrdr",
"XmlRpcSerializer.DeserializeRequest");
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
try
{
using (XmlTextReader xmlRdr = new XmlTextReader(txtrdr))
{
xmlRdr.ProhibitDtd = true;
xdoc.Load(xmlRdr);
}
}
catch (Exception ex)
{
throw new XmlRpcIllFormedXmlException(
"Request from client does not contain valid XML.", ex);
}
return DeserializeRequest(xdoc, svcType);
}
public XmlRpcRequest DeserializeRequest(XmlDocument xdoc, Type svcType)
{
XmlRpcRequest request = new XmlRpcRequest();
XmlNode callNode = SelectSingleNode(xdoc, "methodCall");
if (callNode == null)
{
throw new XmlRpcInvalidXmlRpcException(
"Request XML not valid XML-RPC - missing methodCall element.");
}
XmlNode methodNode = SelectSingleNode(callNode, "methodName");
if (methodNode == null)
{
throw new XmlRpcInvalidXmlRpcException(
"Request XML not valid XML-RPC - missing methodName element.");
}
if (methodNode.FirstChild == null)
{
throw new XmlRpcInvalidXmlRpcException(
"Request XML not valid XML-RPC - missing methodName element.");
}
request.method = methodNode.FirstChild.Value;
if (request.method == "")
{
throw new XmlRpcInvalidXmlRpcException(
"Request XML not valid XML-RPC - empty methodName.");
}
request.mi = null;
ParameterInfo[] pis = new ParameterInfo[0];
if (svcType != null)
{
// retrieve info for the method which handles this XML-RPC method
XmlRpcServiceInfo svcInfo
= XmlRpcServiceInfo.CreateServiceInfo(svcType);
request.mi = svcInfo.GetMethodInfo(request.method);
// if a service type has been specified and we cannot find the requested
// method then we must throw an exception
if (request.mi == null)
{
string msg = String.Format("unsupported method called: {0}",
request.method);
throw new XmlRpcUnsupportedMethodException(msg);
}
// method must be marked with XmlRpcMethod attribute
Attribute attr = Attribute.GetCustomAttribute(request.mi,
typeof(XmlRpcMethodAttribute));
if (attr == null)
{
throw new XmlRpcMethodAttributeException(
"Method must be marked with the XmlRpcMethod attribute.");
}
pis = request.mi.GetParameters();
}
XmlNode paramsNode = SelectSingleNode(callNode, "params");
if (paramsNode == null)
{
if (svcType != null)
{
if (pis.Length == 0)
{
request.args = new object[0];
return request;
}
else
{
throw new XmlRpcInvalidParametersException(
"Method takes parameters and params element is missing.");
}
}
else
{
request.args = new object[0];
return request;
}
}
XmlNode[] paramNodes = SelectNodes(paramsNode, "param");
int paramsPos = GetParamsPos(pis);
int minParamCount = paramsPos == -1 ? pis.Length : paramsPos;
if (svcType != null && paramNodes.Length < minParamCount)
{
throw new XmlRpcInvalidParametersException(
"Request contains too few param elements based on method signature.");
}
if (svcType != null && paramsPos == -1 && paramNodes.Length > pis.Length)
{
throw new XmlRpcInvalidParametersException(
"Request contains too many param elements based on method signature.");
}
ParseStack parseStack = new ParseStack("request");
// TODO: use global action setting
MappingAction mappingAction = MappingAction.Error;
int paramObjCount = (paramsPos == -1 ? paramNodes.Length : paramsPos + 1);
Object[] paramObjs = new Object[paramObjCount];
// parse ordinary parameters
int ordinaryParams = (paramsPos == -1 ? paramNodes.Length : paramsPos);
for (int i = 0; i < ordinaryParams; i++)
{
XmlNode paramNode = paramNodes[i];
XmlNode valueNode = SelectSingleNode(paramNode, "value");
if (valueNode == null)
throw new XmlRpcInvalidXmlRpcException("Missing value element.");
XmlNode node = SelectValueNode(valueNode);
if (svcType != null)
{
parseStack.Push(String.Format("parameter {0}", i + 1));
// TODO: why following commented out?
// parseStack.Push(String.Format("parameter {0} mapped to type {1}",
// i, pis[i].ParameterType.Name));
paramObjs[i] = ParseValue(node, pis[i].ParameterType, parseStack,
mappingAction);
}
else
{
parseStack.Push(String.Format("parameter {0}", i));
paramObjs[i] = ParseValue(node, null, parseStack, mappingAction);
}
parseStack.Pop();
}
// parse params parameters
if (paramsPos != -1)
{
Type paramsType = pis[paramsPos].ParameterType.GetElementType();
Object[] args = new Object[1];
args[0] = paramNodes.Length - paramsPos;
Array varargs = (Array)CreateArrayInstance(pis[paramsPos].ParameterType,
args);
for (int i = 0; i < varargs.Length; i++)
{
XmlNode paramNode = paramNodes[i + paramsPos];
XmlNode valueNode = SelectSingleNode(paramNode, "value");
if (valueNode == null)
throw new XmlRpcInvalidXmlRpcException("Missing value element.");
XmlNode node = SelectValueNode(valueNode);
parseStack.Push(String.Format("parameter {0}", i + 1 + paramsPos));
varargs.SetValue(ParseValue(node, paramsType, parseStack,
mappingAction), i);
parseStack.Pop();
}
paramObjs[paramsPos] = varargs;
}
request.args = paramObjs;
return request;
}
int GetParamsPos(ParameterInfo[] pis)
{
if (pis.Length == 0)
return -1;
if (Attribute.IsDefined(pis[pis.Length - 1], typeof(ParamArrayAttribute)))
{
return pis.Length - 1;
}
else
return -1;
}
public void SerializeResponse(Stream stm, XmlRpcResponse response)
{
Object ret = response.retVal;
if (ret is XmlRpcFaultException)
{
SerializeFaultResponse(stm, (XmlRpcFaultException)ret);
return;
}
XmlTextWriter xtw = new XmlTextWriter(stm, m_encoding);
ConfigureXmlFormat(xtw);
xtw.WriteStartDocument();
xtw.WriteStartElement("", "methodResponse", "");
xtw.WriteStartElement("", "params", "");
// "void" methods actually return an empty string value
if (ret == null)
{
ret = "";
}
xtw.WriteStartElement("", "param", "");
// TODO: use global action setting
MappingAction mappingAction = MappingAction.Error;
try
{
Serialize(xtw, ret, mappingAction);
}
catch (XmlRpcUnsupportedTypeException ex)
{
throw new XmlRpcInvalidReturnType(string.Format(
"Return value is of, or contains an instance of, type {0} which "
+ "cannot be mapped to an XML-RPC type", ex.UnsupportedType));
}
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.Flush();
}
#endif
public XmlRpcResponse DeserializeResponse(Stream stm, Type svcType)
{
if (stm == null)
throw new ArgumentNullException("stm",
"XmlRpcSerializer.DeserializeResponse");
if (AllowInvalidHTTPContent)
{
Stream newStm = new MemoryStream();
Util.CopyStream(stm, newStm);
stm = newStm;
stm.Position = 0;
while (true)
{
// for now just strip off any leading CR-LF characters
int byt = stm.ReadByte();
if (byt == -1)
throw new XmlRpcIllFormedXmlException(
"Response from server does not contain valid XML.");
if (byt != 0x0d && byt != 0x0a && byt != ' ' && byt != '\t')
{
stm.Position = stm.Position - 1;
break;
}
}
}
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
try
{
var xmlRdr = new XmlTextReader(stm);
#if (!COMPACT_FRAMEWORK)
xmlRdr.ProhibitDtd = true;
#endif
xdoc.Load(xmlRdr);
}
catch (Exception ex)
{
throw new XmlRpcIllFormedXmlException(
"Response from server does not contain valid XML.", ex);
}
return DeserializeResponse(xdoc, svcType);
}
public XmlRpcResponse DeserializeResponse(TextReader txtrdr, Type svcType)
{
if (txtrdr == null)
throw new ArgumentNullException("txtrdr",
"XmlRpcSerializer.DeserializeResponse");
XmlDocument xdoc = new XmlDocument();
xdoc.PreserveWhitespace = true;
try
{
using (XmlTextReader xmlRdr = new XmlTextReader(txtrdr))
{
#if (!COMPACT_FRAMEWORK)
xmlRdr.ProhibitDtd = true;
#endif
xdoc.Load(xmlRdr);
}
}
catch (Exception ex)
{
throw new XmlRpcIllFormedXmlException(
"Response from server does not contain valid XML.", ex);
}
return DeserializeResponse(xdoc, svcType);
}
public XmlRpcResponse DeserializeResponse(XmlDocument xdoc, Type returnType)
{
XmlRpcResponse response = new XmlRpcResponse();
Object retObj = null;
XmlNode methodResponseNode = SelectSingleNode(xdoc, "methodResponse");
if (methodResponseNode == null)
{
throw new XmlRpcInvalidXmlRpcException(
"Response XML not valid XML-RPC - missing methodResponse element.");
}
// check for fault response
XmlNode faultNode = SelectSingleNode(methodResponseNode, "fault");
if (faultNode != null)
{
ParseStack parseStack = new ParseStack("fault response");
// TODO: use global action setting
MappingAction mappingAction = MappingAction.Error;
XmlRpcFaultException faultEx = ParseFault(faultNode, parseStack,
mappingAction);
throw faultEx;
}
XmlNode paramsNode = SelectSingleNode(methodResponseNode, "params");
if (paramsNode == null && returnType != null)
{
if (returnType == typeof(void))
return new XmlRpcResponse(null);
else
throw new XmlRpcInvalidXmlRpcException(
"Response XML not valid XML-RPC - missing params element.");
}
XmlNode paramNode = SelectSingleNode(paramsNode, "param");
if (paramNode == null && returnType != null)
{
if (returnType == typeof(void))
return new XmlRpcResponse(null);
else
throw new XmlRpcInvalidXmlRpcException(
"Response XML not valid XML-RPC - missing params element.");
}
XmlNode valueNode = SelectSingleNode(paramNode, "value");
if (valueNode == null)
{
throw new XmlRpcInvalidXmlRpcException(
"Response XML not valid XML-RPC - missing value element.");
}
if (returnType == typeof(void))
{
retObj = null;
}
else
{
ParseStack parseStack = new ParseStack("response");
// TODO: use global action setting
MappingAction mappingAction = MappingAction.Error;
XmlNode node = SelectValueNode(valueNode);
retObj = ParseValue(node, returnType, parseStack, mappingAction);
}
response.retVal = retObj;
return response;
}
//#if (DEBUG)
public
//#endif
void Serialize(
XmlTextWriter xtw,
Object o,
MappingAction mappingAction)
{
Serialize(xtw, o, mappingAction, new ArrayList(16));
}
//#if (DEBUG)
public
//#endif
void Serialize(
XmlTextWriter xtw,
Object o,
MappingAction mappingAction,
ArrayList nestedObjs)
{
if (nestedObjs.Contains(o))
throw new XmlRpcUnsupportedTypeException(nestedObjs[0].GetType(),
"Cannot serialize recursive data structure");
nestedObjs.Add(o);
try
{
xtw.WriteStartElement("", "value", "");
XmlRpcType xType = XmlRpcServiceInfo.GetXmlRpcType(o.GetType());
if (xType == XmlRpcType.tArray)
{
xtw.WriteStartElement("", "array", "");
xtw.WriteStartElement("", "data", "");
Array a = (Array)o;
foreach (Object aobj in a)
{
if (aobj == null)
throw new XmlRpcMappingSerializeException(String.Format(
"Items in array cannot be null ({0}[]).",
o.GetType().GetElementType()));
Serialize(xtw, aobj, mappingAction, nestedObjs);
}
xtw.WriteEndElement();
xtw.WriteEndElement();
}
else if (xType == XmlRpcType.tMultiDimArray)
{
Array mda = (Array)o;
int[] indices = new int[mda.Rank];
BuildArrayXml(xtw, mda, 0, indices, mappingAction, nestedObjs);
}
else if (xType == XmlRpcType.tBase64)
{
byte[] buf = (byte[])o;
xtw.WriteStartElement("", "base64", "");
xtw.WriteBase64(buf, 0, buf.Length);
xtw.WriteEndElement();
}
else if (xType == XmlRpcType.tBoolean)
{
bool boolVal;
if (o is bool)
boolVal = (bool)o;
else
boolVal = (bool)(XmlRpcBoolean)o;
if (boolVal)
xtw.WriteElementString("boolean", "1");
else
xtw.WriteElementString("boolean", "0");
}
else if (xType == XmlRpcType.tDateTime)
{
DateTime dt;
if (o is DateTime)
dt = (DateTime)o;
else
dt = (XmlRpcDateTime)o;
string sdt = dt.ToString("yyyyMMdd'T'HH':'mm':'ss",
DateTimeFormatInfo.InvariantInfo);
xtw.WriteElementString("dateTime.iso8601", sdt);
}
else if (xType == XmlRpcType.tDouble)
{
double doubleVal;
if (o is double)
doubleVal = (double)o;
else
doubleVal = (XmlRpcDouble)o;
xtw.WriteElementString("double", doubleVal.ToString(null,
CultureInfo.InvariantCulture));
}
else if (xType == XmlRpcType.tHashtable)
{
xtw.WriteStartElement("", "struct", "");
XmlRpcStruct xrs = o as XmlRpcStruct;
foreach (object obj in xrs.Keys)
{
string skey = obj as string;
xtw.WriteStartElement("", "member", "");
xtw.WriteElementString("name", skey);
Serialize(xtw, xrs[skey], mappingAction, nestedObjs);
xtw.WriteEndElement();
}
xtw.WriteEndElement();
}
else if (xType == XmlRpcType.tInt32)
{
if (UseIntTag)
xtw.WriteElementString("int", o.ToString());
else
xtw.WriteElementString("i4", o.ToString());
}
else if (xType == XmlRpcType.tInt64)
{
xtw.WriteElementString("i8", o.ToString());
}
else if (xType == XmlRpcType.tString)
{
if (UseStringTag)
xtw.WriteElementString("string", (string)o);
else
xtw.WriteString((string)o);
}
else if (xType == XmlRpcType.tStruct)
{
MappingAction structAction
= StructMappingAction(o.GetType(), mappingAction);
xtw.WriteStartElement("", "struct", "");
MemberInfo[] mis = o.GetType().GetMembers();
foreach (MemberInfo mi in mis)
{
if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute)))
continue;
if (mi.MemberType == MemberTypes.Field)
{
FieldInfo fi = (FieldInfo)mi;
string member = fi.Name;
Attribute attrchk = Attribute.GetCustomAttribute(fi,
typeof(XmlRpcMemberAttribute));
if (attrchk != null && attrchk is XmlRpcMemberAttribute)
{
string mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
if (mmbr != "")
member = mmbr;
}
if (fi.GetValue(o) == null)
{
MappingAction memberAction = MemberMappingAction(o.GetType(),
fi.Name, structAction);
if (memberAction == MappingAction.Ignore)
continue;
throw new XmlRpcMappingSerializeException(@"Member """ + member +
@""" of struct """ + o.GetType().Name + @""" cannot be null.");
}
xtw.WriteStartElement("", "member", "");
xtw.WriteElementString("name", member);
Serialize(xtw, fi.GetValue(o), mappingAction, nestedObjs);
xtw.WriteEndElement();
}
else if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = (PropertyInfo)mi;
string member = pi.Name;
Attribute attrchk = Attribute.GetCustomAttribute(pi,
typeof(XmlRpcMemberAttribute));
if (attrchk != null && attrchk is XmlRpcMemberAttribute)
{
string mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
if (mmbr != "")
member = mmbr;
}
if (pi.GetValue(o, null) == null)
{
MappingAction memberAction = MemberMappingAction(o.GetType(),
pi.Name, structAction);
if (memberAction == MappingAction.Ignore)
continue;
}
xtw.WriteStartElement("", "member", "");
xtw.WriteElementString("name", member);
Serialize(xtw, pi.GetValue(o, null), mappingAction, nestedObjs);
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
}
else if (xType == XmlRpcType.tVoid)
xtw.WriteElementString("string", "");
else
throw new XmlRpcUnsupportedTypeException(o.GetType());
xtw.WriteEndElement();
}
catch (System.NullReferenceException)
{
throw new XmlRpcNullReferenceException("Attempt to serialize data "
+ "containing null reference");
}
finally
{
nestedObjs.RemoveAt(nestedObjs.Count - 1);
}
}
void BuildArrayXml(
XmlTextWriter xtw,
Array ary,
int CurRank,
int[] indices,
MappingAction mappingAction,
ArrayList nestedObjs)
{
xtw.WriteStartElement("", "array", "");
xtw.WriteStartElement("", "data", "");
if (CurRank < (ary.Rank - 1))
{
for (int i = 0; i < ary.GetLength(CurRank); i++)
{
indices[CurRank] = i;
xtw.WriteStartElement("", "value", "");
BuildArrayXml(xtw, ary, CurRank + 1, indices, mappingAction, nestedObjs);
xtw.WriteEndElement();
}
}
else
{
for (int i = 0; i < ary.GetLength(CurRank); i++)
{
indices[CurRank] = i;
Serialize(xtw, ary.GetValue(indices), mappingAction, nestedObjs);
}
}
xtw.WriteEndElement();
xtw.WriteEndElement();
}
Object ParseValue(
XmlNode node,
Type ValueType,
ParseStack parseStack,
MappingAction mappingAction)
{
Type parsedType;
Type parsedArrayType;
return ParseValue(node, ValueType, parseStack, mappingAction,
out parsedType, out parsedArrayType);
}
//#if (DEBUG)
public
//#endif
Object ParseValue(
XmlNode node,
Type ValueType,
ParseStack parseStack,
MappingAction mappingAction,
out Type ParsedType,
out Type ParsedArrayType)
{
ParsedType = null;
ParsedArrayType = null;
// if suppplied type is System.Object then ignore it because
// if doesn't provide any useful information (parsing methods
// expect null in this case)
Type valType = ValueType;
if (valType != null && valType.BaseType == null)
valType = null;
Object retObj = null;
if (node == null)
{
retObj = "";
}
else if (node is XmlText || node is XmlWhitespace)
{
if (valType != null && valType != typeof(string))
{
throw new XmlRpcTypeMismatchException(parseStack.ParseType
+ " contains implicit string value where "
+ XmlRpcServiceInfo.GetXmlRpcTypeString(valType)
+ " expected " + StackDump(parseStack));
}
retObj = node.Value;
}
else
{
if (node.Name == "array")
retObj = ParseArray(node, valType, parseStack, mappingAction);
else if (node.Name == "base64")
retObj = ParseBase64(node, valType, parseStack, mappingAction);
else if (node.Name == "struct")
{
// if we don't know the expected struct type then we must
// parse the XML-RPC struct as an instance of XmlRpcStruct
if (valType != null && valType != typeof(XmlRpcStruct)
&& !valType.IsSubclassOf(typeof(XmlRpcStruct)))
{
retObj = ParseStruct(node, valType, parseStack, mappingAction);
}
else
{
if (valType == null || valType == typeof(object))
valType = typeof(XmlRpcStruct);
// TODO: do we need to validate type here?
retObj = ParseHashtable(node, valType, parseStack, mappingAction);
}
}
else if (node.Name == "i4" // integer has two representations in XML-RPC spec
|| node.Name == "int")
{
retObj = ParseInt(node, valType, parseStack, mappingAction);
ParsedType = typeof(int);
ParsedArrayType = typeof(int[]);
}
else if (node.Name == "i8")
{
retObj = ParseLong(node, valType, parseStack, mappingAction);
ParsedType = typeof(long);
ParsedArrayType = typeof(long[]);
}
else if (node.Name == "string")
{
retObj = ParseString(node, valType, parseStack, mappingAction);
ParsedType = typeof(string);
ParsedArrayType = typeof(string[]);
}
else if (node.Name == "boolean")
{
retObj = ParseBoolean(node, valType, parseStack, mappingAction);
ParsedType = typeof(bool);
ParsedArrayType = typeof(bool[]);
}
else if (node.Name == "double")
{
retObj = ParseDouble(node, valType, parseStack, mappingAction);
ParsedType = typeof(double);
ParsedArrayType = typeof(double[]);
}
else if (node.Name == "dateTime.iso8601")
{
retObj = ParseDateTime(node, valType, parseStack, mappingAction);
ParsedType = typeof(DateTime);
ParsedArrayType = typeof(DateTime[]);
}
else
throw new XmlRpcInvalidXmlRpcException(
"Invalid value element: <" + node.Name + ">");
}
return retObj;
}
Object ParseArray(
XmlNode node,
Type ValueType,
ParseStack parseStack,
MappingAction mappingAction)
{
// required type must be an array
if (ValueType != null
&& !(ValueType.IsArray == true
|| ValueType == typeof(Array)
|| ValueType == typeof(object)))
{
throw new XmlRpcTypeMismatchException(parseStack.ParseType
+ " contains array value where "
+ XmlRpcServiceInfo.GetXmlRpcTypeString(ValueType)
+ " expected " + StackDump(parseStack));
}
if (ValueType != null)
{
XmlRpcType xmlRpcType = XmlRpcServiceInfo.GetXmlRpcType(ValueType);
if (xmlRpcType == XmlRpcType.tMultiDimArray)
{
parseStack.Push("array mapped to type " + ValueType.Name);
Object ret = ParseMultiDimArray(node, ValueType, parseStack,
mappingAction);
return ret;
}
parseStack.Push("array mapped to type " + ValueType.Name);
}
else
parseStack.Push("array");
XmlNode dataNode = SelectSingleNode(node, "data");
XmlNode[] childNodes = SelectNodes(dataNode, "value");
int nodeCount = childNodes.Length;
Object[] elements = new Object[nodeCount];
// determine type of array elements
Type elemType = null;
if (ValueType != null