ad7f029edc7039eeeac76a55ec4a5710746eb5d4
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / Runtime / XmlILStorageConverter.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XmlILStorageConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 using System;
8 using System.Collections.Generic;
9 using System.IO;
10 using System.Xml;
11 using System.Xml.XPath;
12 using System.Xml.Schema;
13 using System.Xml.Xsl;
14 using System.ComponentModel;
15
16 namespace System.Xml.Xsl.Runtime {
17
18     /// <summary>
19     /// This is a simple convenience wrapper internal class that contains static helper methods that get a value
20     /// converter from XmlQueryRuntime and use it convert among several physical Clr representations for
21     /// the same logical Xml type.  For example, an external function might have an argument typed as
22     /// xs:integer, with Clr type Decimal.  Since ILGen stores xs:integer as Clr type Int64 instead of
23     /// Decimal, a conversion to the desired storage type must take place.
24     /// </summary>
25     [EditorBrowsable(EditorBrowsableState.Never)]
26     public static class XmlILStorageConverter {
27
28         //-----------------------------------------------
29         // ToAtomicValue
30         //-----------------------------------------------
31
32         public static XmlAtomicValue StringToAtomicValue(string value, int index, XmlQueryRuntime runtime) {
33             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
34         }
35
36         public static XmlAtomicValue DecimalToAtomicValue(decimal value, int index, XmlQueryRuntime runtime) {
37             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
38         }
39
40         public static XmlAtomicValue Int64ToAtomicValue(long value, int index, XmlQueryRuntime runtime) {
41             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
42         }
43
44         public static XmlAtomicValue Int32ToAtomicValue(int value, int index, XmlQueryRuntime runtime) {
45             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
46         }
47
48         public static XmlAtomicValue BooleanToAtomicValue(bool value, int index, XmlQueryRuntime runtime) {
49             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
50         }
51
52         public static XmlAtomicValue DoubleToAtomicValue(double value, int index, XmlQueryRuntime runtime) {
53             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
54         }
55
56         public static XmlAtomicValue SingleToAtomicValue(float value, int index, XmlQueryRuntime runtime) {
57             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
58         }
59
60         public static XmlAtomicValue DateTimeToAtomicValue(DateTime value, int index, XmlQueryRuntime runtime) {
61             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
62         }
63
64         public static XmlAtomicValue XmlQualifiedNameToAtomicValue(XmlQualifiedName value, int index, XmlQueryRuntime runtime) {
65             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
66         }
67
68         public static XmlAtomicValue TimeSpanToAtomicValue(TimeSpan value, int index, XmlQueryRuntime runtime) {
69             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
70         }
71
72         public static XmlAtomicValue BytesToAtomicValue(byte[] value, int index, XmlQueryRuntime runtime) {
73             return new XmlAtomicValue(runtime.GetXmlType(index).SchemaType, value);
74         }
75
76         public static IList<XPathItem> NavigatorsToItems(IList<XPathNavigator> listNavigators) {
77             // Check to see if the navigator cache implements IList<XPathItem>
78             IList<XPathItem> listItems = listNavigators as IList<XPathItem>;
79             if (listItems != null)
80                 return listItems;
81
82             // Create XmlQueryNodeSequence, which does implement IList<XPathItem>
83             return new XmlQueryNodeSequence(listNavigators);
84         }
85
86         public static IList<XPathNavigator> ItemsToNavigators(IList<XPathItem> listItems) {
87             // Check to see if the navigator cache implements IList<XPathNavigator>
88             IList<XPathNavigator> listNavs = listItems as IList<XPathNavigator>;
89             if (listNavs != null)
90                 return listNavs;
91
92             // Create XmlQueryNodeSequence, which does implement IList<XPathNavigator>
93             XmlQueryNodeSequence seq = new XmlQueryNodeSequence(listItems.Count);
94             for (int i = 0; i < listItems.Count; i++)
95                 seq.Add((XPathNavigator) listItems[i]);
96
97             return seq;
98         }
99     }
100 }