Merge pull request #554 from deplinenoise/ppc_fixes
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XUtil.cs
index 178dbf2c40ac7dabc18eb71cbf29d7111c852ed1..69cb484b23ed49c47b3e3b6a1895c2fec40dcb51 100644 (file)
@@ -27,6 +27,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Globalization;
 using System.IO;
 using System.Text;
 using System.Xml;
@@ -40,14 +41,46 @@ namespace System.Xml.Linq
                public const string XmlnsNamespace =
                        "http://www.w3.org/2000/xmlns/";
 
-               // FIXME: implement
+               public static bool ConvertToBoolean (string s)
+               {
+                       return XmlConvert.ToBoolean (s.ToLower (CultureInfo.InvariantCulture));
+               }
+
+               public static DateTime ToDateTime (string s)
+               {
+                       try {
+                               return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
+                       } catch {
+                               return DateTime.Parse (s);
+                       }
+               }
+
                public static string ToString (object o)
                {
                        if (o == null)
                                throw new InvalidOperationException ("Attempt to get string from null");
-                       if (o is string)
+
+                       switch (Type.GetTypeCode (o.GetType ())) {
+                       case TypeCode.String:
                                return (string) o;
-                       return o.ToString ();
+                       case TypeCode.DateTime:
+                               return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
+                       case TypeCode.Decimal:
+                               return ((decimal) o).ToString (CultureInfo.InvariantCulture);
+                       case TypeCode.Double:
+                               return ((double) o).ToString ("r", CultureInfo.InvariantCulture);
+                       case TypeCode.Single:
+                               return ((float) o).ToString ("r", CultureInfo.InvariantCulture);
+                       case TypeCode.Boolean:
+                               // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
+                               return o.ToString().ToLower();
+                       default:
+                               if (o is TimeSpan)
+                                       return XmlConvert.ToString ((TimeSpan) o);
+                               if (o is DateTimeOffset)
+                                       return XmlConvert.ToString ((DateTimeOffset) o);
+                               return o.ToString ();
+                       }
                }
 
                public static bool ToBoolean (object o)
@@ -60,62 +93,60 @@ namespace System.Xml.Linq
                        throw new NotImplementedException ();
                }
 
-               public static IEnumerable<XNode> ToNodes (object o)
+               public static IEnumerable ExpandArray (object o)
                {
                        XNode n = o as XNode;
                        if (n != null)
                                yield return n;
                        else if (o is string)
-                               yield return new XText ((string) o);
+                               yield return o;
                        else if (o is IEnumerable)
                                foreach (object obj in (IEnumerable) o)
-                                       foreach (XNode nn in ToNodes (obj))
-                                               yield return nn;
+                                       foreach (object oo in ExpandArray (obj))
+                                               yield return oo;
                        else
-                               yield return new XText (o.ToString ());
+                               yield return o;
                }
 
-               public static object Clone (object o)
+               public static XNode ToNode (object o)
+               {
+                       if (o is XAttribute)
+                               throw new ArgumentException ("Attribute node is not allowed as argument");
+                       XNode n = o as XNode;
+                       if (n != null)
+                               return n;
+                       else if (o is string)
+                               return new XText ((string) o);
+                       else
+                               return new XText (ToString (o));
+               }
+
+               public static object GetDetachedObject (XObject child)
+               {
+                       return child.Owner != null ? Clone (child) : child;
+               }
 
+               public static object Clone (object o)
                {
                        if (o is string)
                                return (string) o;
+                       if (o is XAttribute)
+                               return new XAttribute ((XAttribute) o);
                        if (o is XElement)
                                return new XElement ((XElement) o);
                        if (o is XCData)
-                               return new XCData (((XCData) o).Value);
+                               return new XCData ((XCData) o);
                        if (o is XComment)
-                               return new XComment (((XComment) o).Value);
-                       XPI pi = o as XPI;
-                       if (pi != null)
-                               return new XPI (pi.Target, pi.Data);
-                       XDeclaration xd = o as XDeclaration;
-                       if (xd != null)
-                               return new XDeclaration (xd.Version, xd.Encoding, xd.Standalone);
-                       XDocumentType dtd = o as XDocumentType;
-                       if (dtd != null)
-                               throw new NotImplementedException ();
+                               return new XComment ((XComment) o);
+                       if (o is XPI)
+                               return new XPI ((XPI) o);
+                       if (o is XDeclaration)
+                               return new XDeclaration ((XDeclaration) o);
+                       if (o is XDocumentType)
+                               return new XDocumentType ((XDocumentType) o);
+                       if (o is XText)
+                               return new XText ((XText) o);
                        throw new ArgumentException ();
                }
-
-               public static IEnumerable<object> ShrinkArray (params object [] content)
-               {
-                       if (content == null || content.Length == 0)
-                               yield break;
-                       string prev = null;
-                       foreach (object o in content) {
-                               if (o is XNode) {
-                                       if (prev != null) {
-                                               yield return prev;
-                                               prev = null;
-                                       }
-                                       yield return o;
-                               } else {
-                                       prev += o;
-                               }
-                       }
-                       if (prev != null)
-                               yield return prev;
-               }
        }
 }