Merge pull request #799 from kebby/master
[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.Decimal:
69                                 return ((decimal) o).ToString (CultureInfo.InvariantCulture);
70                         case TypeCode.Double:
71                                 return ((double) o).ToString ("r", CultureInfo.InvariantCulture);
72                         case TypeCode.Single:
73                                 return ((float) o).ToString ("r", CultureInfo.InvariantCulture);
74                         case TypeCode.Boolean:
75                                 // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
76                                 return o.ToString().ToLower();
77                         default:
78                                 if (o is TimeSpan)
79                                         return XmlConvert.ToString ((TimeSpan) o);
80                                 if (o is DateTimeOffset)
81                                         return XmlConvert.ToString ((DateTimeOffset) o);
82                                 return o.ToString ();
83                         }
84                 }
85
86                 public static bool ToBoolean (object o)
87                 {
88                         throw new NotImplementedException ();
89                 }
90
91                 public static Nullable <bool> ToNullableBoolean (object o)
92                 {
93                         throw new NotImplementedException ();
94                 }
95
96                 public static IEnumerable<object> ExpandArray (object o)
97                 {
98                         XNode n = o as XNode;
99                         if (n != null)
100                                 yield return n;
101                         else if (o is string)
102                                 yield return o;
103                         else if (o is IEnumerable)
104                                 foreach (object obj in (IEnumerable) o)
105                                         foreach (object oo in ExpandArray (obj))
106                                                 yield return oo;
107                         else
108                                 yield return o;
109                 }
110
111                 public static XNode ToNode (object o)
112                 {
113                         if (o is XAttribute)
114                                 throw new ArgumentException ("Attribute node is not allowed as argument");
115                         XNode n = o as XNode;
116                         if (n != null)
117                                 return n;
118                         else if (o is string)
119                                 return new XText ((string) o);
120                         else
121                                 return new XText (ToString (o));
122                 }
123
124                 public static object GetDetachedObject (XObject child)
125                 {
126                         return child.Owner != null ? Clone (child) : child;
127                 }
128
129                 public static object Clone (object o)
130                 {
131                         if (o is string)
132                                 return (string) o;
133                         if (o is XAttribute)
134                                 return new XAttribute ((XAttribute) o);
135                         if (o is XElement)
136                                 return new XElement ((XElement) o);
137                         if (o is XCData)
138                                 return new XCData ((XCData) o);
139                         if (o is XComment)
140                                 return new XComment ((XComment) o);
141                         if (o is XPI)
142                                 return new XPI ((XPI) o);
143                         if (o is XDeclaration)
144                                 return new XDeclaration ((XDeclaration) o);
145                         if (o is XDocumentType)
146                                 return new XDocumentType ((XDocumentType) o);
147                         if (o is XText)
148                                 return new XText ((XText) o);
149                         throw new ArgumentException ();
150                 }
151         }
152 }