* SqlXml.cs (CreateReader): Better use
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlXml.cs
1 //
2 // System.Data.SqlTypes.SqlXml
3 //
4 // Author:
5 //      Umadevi S (sumadevi@novell.com)
6 //      Veerapuram Varadhan  (vvaradhan@novell.com>)
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.IO;
32 using System.Xml;
33 using System.Xml.Schema;
34 using System.Globalization;
35 using System.Threading;
36 using System.Xml.Serialization;
37 using System.Text;
38
39 #if NET_2_0
40
41 namespace System.Data.SqlTypes
42 {
43         [SerializableAttribute]
44         [XmlSchemaProvider ("GetXsdType")]
45         public sealed class SqlXml : INullable, IXmlSerializable
46         {
47                 bool notNull;
48                 string xmlValue;
49                 
50                 public SqlXml ()
51                 {
52                         notNull = false;
53                         xmlValue = null;
54                 }
55
56                 public SqlXml (Stream value)
57                 {
58                         if (value == null) {
59                                 notNull = false;
60                                 xmlValue = null;
61                         } else {
62                                 int len = (int) value.Length;
63                                 
64                                 if (len < 1) {
65                                         xmlValue = String.Empty;
66                                 } else {
67                                         int bufSize = 8192;
68                                         StringBuilder sb = new StringBuilder (len);
69                                 
70                                         value.Position = 0;
71                                         // Now read value into a byte buffer.
72                                         byte [] bytes = null;
73                                 
74                                         if (len < bufSize)
75                                                 bufSize = len;
76                                         bytes = new byte [bufSize];
77
78                                         while (len > 0) {
79                                                 // Read may return anything from 0 to bufSize.
80                                                 int n = value.Read(bytes, 0, bufSize);
81                                                 sb.Append (Encoding.Unicode.GetString (bytes, 0, n));
82                                         
83                                                 // The end of the file is reached.
84                                                 if (n==0)
85                                                     break;
86                                                 len -= n;
87                                         }
88                                         xmlValue = sb.ToString ();
89                                 }
90                                 notNull = true;
91                         }
92                 }
93
94                 public SqlXml (XmlReader value)
95                 {
96                         if (value == null) {
97                                 notNull = false;
98                                 xmlValue = null;
99                         } else {
100                                 if (value.Read ()) {
101                                         value.MoveToContent ();
102                                         xmlValue = value.ReadOuterXml();
103                                 } else 
104                                         xmlValue = String.Empty;
105                                 notNull = true;
106                         }
107                 }
108
109                 public bool IsNull {
110                         get { return !notNull; }
111                 }
112
113                 public static SqlXml Null {
114                         get {
115                                 return new SqlXml ();
116                         }
117                 }
118
119                 public string Value {
120                         get {
121                                 if (notNull)
122                                         return xmlValue;
123                                 throw new SqlNullValueException ();
124                         }
125                 }
126
127                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
128                 {
129                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("anyType", "http://www.w3.org/2001/XMLSchema");
130                         return qualifiedName;
131                 }
132
133                 public XmlReader CreateReader ()
134                 {
135                         if (notNull) {
136                                 XmlReaderSettings xs = new XmlReaderSettings ();
137                                 xs.ConformanceLevel = ConformanceLevel.Fragment;
138                                 return XmlTextReader.Create (new StringReader (xmlValue), xs);
139                         } else
140                                 throw new SqlNullValueException (); 
141                 }
142
143                 [MonoTODO]
144                 XmlSchema IXmlSerializable.GetSchema ()
145                 {
146                         throw new NotImplementedException ();
147                 }
148                 
149                 [MonoTODO]
150                 void IXmlSerializable.ReadXml (XmlReader r)
151                 {
152                         throw new NotImplementedException ();
153                 }
154                 
155                 [MonoTODO]
156                 void IXmlSerializable.WriteXml (XmlWriter writer) 
157                 {
158                         throw new NotImplementedException ();
159                 }
160         }
161 }
162
163 #endif