1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
//------------------------------------------------------------------------------
// <copyright file="XsltConvert.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Schema;
using System.Diagnostics;
using System.ComponentModel;
namespace System.Xml.Xsl.Runtime {
using Res = System.Xml.Utils.Res;
/// <summary>
/// Contains conversion routines used by Xslt. These conversions fall into several categories:
/// 1. Internal type to internal type: These are conversions from one of the five Xslt types to another
/// of the five types.
/// 2. External type to internal type: These are conversions from any of the Xsd types to one of the five
/// Xslt types.
/// 3. Internal type to external type: These are conversions from one of the five Xslt types to any of
/// of the Xsd types.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class XsltConvert {
internal static readonly Type BooleanType = typeof(bool);
internal static readonly Type ByteArrayType = typeof(byte[]);
internal static readonly Type ByteType = typeof(byte);
internal static readonly Type DateTimeType = typeof(DateTime);
internal static readonly Type DecimalType = typeof(decimal);
internal static readonly Type DoubleType = typeof(double);
internal static readonly Type ICollectionType = typeof(ICollection);
internal static readonly Type IEnumerableType = typeof(IEnumerable);
internal static readonly Type IListType = typeof(IList);
internal static readonly Type Int16Type = typeof(short);
internal static readonly Type Int32Type = typeof(int);
internal static readonly Type Int64Type = typeof(long);
internal static readonly Type IXPathNavigableType = typeof(IXPathNavigable);
internal static readonly Type ObjectType = typeof(object);
internal static readonly Type SByteType = typeof(sbyte);
internal static readonly Type SingleType = typeof(float);
internal static readonly Type StringType = typeof(string);
internal static readonly Type TimeSpanType = typeof(TimeSpan);
internal static readonly Type UInt16Type = typeof(ushort);
internal static readonly Type UInt32Type = typeof(uint);
internal static readonly Type UInt64Type = typeof(ulong);
internal static readonly Type UriType = typeof(Uri);
internal static readonly Type VoidType = typeof(void);
internal static readonly Type XmlAtomicValueType = typeof(XmlAtomicValue);
internal static readonly Type XmlQualifiedNameType = typeof(XmlQualifiedName);
internal static readonly Type XPathItemType = typeof(XPathItem);
internal static readonly Type XPathNavigatorArrayType = typeof(XPathNavigator[]);
internal static readonly Type XPathNavigatorType = typeof(XPathNavigator);
internal static readonly Type XPathNodeIteratorType = typeof(XPathNodeIterator);
//------------------------------------------------------------------------
// ToBoolean (internal type to internal type)
//------------------------------------------------------------------------
public static bool ToBoolean(XPathItem item) {
XsltLibrary.CheckXsltValue(item);
if (item.IsNode)
return true;
Type itemType = item.ValueType;
if (itemType == StringType) {
return item.Value.Length != 0;
}
else if (itemType == DoubleType) {
// (x < 0 || 0 < x) == (x != 0) && !Double.IsNaN(x)
double dbl = item.ValueAsDouble;
return dbl < 0 || 0 < dbl;
}
else {
Debug.Assert(itemType == BooleanType, "Unexpected type of atomic sequence " + itemType.ToString());
return item.ValueAsBoolean;
}
}
public static bool ToBoolean(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 0)
return false;
return ToBoolean(listItems[0]);
}
//------------------------------------------------------------------------
// ToDouble (internal type to internal type)
//------------------------------------------------------------------------
public static double ToDouble(string value) {
return XPathConvert.StringToDouble(value);
}
public static double ToDouble(XPathItem item) {
XsltLibrary.CheckXsltValue(item);
if (item.IsNode)
return XPathConvert.StringToDouble(item.Value);
Type itemType = item.ValueType;
if (itemType == StringType) {
return XPathConvert.StringToDouble(item.Value);
}
else if (itemType == DoubleType) {
return item.ValueAsDouble;
}
else {
Debug.Assert(itemType == BooleanType, "Unexpected type of atomic sequence " + itemType.ToString());
return item.ValueAsBoolean ? 1d : 0d;
}
}
public static double ToDouble(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 0)
return Double.NaN;
return ToDouble(listItems[0]);
}
//------------------------------------------------------------------------
// ToNode (internal type to internal type)
//------------------------------------------------------------------------
public static XPathNavigator ToNode(XPathItem item) {
XsltLibrary.CheckXsltValue(item);
if (!item.IsNode) {
// Create Navigator over text node containing string value of item
XPathDocument doc = new XPathDocument();
XmlRawWriter writer = doc.LoadFromWriter(XPathDocument.LoadFlags.AtomizeNames, string.Empty);
writer.WriteString(ToString(item));
writer.Close();
return doc.CreateNavigator();
}
RtfNavigator rtf = item as RtfNavigator;
if (rtf != null)
return rtf.ToNavigator();
return (XPathNavigator) item;
}
public static XPathNavigator ToNode(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 1)
return ToNode(listItems[0]);
throw new XslTransformException(Res.Xslt_NodeSetNotNode, string.Empty);
}
//------------------------------------------------------------------------
// ToNodes (internal type to internal type)
//------------------------------------------------------------------------
public static IList<XPathNavigator> ToNodeSet(XPathItem item) {
return new XmlQueryNodeSequence(ToNode(item));
}
public static IList<XPathNavigator> ToNodeSet(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 1)
return new XmlQueryNodeSequence(ToNode(listItems[0]));
return XmlILStorageConverter.ItemsToNavigators(listItems);
}
//------------------------------------------------------------------------
// ToString (internal type to internal type)
//------------------------------------------------------------------------
public static string ToString(double value) {
return XPathConvert.DoubleToString(value);
}
public static string ToString(XPathItem item) {
XsltLibrary.CheckXsltValue(item);
// Use XPath 1.0 rules to convert double to string
if (!item.IsNode && item.ValueType == DoubleType)
return XPathConvert.DoubleToString(item.ValueAsDouble);
return item.Value;
}
public static string ToString(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 0)
return string.Empty;
return ToString(listItems[0]);
}
//------------------------------------------------------------------------
// External type to internal type
//------------------------------------------------------------------------
public static string ToString(DateTime value) {
return (new XsdDateTime(value, XsdDateTimeFlags.DateTime)).ToString();
}
public static double ToDouble(decimal value) {
return (double) value;
}
public static double ToDouble(int value) {
return (double) value;
}
public static double ToDouble(long value) {
return (double) value;
}
//------------------------------------------------------------------------
// Internal type to external type
//------------------------------------------------------------------------
public static decimal ToDecimal(double value) {
checked { return (decimal) value; }
}
public static int ToInt(double value) {
checked { return (int) value; }
}
public static long ToLong(double value) {
checked { return (long) value; }
}
public static DateTime ToDateTime(string value) {
return (DateTime)(new XsdDateTime(value, XsdDateTimeFlags.AllXsd));
}
//------------------------------------------------------------------------
// External type to external type
//------------------------------------------------------------------------
internal static XmlAtomicValue ConvertToType(XmlAtomicValue value, XmlQueryType destinationType) {
Debug.Assert(destinationType.IsStrict && destinationType.IsAtomicValue, "Can only convert to strict atomic type.");
// This conversion matrix should match the one in XmlILVisitor.GetXsltConvertMethod
switch (destinationType.TypeCode) {
case XmlTypeCode.Boolean:
switch (value.XmlType.TypeCode) {
case XmlTypeCode.Boolean:
case XmlTypeCode.Double:
case XmlTypeCode.String:
return new XmlAtomicValue(destinationType.SchemaType, ToBoolean(value));
}
break;
case XmlTypeCode.DateTime:
if (value.XmlType.TypeCode == XmlTypeCode.String)
return new XmlAtomicValue(destinationType.SchemaType, ToDateTime(value.Value));
break;
case XmlTypeCode.Decimal:
if (value.XmlType.TypeCode == XmlTypeCode.Double)
return new XmlAtomicValue(destinationType.SchemaType, ToDecimal(value.ValueAsDouble));
break;
case XmlTypeCode.Double:
switch (value.XmlType.TypeCode) {
case XmlTypeCode.Boolean:
case XmlTypeCode.Double:
case XmlTypeCode.String:
return new XmlAtomicValue(destinationType.SchemaType, ToDouble(value));
case XmlTypeCode.Decimal:
return new XmlAtomicValue(destinationType.SchemaType, ToDouble((decimal) value.ValueAs(DecimalType, null)));
case XmlTypeCode.Int:
case XmlTypeCode.Long:
return new XmlAtomicValue(destinationType.SchemaType, ToDouble(value.ValueAsLong));
}
break;
case XmlTypeCode.Int:
case XmlTypeCode.Long:
if (value.XmlType.TypeCode == XmlTypeCode.Double)
return new XmlAtomicValue(destinationType.SchemaType, ToLong(value.ValueAsDouble));
break;
case XmlTypeCode.String:
switch (value.XmlType.TypeCode) {
case XmlTypeCode.Boolean:
case XmlTypeCode.Double:
case XmlTypeCode.String:
return new XmlAtomicValue(destinationType.SchemaType, ToString(value));
case XmlTypeCode.DateTime:
return new XmlAtomicValue(destinationType.SchemaType, ToString(value.ValueAsDateTime));
}
break;
}
Debug.Fail("Conversion from " + value.XmlType.QualifiedName.Name + " to " + destinationType + " is not supported.");
return value;
}
//------------------------------------------------------------------------
// EnsureXXX methods (TreatAs)
//------------------------------------------------------------------------
public static IList<XPathNavigator> EnsureNodeSet(IList<XPathItem> listItems) {
XsltLibrary.CheckXsltValue(listItems);
if (listItems.Count == 1) {
XPathItem item = listItems[0];
if (!item.IsNode)
throw new XslTransformException(Res.XPath_NodeSetExpected, string.Empty);
if (item is RtfNavigator)
throw new XslTransformException(Res.XPath_RtfInPathExpr, string.Empty);
}
return XmlILStorageConverter.ItemsToNavigators(listItems);
}
//------------------------------------------------------------------------
// InferXsltType
//------------------------------------------------------------------------
/// <summary>
/// Infer one of the Xslt types from "clrType" -- Boolean, Double, String, Node, Node*, Item*.
/// </summary>
internal static XmlQueryType InferXsltType(Type clrType) {
if (clrType == BooleanType) return XmlQueryTypeFactory.BooleanX;
if (clrType == ByteType) return XmlQueryTypeFactory.DoubleX;
if (clrType == DecimalType) return XmlQueryTypeFactory.DoubleX;
if (clrType == DateTimeType) return XmlQueryTypeFactory.StringX;
if (clrType == DoubleType) return XmlQueryTypeFactory.DoubleX;
if (clrType == Int16Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == Int32Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == Int64Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == IXPathNavigableType) return XmlQueryTypeFactory.NodeNotRtf;
if (clrType == SByteType) return XmlQueryTypeFactory.DoubleX;
if (clrType == SingleType) return XmlQueryTypeFactory.DoubleX;
if (clrType == StringType) return XmlQueryTypeFactory.StringX;
if (clrType == UInt16Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == UInt32Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == UInt64Type) return XmlQueryTypeFactory.DoubleX;
if (clrType == XPathNavigatorArrayType) return XmlQueryTypeFactory.NodeSDod;
if (clrType == XPathNavigatorType) return XmlQueryTypeFactory.NodeNotRtf;
if (clrType == XPathNodeIteratorType) return XmlQueryTypeFactory.NodeSDod;
if (clrType.IsEnum) return XmlQueryTypeFactory.DoubleX;
if (clrType == VoidType) return XmlQueryTypeFactory.Empty;
return XmlQueryTypeFactory.ItemS;
}
}
}
|