/
XsltConvert.cs
374 lines (296 loc) · 13.8 KB
/
XsltConvert.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Schema;
using System.Xml.XPath;
namespace System.Xml.Xsl.Runtime
{
/// <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
{
//------------------------------------------------------------------------
// 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 == typeof(string))
{
return item.Value.Length != 0;
}
else if (itemType == typeof(double))
{
// (x < 0 || 0 < x) == (x != 0) && !Double.IsNaN(x)
double dbl = item.ValueAsDouble;
return dbl < 0 || 0 < dbl;
}
else
{
Debug.Assert(itemType == typeof(bool), $"Unexpected type of atomic sequence {itemType}");
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 == typeof(string))
{
return XPathConvert.StringToDouble(item.Value);
}
else if (itemType == typeof(double))
{
return item.ValueAsDouble;
}
else
{
Debug.Assert(itemType == typeof(bool), $"Unexpected type of atomic sequence {itemType}");
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(SR.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 == typeof(double))
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(typeof(decimal), 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(SR.XPath_NodeSetExpected, string.Empty);
if (item is RtfNavigator)
throw new XslTransformException(SR.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 == typeof(bool)) return XmlQueryTypeFactory.BooleanX;
if (clrType == typeof(byte)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(decimal)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(DateTime)) return XmlQueryTypeFactory.StringX;
if (clrType == typeof(double)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(short)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(int)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(long)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(IXPathNavigable)) return XmlQueryTypeFactory.NodeNotRtf;
if (clrType == typeof(sbyte)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(float)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(string)) return XmlQueryTypeFactory.StringX;
if (clrType == typeof(ushort)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(uint)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(ulong)) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(XPathNavigator[])) return XmlQueryTypeFactory.NodeSDod;
if (clrType == typeof(XPathNavigator)) return XmlQueryTypeFactory.NodeNotRtf;
if (clrType == typeof(XPathNodeIterator)) return XmlQueryTypeFactory.NodeSDod;
if (clrType.IsEnum) return XmlQueryTypeFactory.DoubleX;
if (clrType == typeof(void)) return XmlQueryTypeFactory.Empty;
return XmlQueryTypeFactory.ItemS;
}
}
}