2003-05-15: Jean-Marc ANDRE (jean-marc.andre@polymtl.ca)
[mono.git] / mcs / class / System.Runtime.Serialization.Formatters.Soap / System.Runtime.Serialization.Formatters.Soap / SoapReader.cs
1 // created on 24/04/2003 at 15:35\r
2 //\r
3 //      System.Runtime.Serialization.Formatters.Soap.SoapReader\r
4 //\r
5 //      Authors:\r
6 //              Jean-Marc Andre (jean-marc.andre@polymtl.ca)\r
7 //\r
8 \r
9 using System;\r
10 using System.Reflection;\r
11 using System.Collections;\r
12 using System.Runtime.Serialization;\r
13 \r
14 namespace System.Runtime.Serialization.Formatters.Soap {\r
15         internal class SoapReader: ISoapReader {\r
16                 public event ElementReadEventHandler ElementReadEvent;\r
17                 \r
18                 public SoapReader(ISoapParser parser) {\r
19                         // register the SoapElementReadEvent handler\r
20                         parser.SoapElementReadEvent += new SoapElementReadEventHandler(SoapElementRead);\r
21                 }\r
22                 \r
23                 // called when SoapElementReadEvent is raized by the SoapParser object\r
24                 public void SoapElementRead(ISoapParser sender, SoapElementReadEventArgs e) {\r
25                         Queue elementQueue = e.ElementQueue;\r
26                         Queue elementInfoQueue = new Queue();\r
27                         SoapSerializationEntry root = (SoapSerializationEntry) elementQueue.Dequeue();\r
28                         \r
29                         ElementInfo rootInfo = GetElementInfo(root);\r
30                         SoapSerializationEntry field;\r
31                         while(elementQueue.Count > 0){\r
32                                 field = (SoapSerializationEntry) elementQueue.Dequeue();\r
33                                 elementInfoQueue.Enqueue(GetElementInfo(field));\r
34                         }\r
35 \r
36                         // raise the ElementReadEvent\r
37                         ElementReadEvent(this,new ElementReadEventArgs(rootInfo, elementInfoQueue));\r
38                 }\r
39                 \r
40                 // Converts the text information from the SoapParser into\r
41                 // information that with by used by the ObjectReader to reconstruct\r
42                 // the object.\r
43                 private ElementInfo GetElementInfo(SoapSerializationEntry entry) {\r
44                         SoapTypeMapping mapping = new SoapTypeMapping(entry.elementName, entry.elementNamespace);\r
45                         Type elementType = SoapTypeMapper.GetType(mapping);\r
46                         \r
47                         \r
48                         long id = 0;\r
49                         ElementType elementId = ElementType.Nothing;\r
50                         ICollection attrLst = entry.elementAttributes;\r
51                         int[] arrayRank = null;\r
52                         foreach(SoapAttributeStruct attr in attrLst){\r
53                                 if(attr.attributeName == "id"){\r
54                                         string attrId = ((string)attr.attributeValue).Remove(0,4);\r
55                                         id = (new FormatterConverter()).ToInt64(attrId);\r
56                                         elementId = ElementType.Id;\r
57                                 } else if(attr.attributeName == "href") {\r
58                                         string attrId = ((string)attr.attributeValue).Remove(0,5);\r
59                                         id = (new FormatterConverter()).ToInt64(attrId);\r
60                                         elementId = ElementType.Href;\r
61                                 } else if(attr.attributeName == "xsi:null" && attr.attributeValue == "1") {\r
62                                         elementId = ElementType.Null;\r
63                                 } else if(attr.attributeName == "SOAP-ENC:arrayType") {\r
64                                         string[] tokens = attr.attributeValue.Split(new System.Char[] {'[',',',']'});\r
65                                         mapping = new SoapTypeMapping(tokens[0], attr.prefix);\r
66                                         elementType = SoapTypeMapper.GetType(mapping);\r
67                                         arrayRank = new int[tokens.Length - 2];\r
68                                         for(int i=0; i<tokens.Length-2; i++) {\r
69                                                 arrayRank[i] = Convert.ToInt32(tokens[i+1]);\r
70                                         }\r
71                                         Type tempType = Type.GetType(elementType.ToString()+"[]");\r
72                                         \r
73                                         if(tempType == null) {\r
74                                                 AssemblyName assName = elementType.Assembly.GetName();\r
75                                                 Assembly ass = Assembly.Load(assName);\r
76                                                 tempType = ass.GetType(elementType.ToString()+"[]", true);\r
77                                         }\r
78                                         \r
79                                         elementType = tempType;\r
80                                 } else if(attr.attributeName == "xsi:type") {\r
81                                         mapping = new SoapTypeMapping(attr.attributeValue, attr.prefix);\r
82                                         elementType = SoapTypeMapper.GetType(mapping);\r
83                                         \r
84                                         \r
85                                 }\r
86                         }\r
87                         \r
88 \r
89                         ElementInfo elementInfo =  new ElementInfo(elementType, entry.elementName, entry.elementValue, elementId, id, arrayRank);\r
90                         return elementInfo;\r
91                         \r
92                 }\r
93         }\r
94 }\r