using System;
using System.Collections;
using System.Collections.Generic;
+using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
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)
yield return o;
}
- // FIXME: it will be removed. It makes attribute processing incorrect.
- public static IEnumerable<XNode> ToNodes (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)
- yield return n;
+ return n;
else if (o is string)
- yield return new XText ((string) o);
- else if (o is IEnumerable)
- foreach (object obj in (IEnumerable) o)
- foreach (XNode nn in ToNodes (obj))
- yield return nn;
+ return new XText ((string) o);
else
- yield return new XText (o.ToString ());
+ return new XText (ToString (o));
}
- public static object Clone (object 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 ();
}
-
- // FIXME: it will be removed. Shrinking just arguments is insufficient.
- 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;
- }
}
}