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}"); return(item.ValueAsBoolean ? 1d : 0d); } }
//------------------------------------------------------------------------ // 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}"); return(item.ValueAsBoolean); } }
public static bool ToBoolean(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(false); } return(ToBoolean(listItems[0])); }
public static string ToString(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(string.Empty); } return(ToString(listItems[0])); }
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)); }
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); }
public static double ToDouble(IList <XPathItem> listItems) { XsltLibrary.CheckXsltValue(listItems); if (listItems.Count == 0) { return(double.NaN); } return(ToDouble(listItems[0])); }
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); }
//------------------------------------------------ // Msxml Extension Functions //------------------------------------------------ public static double MSNumber(IList <XPathItem> value) { XsltLibrary.CheckXsltValue(value); if (value.Count == 0) { return(Double.NaN); } XPathItem item = value[0]; string stringValue; if (item.IsNode) { stringValue = item.Value; } else { Type itemType = item.ValueType; if (itemType == XsltConvert.StringType) { stringValue = item.Value; } else if (itemType == XsltConvert.DoubleType) { return(item.ValueAsDouble); } else { Debug.Assert(itemType == XsltConvert.BooleanType, "Unexpected type of atomic value " + itemType.ToString()); return(item.ValueAsBoolean ? 1d : 0d); } } Debug.Assert(stringValue != null); double d; if (XmlConvert.TryToDouble(stringValue, out d) != null) { d = double.NaN; } return(d); }
//------------------------------------------------------------------------ // 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)); }
//------------------------------------------------ // EXslt Functions //------------------------------------------------ public static string EXslObjectType(IList <XPathItem> value) { if (value.Count != 1) { XsltLibrary.CheckXsltValue(value); return("node-set"); } XPathItem item = value[0]; if (item is RtfNavigator) { return("RTF"); } else if (item.IsNode) { Debug.Assert(item is XPathNavigator); return("node-set"); } object o = item.TypedValue; if (o is string) { return("string"); } else if (o is double) { return("number"); } else if (o is bool) { return("boolean"); } else { Debug.Fail("Unexpected type: " + o.GetType().ToString()); return("external"); } }
//------------------------------------------------------------------------ // 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); }
//----------------------------------------------- // Constructors //----------------------------------------------- /// <summary> /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately). /// </summary> internal XmlQueryRuntime(XmlILCommand cmd, object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt) { Debug.Assert(cmd != null, "Command object must be non-null"); string[] names = cmd.Names; Int32Pair[] filters = cmd.Filters; WhitespaceRuleLookup wsRules; int i; this.cmd = cmd; // Early-Bound Library Objects wsRules = (cmd.WhitespaceRules.Count != 0) ? new WhitespaceRuleLookup(cmd.WhitespaceRules) : null; this.ctxt = new XmlQueryContext(this, defaultDataSource, dataSources, argList, wsRules); this.xsltLib = null; this.earlyInfo = cmd.EarlyBound; this.earlyObjects = (this.earlyInfo != null) ? new object[earlyInfo.Length] : null; // Global variables and parameters this.globalNames = cmd.GlobalNames; this.globalValues = (this.globalNames != null) ? new object[this.globalNames.Length] : null; // Names this.nameTableQuery = this.ctxt.QueryNameTable; this.atomizedNames = null; if (names != null) { // Atomize all names in "nameTableQuery". Use names from the default data source's // name table when possible. XmlNameTable nameTableDefault = ctxt.DefaultNameTable; this.atomizedNames = new string[names.Length]; if (nameTableDefault != this.nameTableQuery && nameTableDefault != null) { // Ensure that atomized names from the default data source are added to the // name table used in this query for (i = 0; i < names.Length; i++) { string name = nameTableDefault.Get(names[i]); this.atomizedNames[i] = this.nameTableQuery.Add(name ?? names[i]); } } else { // Enter names into nametable used in this query for (i = 0; i < names.Length; i++) this.atomizedNames[i] = this.nameTableQuery.Add(names[i]); } } // Name filters this.filters = null; if (filters != null) { // Construct name filters. Each pair of integers in the filters[] array specifies the // (localName, namespaceUri) of the NameFilter to be created. this.filters = new XmlNavigatorFilter[filters.Length]; for (i = 0; i < filters.Length; i++) this.filters[i] = XmlNavNameFilter.Create(this.atomizedNames[filters[i].Left], this.atomizedNames[filters[i].Right]); } // Prefix maping lists this.prefixMappingsList = cmd.PrefixMappingsList; // Xml types this.types = cmd.Types; // Xml collations this.collations = cmd.Collations; // Document ordering this.docOrderCmp = new DocumentOrderComparer(); // Indexes this.indexes = null; // Output construction this.stkOutput = new Stack<XmlQueryOutput>(16); this.output = new XmlQueryOutput(this, seqWrt); }
//----------------------------------------------- // Constructors //----------------------------------------------- /// <summary> /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately). /// </summary> internal XmlQueryRuntime(XmlQueryStaticData data, object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt) { Debug.Assert(data != null); string[] names = data.Names; Int32Pair[] filters = data.Filters; WhitespaceRuleLookup wsRules; int i; // Early-Bound Library Objects wsRules = (data.WhitespaceRules != null && data.WhitespaceRules.Count != 0) ? new WhitespaceRuleLookup(data.WhitespaceRules) : null; _ctxt = new XmlQueryContext(this, defaultDataSource, dataSources, argList, wsRules); _xsltLib = null; _earlyInfo = data.EarlyBound; _earlyObjects = (_earlyInfo != null) ? new object[_earlyInfo.Length] : null; // Global variables and parameters _globalNames = data.GlobalNames; _globalValues = (_globalNames != null) ? new object[_globalNames.Length] : null; // Names _nameTableQuery = _ctxt.QueryNameTable; _atomizedNames = null; if (names != null) { // Atomize all names in "nameTableQuery". Use names from the default data source's // name table when possible. XmlNameTable nameTableDefault = _ctxt.DefaultNameTable; _atomizedNames = new string[names.Length]; if (nameTableDefault != _nameTableQuery && nameTableDefault != null) { // Ensure that atomized names from the default data source are added to the // name table used in this query for (i = 0; i < names.Length; i++) { string name = nameTableDefault.Get(names[i]); _atomizedNames[i] = _nameTableQuery.Add(name ?? names[i]); } } else { // Enter names into nametable used in this query for (i = 0; i < names.Length; i++) { _atomizedNames[i] = _nameTableQuery.Add(names[i]); } } } // Name filters _filters = null; if (filters != null) { // Construct name filters. Each pair of integers in the filters[] array specifies the // (localName, namespaceUri) of the NameFilter to be created. _filters = new XmlNavigatorFilter[filters.Length]; for (i = 0; i < filters.Length; i++) { _filters[i] = XmlNavNameFilter.Create(_atomizedNames[filters[i].Left], _atomizedNames[filters[i].Right]); } } // Prefix maping lists _prefixMappingsList = data.PrefixMappingsList; // Xml types _types = data.Types; // Xml collations _collations = data.Collations; // Document ordering _docOrderCmp = new DocumentOrderComparer(); // Indexes _indexes = null; // Output construction _stkOutput = new Stack <XmlQueryOutput>(16); _output = new XmlQueryOutput(this, seqWrt); }
//----------------------------------------------- // Constructors //----------------------------------------------- /// <summary> /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately). /// </summary> internal XmlQueryRuntime(XmlQueryStaticData data, object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt) { Debug.Assert(data != null); string[] names = data.Names; Int32Pair[] filters = data.Filters; WhitespaceRuleLookup wsRules; int i; // Early-Bound Library Objects wsRules = (data.WhitespaceRules != null && data.WhitespaceRules.Count != 0) ? new WhitespaceRuleLookup(data.WhitespaceRules) : null; _ctxt = new XmlQueryContext(this, defaultDataSource, dataSources, argList, wsRules); _xsltLib = null; _earlyInfo = data.EarlyBound; _earlyObjects = (_earlyInfo != null) ? new object[_earlyInfo.Length] : null; // Global variables and parameters _globalNames = data.GlobalNames; _globalValues = (_globalNames != null) ? new object[_globalNames.Length] : null; // Names _nameTableQuery = _ctxt.QueryNameTable; _atomizedNames = null; if (names != null) { // Atomize all names in "nameTableQuery". Use names from the default data source's // name table when possible. XmlNameTable nameTableDefault = _ctxt.DefaultNameTable; _atomizedNames = new string[names.Length]; if (nameTableDefault != _nameTableQuery && nameTableDefault != null) { // Ensure that atomized names from the default data source are added to the // name table used in this query for (i = 0; i < names.Length; i++) { string name = nameTableDefault.Get(names[i]); _atomizedNames[i] = _nameTableQuery.Add(name ?? names[i]); } } else { // Enter names into nametable used in this query for (i = 0; i < names.Length; i++) _atomizedNames[i] = _nameTableQuery.Add(names[i]); } } // Name filters _filters = null; if (filters != null) { // Construct name filters. Each pair of integers in the filters[] array specifies the // (localName, namespaceUri) of the NameFilter to be created. _filters = new XmlNavigatorFilter[filters.Length]; for (i = 0; i < filters.Length; i++) _filters[i] = XmlNavNameFilter.Create(_atomizedNames[filters[i].Left], _atomizedNames[filters[i].Right]); } // Prefix maping lists _prefixMappingsList = data.PrefixMappingsList; // Xml types _types = data.Types; // Xml collations _collations = data.Collations; // Document ordering _docOrderCmp = new DocumentOrderComparer(); // Indexes _indexes = null; // Output construction _stkOutput = new Stack<XmlQueryOutput>(16); _output = new XmlQueryOutput(this, seqWrt); }
//----------------------------------------------- // Constructors //----------------------------------------------- /// <summary> /// This constructor is internal so that external users cannot construct it (and therefore we do not have to test it separately). /// </summary> internal XmlQueryRuntime(XmlILCommand cmd, object defaultDataSource, XmlResolver dataSources, XsltArgumentList argList, XmlSequenceWriter seqWrt) { Debug.Assert(cmd != null, "Command object must be non-null"); string[] names = cmd.Names; Int32Pair[] filters = cmd.Filters; WhitespaceRuleLookup wsRules; int i; this.cmd = cmd; // Early-Bound Library Objects wsRules = (cmd.WhitespaceRules.Count != 0) ? new WhitespaceRuleLookup(cmd.WhitespaceRules) : null; this.ctxt = new XmlQueryContext(this, defaultDataSource, dataSources, argList, wsRules); this.xsltLib = null; this.earlyInfo = cmd.EarlyBound; this.earlyObjects = (this.earlyInfo != null) ? new object[earlyInfo.Length] : null; // Global variables and parameters this.globalNames = cmd.GlobalNames; this.globalValues = (this.globalNames != null) ? new object[this.globalNames.Length] : null; // Names this.nameTableQuery = this.ctxt.QueryNameTable; this.atomizedNames = null; if (names != null) { // Atomize all names in "nameTableQuery". Use names from the default data source's // name table when possible. XmlNameTable nameTableDefault = ctxt.DefaultNameTable; this.atomizedNames = new string[names.Length]; if (nameTableDefault != this.nameTableQuery && nameTableDefault != null) { // Ensure that atomized names from the default data source are added to the // name table used in this query for (i = 0; i < names.Length; i++) { string name = nameTableDefault.Get(names[i]); this.atomizedNames[i] = this.nameTableQuery.Add(name ?? names[i]); } } else { // Enter names into nametable used in this query for (i = 0; i < names.Length; i++) { this.atomizedNames[i] = this.nameTableQuery.Add(names[i]); } } } // Name filters this.filters = null; if (filters != null) { // Construct name filters. Each pair of integers in the filters[] array specifies the // (localName, namespaceUri) of the NameFilter to be created. this.filters = new XmlNavigatorFilter[filters.Length]; for (i = 0; i < filters.Length; i++) { this.filters[i] = XmlNavNameFilter.Create(this.atomizedNames[filters[i].Left], this.atomizedNames[filters[i].Right]); } } // Prefix maping lists this.prefixMappingsList = cmd.PrefixMappingsList; // Xml types this.types = cmd.Types; // Xml collations this.collations = cmd.Collations; // Document ordering this.docOrderCmp = new DocumentOrderComparer(); // Indexes this.indexes = null; // Output construction this.stkOutput = new Stack <XmlQueryOutput>(16); this.output = new XmlQueryOutput(this, seqWrt); }