* BuildEngine.cs (BuildProjectFile): Use AddProperty method to specify
[mono.git] / mcs / class / System.Messaging / System.Messaging / XmlMessageFormatter.cs
1 //
2 // System.Messaging
3 //
4 // Authors:
5 //      Peter Van Isacker (sclytrack@planetinternet.be)
6 //      Rafael Teixeira   (rafaelteixeirabr@hotmail.com)
7 //
8 //      (C) 2003 Peter Van Isacker
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.IO;
36 using System.Xml.Serialization;
37
38
39 namespace System.Messaging 
40 {
41         public class XmlMessageFormatter: IMessageFormatter, ICloneable 
42         {
43                 private ArrayList targetTypes = new ArrayList ();               
44         
45                 public XmlMessageFormatter()
46                 {
47                 }
48                 
49                 public XmlMessageFormatter(string[] targetTypeNames) : this (GetTypesFromNames (targetTypeNames))
50                 {
51                 }
52                 
53                 public XmlMessageFormatter(Type[] targetTypes)
54                 {
55                         this.targetTypes = new ArrayList (targetTypes);
56                 }
57
58                 private static Type[] GetTypesFromNames(string[] targetTypeNames)
59                 {
60                         Type[] ts = new Type[targetTypeNames.Length];
61                         for (int i = 0; i < targetTypeNames.Length; i++)
62                                 ts[i] = Type.GetType (targetTypeNames[i]);
63                         
64                         return ts;
65                 }
66
67                 [MessagingDescription ("XmlMsgTargetTypeNames")]
68                 public string[] TargetTypeNames 
69                 {
70                         get 
71                         {
72                                 if (targetTypes == null)
73                                         return null;
74                                         
75                                 ArrayList listOfNames = new ArrayList (targetTypes.Count);
76                                 foreach (Type type in targetTypes)
77                                         listOfNames.Add (type.FullName);
78                                 return (string[]) listOfNames.ToArray (typeof (string));
79                         }
80                         set { TargetTypes = GetTypesFromNames (value); }
81                 }
82
83                 [Browsable (false)]
84                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
85                 [MessagingDescription ("XmlMsgTargetTypes")]
86                 public Type[] TargetTypes
87                 {
88                         get { return (Type[]) targetTypes.ToArray (typeof (Type)); }
89                         set { targetTypes = new ArrayList (value); }
90                 }
91                 
92                 [MonoTODO]
93                 public bool CanRead(Message message)
94                 {
95                         // Need an implementation of XmlNodeReader.
96                         throw new NotImplementedException();
97                 }
98                 
99                 public object Clone()
100                 {
101                         return new XmlMessageFormatter((Type[])TargetTypes.Clone());
102                 }
103                 
104                 private void AddType (Type t)
105                 {
106                         targetTypes.Add (t);
107                 }
108                                 
109                 public object Read(Message message)
110                 {
111                         message.BodyStream.Seek (0, SeekOrigin.Begin);
112                         object result = null;
113                         foreach (Type t in targetTypes) {
114                                 XmlSerializer serializer = new XmlSerializer (t);
115                                 try {
116                                         return serializer.Deserialize (message.BodyStream);
117                                 } catch (InvalidOperationException e) {
118                                         Console.WriteLine (e);
119                                 }
120                         }
121                         string error = "Unable to deserialize message body.  Type is not one of: " 
122                                 + String.Join (",", TargetTypeNames);
123                         throw new InvalidOperationException (error);
124                 }
125                 
126                 public void Write(Message message, object obj)
127                 {
128                         if (message == null)
129                                 throw new ArgumentNullException ();
130                                 
131                         Stream stream = message.BodyStream;
132                         if (stream == null) {
133                                 stream = new MemoryStream ();
134                                 message.BodyStream = stream;
135                         }
136
137                         XmlSerializer serializer = new XmlSerializer (obj.GetType ());
138                         serializer.Serialize (stream, obj);
139
140                         message.BodyType = (int) FormatterTypes.Xml;
141                         AddType (obj.GetType ());
142                 }
143         }
144 }