New tests.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / XmlReaderBodyWriter.cs
1 //
2 // XmlReaderBodyWriter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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.ObjectModel;
30 using System.IO;
31 using System.Runtime.Serialization;
32 using System.Xml;
33
34 namespace System.ServiceModel.Channels
35 {
36         internal class XmlReaderBodyWriter : BodyWriter
37         {
38                 XmlDictionaryReader reader;
39                 string xml_bak;
40                 XmlParserContext parser_context;
41                 bool consumed;
42
43                 public XmlReaderBodyWriter (string xml, int maxBufferSize, XmlParserContext ctx)
44                         : base (true)
45                 {
46                         var settings = new XmlReaderSettings () {
47                                 // FIXME: enable this line (once MaxCharactersInDocument is implemented)
48                                 // MaxCharactersInDocument = maxBufferSize,
49                                 ConformanceLevel = ConformanceLevel.Fragment
50                                 };
51                         reader = XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (xml), settings, ctx));
52                         reader.MoveToContent ();
53                         xml_bak = xml;
54                         parser_context = ctx;
55                 }
56
57                 public XmlReaderBodyWriter (XmlDictionaryReader reader)
58                         : base (false)
59                 {
60                         reader.MoveToContent ();
61                         this.reader = reader;
62                 }
63
64                 protected override BodyWriter OnCreateBufferedCopy (
65                         int maxBufferSize)
66                 {
67 #if true
68                         if (xml_bak == null) {
69                                 if (consumed)
70                                         throw new InvalidOperationException ("Body xml reader is already consumed");
71                                 var sw = new StringWriter ();
72                                 var xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw));
73                                 xw.WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
74                                 xw.WriteAttributes (reader, false);
75
76                                 var nsmgr = new XmlNamespaceManager (reader.NameTable);
77                                 var inr = reader as IXmlNamespaceResolver;
78                                 if (inr != null)
79                                         foreach (var p in inr.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml))
80                                                 if (xw.LookupPrefix (p.Value) != p.Key)
81                                                         xw.WriteXmlnsAttribute (p.Key, p.Value);
82                                 if (!reader.IsEmptyElement) {
83                                         reader.Read ();
84                                         while (reader.NodeType != XmlNodeType.EndElement)
85                                                 xw.WriteNode (reader, false);
86                                 }
87                                 xw.WriteEndElement ();
88
89                                 xw.Close ();
90                                 xml_bak = sw.ToString ();
91                                 reader = null;
92                         }
93 #else // FIXME: this should be better, but somehow doesn't work.
94                         if (xml_bak == null) {
95                                 if (consumed)
96                                         throw new InvalidOperationException ("Body xml reader is already consumed");
97                                 var nss = new XmlNamespaceManager (reader.NameTable);
98                                 var nsr = reader as IXmlNamespaceResolver;
99                                 if (nsr != null)
100                                         foreach (var p in nsr.GetNamespacesInScope (XmlNamespaceScope.ExcludeXml))
101                                                 nss.AddNamespace (p.Key, p.Value);
102                                 parser_context = new XmlParserContext (nss.NameTable, nss, reader.XmlLang, reader.XmlSpace);
103                                 xml_bak = reader.ReadOuterXml ();
104                         }
105 #endif
106                         return new XmlReaderBodyWriter (xml_bak, maxBufferSize, parser_context);
107                 }
108
109                 protected override void OnWriteBodyContents (
110                         XmlDictionaryWriter writer)
111                 {
112                         if (consumed)
113                                 throw new InvalidOperationException ("Body xml reader is already consumed");
114                         if (reader == null && String.IsNullOrEmpty (xml_bak))
115                                 return;
116                         XmlReader r = xml_bak != null ? XmlReader.Create (new StringReader (xml_bak), null, parser_context) : reader;
117                         r.MoveToContent ();
118                         writer.WriteNode (r, false);
119                         if (xml_bak == null)
120                                 consumed = true;
121                 }
122         }
123 }