5 // Copyright 2007 Novell (http://www.novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Globalization;
35 using XPI = System.Xml.Linq.XProcessingInstruction;
37 namespace System.Xml.Linq
39 internal static class XUtil
41 public const string XmlnsNamespace =
42 "http://www.w3.org/2000/xmlns/";
44 public static bool ConvertToBoolean (string s)
46 return XmlConvert.ToBoolean (s.ToLower (CultureInfo.InvariantCulture));
49 public static DateTime ToDateTime (string s)
52 return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
54 return DateTime.Parse (s);
58 public static string ToString (object o)
61 throw new InvalidOperationException ("Attempt to get string from null");
63 switch (Type.GetTypeCode (o.GetType ())) {
66 case TypeCode.DateTime:
67 return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
68 case TypeCode.Decimal:
69 return ((decimal) o).ToString (CultureInfo.InvariantCulture);
71 return ((double) o).ToString ("r", CultureInfo.InvariantCulture);
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();
79 return XmlConvert.ToString ((TimeSpan) o);
80 if (o is DateTimeOffset)
81 return XmlConvert.ToString ((DateTimeOffset) o);
86 public static bool ToBoolean (object o)
88 throw new NotImplementedException ();
91 public static Nullable <bool> ToNullableBoolean (object o)
93 throw new NotImplementedException ();
96 public static IEnumerable<object> ExpandArray (object o)
101 else if (o is string)
103 else if (o is IEnumerable)
104 foreach (object obj in (IEnumerable) o)
105 foreach (object oo in ExpandArray (obj))
111 public static XNode ToNode (object o)
114 throw new ArgumentException ("Attribute node is not allowed as argument");
115 XNode n = o as XNode;
118 else if (o is string)
119 return new XText ((string) o);
121 return new XText (ToString (o));
124 public static object GetDetachedObject (XObject child)
126 return child.Owner != null ? Clone (child) : child;
129 public static object Clone (object o)
134 return new XAttribute ((XAttribute) o);
136 return new XElement ((XElement) o);
138 return new XCData ((XCData) o);
140 return new XComment ((XComment) o);
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);
148 return new XText ((XText) o);
149 throw new ArgumentException ();