Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / System.Runtime.Serialization / System.Runtime.Serialization / XmlObjectSerializer.cs
1 //
2 // XmlObjectSerializer.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Text;
32 using System.Reflection;
33 using System.Runtime.Serialization.Formatters.Binary;
34 using System.Xml;
35 using System.Xml.Schema;
36
37 using QName = System.Xml.XmlQualifiedName;
38
39 namespace System.Runtime.Serialization
40 {
41         public abstract class XmlObjectSerializer
42         {
43                 // This is only for compatible mode.
44                 IDataContractSurrogate surrogate;
45
46                 SerializationBinder binder;
47                 ISurrogateSelector selector;
48
49                 int max_items = 0x10000; // FIXME: could be from config.
50
51                 protected XmlObjectSerializer ()
52                 {
53                 }
54
55                 public virtual bool IsStartObject (XmlReader reader)
56                 {
57                         return IsStartObject (XmlDictionaryReader.CreateDictionaryReader (reader));
58                 }
59
60                 public abstract bool IsStartObject (XmlDictionaryReader reader);
61
62                 public virtual object ReadObject (Stream stream)
63                 {
64                         var settings = new XmlReaderSettings ();
65                         settings.CheckCharacters = false;
66                         return ReadObject (XmlReader.Create (stream, settings));
67                 }
68
69                 public virtual object ReadObject (XmlReader reader)
70                 {
71                         return ReadObject (XmlDictionaryReader.CreateDictionaryReader (reader));
72                 }
73
74                 public virtual object ReadObject (XmlDictionaryReader reader)
75                 {
76                         return ReadObject (reader, true);
77                 }
78
79                 public virtual object ReadObject (XmlReader reader, bool readContentOnly)
80                 {
81                         return ReadObject (
82                                 XmlDictionaryReader.CreateDictionaryReader (reader),
83                                 readContentOnly);
84                 }
85
86                 [MonoTODO]
87                 public abstract object ReadObject (XmlDictionaryReader reader, bool readContentOnly);
88
89                 public virtual void WriteObject (Stream stream, object graph)
90                 {
91                         var settings = new XmlWriterSettings ();
92                         settings.Encoding = Encoding.UTF8;
93                         settings.CloseOutput = false;
94                         settings.OmitXmlDeclaration = true;
95                         settings.CheckCharacters = false;
96                         using (XmlWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (stream, settings))) {
97                                 WriteObject (xw, graph);
98                         }
99                 }
100
101                 public virtual void WriteObject (XmlWriter writer, object graph)
102                 {
103                         WriteObject (XmlDictionaryWriter.CreateDictionaryWriter (writer), graph);
104                 }
105
106                 public virtual void WriteStartObject (XmlWriter writer, object graph)
107                 {
108                         WriteStartObject (XmlDictionaryWriter.CreateDictionaryWriter (writer), graph);
109                 }
110
111                 public virtual void WriteObject (XmlDictionaryWriter writer, object graph)
112                 {
113                         WriteStartObject (writer, graph);
114                         WriteObjectContent (writer, graph);
115                         WriteEndObject (writer);
116                 }
117
118                 public abstract void WriteStartObject (
119                         XmlDictionaryWriter writer, object graph);
120
121                 public virtual void WriteObjectContent (XmlWriter writer, object graph)
122                 {
123                         WriteObjectContent (
124                                 XmlDictionaryWriter.CreateDictionaryWriter (writer),
125                                 graph);
126                 }
127
128                 public abstract void WriteObjectContent (
129                         XmlDictionaryWriter writer, object graph);
130
131                 public virtual void WriteEndObject (XmlWriter writer)
132                 {
133                         WriteEndObject (XmlDictionaryWriter.CreateDictionaryWriter (writer));
134                 }
135
136                 public abstract void WriteEndObject (
137                         XmlDictionaryWriter writer);
138         }
139 }