2010-01-12 Atsushi Enomoto <atsushi@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.IO;
31 using System.Text;
32 using System.Xml;
33
34 using XPI = System.Xml.Linq.XProcessingInstruction;
35
36 namespace System.Xml.Linq
37 {
38         internal static class XUtil
39         {
40                 public const string XmlnsNamespace =
41                         "http://www.w3.org/2000/xmlns/";
42
43                 public static bool ConvertToBoolean (string s)
44                 {
45                         return XmlConvert.ToBoolean (s.ToLowerInvariant ());
46                 }
47
48                 public static DateTime ToDateTime (string s)
49                 {
50                         try {
51                                 return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
52                         } catch {
53                                 return DateTime.Parse (s);
54                         }
55                 }
56
57                 public static string ToString (object o)
58                 {
59                         if (o == null)
60                                 throw new InvalidOperationException ("Attempt to get string from null");
61
62                         switch (Type.GetTypeCode (o.GetType ())) {
63                         case TypeCode.String:
64                                 return (string) o;
65                         case TypeCode.DateTime:
66                                 return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
67                         case TypeCode.Boolean:
68                                 // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
69                                 return o.ToString().ToLower();
70                         default:
71                                 return o.ToString ();
72                         }
73                 }
74
75                 public static bool ToBoolean (object o)
76                 {
77                         throw new NotImplementedException ();
78                 }
79
80                 public static Nullable <bool> ToNullableBoolean (object o)
81                 {
82                         throw new NotImplementedException ();
83                 }
84
85                 public static IEnumerable ExpandArray (object o)
86                 {
87                         XNode n = o as XNode;
88                         if (n != null)
89                                 yield return n;
90                         else if (o is string)
91                                 yield return o;
92                         else if (o is IEnumerable)
93                                 foreach (object obj in (IEnumerable) o)
94                                         foreach (object oo in ExpandArray (obj))
95                                                 yield return oo;
96                         else
97                                 yield return o;
98                 }
99
100                 public static XNode ToNode (object o)
101                 {
102                         if (o is XAttribute)
103                                 throw new ArgumentException ("Attribute node is not allowed as argument");
104                         XNode n = o as XNode;
105                         if (n != null)
106                                 return n;
107                         else if (o is string)
108                                 return new XText ((string) o);
109                         else
110                                 return new XText (o.ToString ());
111                 }
112
113                 public static object GetDetachedObject (XObject child)
114                 {
115                         return child.Owner != null ? Clone (child) : child;
116                 }
117
118                 public static object Clone (object o)
119                 {
120                         if (o is string)
121                                 return (string) o;
122                         if (o is XAttribute)
123                                 return new XAttribute ((XAttribute) o);
124                         if (o is XElement)
125                                 return new XElement ((XElement) o);
126                         if (o is XCData)
127                                 return new XCData ((XCData) o);
128                         if (o is XComment)
129                                 return new XComment ((XComment) o);
130                         if (o is XPI)
131                                 return new XPI ((XPI) o);
132                         if (o is XDeclaration)
133                                 return new XDeclaration ((XDeclaration) o);
134                         if (o is XDocumentType)
135                                 return new XDocumentType ((XDocumentType) o);
136                         if (o is XText)
137                                 return new XText ((XText) o);
138                         throw new ArgumentException ();
139                 }
140         }
141 }