2010-02-09 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XUtil.cs
1 //
2 // Authors:
3 //   Atsushi Enomoto
4 //
5 // Copyright 2007 Novell (http://www.novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26
27 using System;
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Globalization;
31 using System.IO;
32 using System.Text;
33 using System.Xml;
34
35 using XPI = System.Xml.Linq.XProcessingInstruction;
36
37 namespace System.Xml.Linq
38 {
39         internal static class XUtil
40         {
41                 public const string XmlnsNamespace =
42                         "http://www.w3.org/2000/xmlns/";
43
44                 public static bool ConvertToBoolean (string s)
45                 {
46                         return XmlConvert.ToBoolean (s.ToLower (CultureInfo.InvariantCulture));
47                 }
48
49                 public static DateTime ToDateTime (string s)
50                 {
51                         try {
52                                 return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
53                         } catch {
54                                 return DateTime.Parse (s);
55                         }
56                 }
57
58                 public static string ToString (object o)
59                 {
60                         if (o == null)
61                                 throw new InvalidOperationException ("Attempt to get string from null");
62
63                         switch (Type.GetTypeCode (o.GetType ())) {
64                         case TypeCode.String:
65                                 return (string) o;
66                         case TypeCode.DateTime:
67                                 return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
68                         case TypeCode.Double:
69                                 return ((double) o).ToString ("r");
70                         case TypeCode.Single:
71                                 return ((float) o).ToString ("r");
72                         case TypeCode.Boolean:
73                                 // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
74                                 return o.ToString().ToLower();
75                         default:
76                                 if (o is TimeSpan)
77                                         return XmlConvert.ToString ((TimeSpan) o);
78                                 if (o is DateTimeOffset)
79                                         return XmlConvert.ToString ((DateTimeOffset) o);
80                                 return o.ToString ();
81                         }
82                 }
83
84                 public static bool ToBoolean (object o)
85                 {
86                         throw new NotImplementedException ();
87                 }
88
89                 public static Nullable <bool> ToNullableBoolean (object o)
90                 {
91                         throw new NotImplementedException ();
92                 }
93
94                 public static IEnumerable ExpandArray (object o)
95                 {
96                         XNode n = o as XNode;
97                         if (n != null)
98                                 yield return n;
99                         else if (o is string)
100                                 yield return o;
101                         else if (o is IEnumerable)
102                                 foreach (object obj in (IEnumerable) o)
103                                         foreach (object oo in ExpandArray (obj))
104                                                 yield return oo;
105                         else
106                                 yield return o;
107                 }
108
109                 public static XNode ToNode (object o)
110                 {
111                         if (o is XAttribute)
112                                 throw new ArgumentException ("Attribute node is not allowed as argument");
113                         XNode n = o as XNode;
114                         if (n != null)
115                                 return n;
116                         else if (o is string)
117                                 return new XText ((string) o);
118                         else
119                                 return new XText (ToString (o));
120                 }
121
122                 public static object GetDetachedObject (XObject child)
123                 {
124                         return child.Owner != null ? Clone (child) : child;
125                 }
126
127                 public static object Clone (object o)
128                 {
129                         if (o is string)
130                                 return (string) o;
131                         if (o is XAttribute)
132                                 return new XAttribute ((XAttribute) o);
133                         if (o is XElement)
134                                 return new XElement ((XElement) o);
135                         if (o is XCData)
136                                 return new XCData ((XCData) o);
137                         if (o is XComment)
138                                 return new XComment ((XComment) o);
139                         if (o is XPI)
140                                 return new XPI ((XPI) o);
141                         if (o is XDeclaration)
142                                 return new XDeclaration ((XDeclaration) o);
143                         if (o is XDocumentType)
144                                 return new XDocumentType ((XDocumentType) o);
145                         if (o is XText)
146                                 return new XText ((XText) o);
147                         throw new ArgumentException ();
148                 }
149         }
150 }