public static Resultset Dyncast([Implict] Executive engine, object arg) { QueryNode.LispProcessingContext owner = (QueryNode.LispProcessingContext)engine.Owner; XmlDataAccessor accessor = new XmlDataAccessor(); if (arg == null) return null; else if (arg is Resultset) return (Resultset)arg; else if (arg is XmlNode) { XmlNode node = ((XmlNode)arg); if (node is XmlDocument) node = ((XmlDocument)node).DocumentElement; XmlDataAccessor.NodeList nodeList = new XmlDataAccessor.NodeList(); foreach (XmlAttribute attr in node.Attributes) if (!XmlDataAccessor.IsSpecialAttribute(attr)) nodeList.Add(attr); foreach (XmlNode child in node.ChildNodes) nodeList.Add(child); return accessor.ParseNodes(nodeList, null, owner.Node); } else if (arg is XmlNodeList) return accessor.ParseNodes(((XmlNodeList)arg), null, owner.Node); else if (arg is Array) { Array array = (Array)arg; DataTable dt = RowType.CreateSchemaTable(); if (array.Rank > 1) throw new ESQLException("Can't cast multi-diminsion array to resultset"); else { Type elemType = array.GetType().GetElementType(); DataRow r = dt.NewRow(); r["ColumnName"] = "node"; r["ColumnOrdinal"] = 0; if (Type.GetTypeCode(elemType) == TypeCode.Object) r["DataType"] = typeof(System.Object); else r["DataType"] = elemType; dt.Rows.Add(r); } Resultset rs = new Resultset(new RowType(dt), null); foreach (object item in array) { Row row = rs.NewRow(); row.SetValue(0, item); rs.Enqueue(row); } return rs; } else if (arg == DBNull.Value) return null; else { DataTable dt = RowType.CreateSchemaTable(); DataRow r = dt.NewRow(); r["ColumnName"] = "node"; r["ColumnOrdinal"] = 0; r["DataType"] = TypeConverter.GetTypeByTypeCode(Type.GetTypeCode(arg.GetType())); dt.Rows.Add(r); Resultset rs = new Resultset(new RowType(dt), null); Row row = rs.NewRow(); row.SetValue(0, arg); rs.Enqueue(row); return rs; } }
public static object Extract([Implict] Executive engine, object arg, string xpath) { QueryNode.LispProcessingContext owner = (QueryNode.LispProcessingContext)engine.Owner; XmlDataAccessor xmlacc = new XmlDataAccessor(); if (arg == null) return null; else if (arg is Resultset) { Resultset rs = (Resultset)arg; Resultset dest = new Resultset(RowType.CreateContainerType(typeof(System.Object)), null); if (!dest.RowType.RowTypeEquals(rs.RowType)) throw new InvalidOperationException(); while (rs.Begin != null) { Row row = rs.Dequeue(); XmlNode node = (XmlNode)row.GetObject(0); object name = row.GetValue(1); row = dest.NewRow(); if (node != null) { XmlNodeList nodes = node.SelectNodes(xpath, owner.QueryContext.GetNsManager(node.OwnerDocument)); if (nodes.Count > 0) row.SetObject(0, nodes); } row.SetValue(1, name); dest.Enqueue(row); } return dest; } else if (arg is XmlNode) { XmlNode node = (XmlNode)arg; XmlNodeList nodes = node.SelectNodes(xpath, owner.QueryContext.GetNsManager(node.OwnerDocument)); if (nodes.Count == 0) return null; else return nodes; } else if (arg is XmlNodeList) { XmlDataAccessor.NodeList nodeList = new XmlDataAccessor.NodeList(); foreach (XmlNode node in (XmlNodeList)arg) { XmlNodeList nodes = node.SelectNodes(xpath, owner.QueryContext.GetNsManager(node.OwnerDocument)); nodeList.AddRange(nodes); } if (nodeList.Count > 0) return nodeList; else return null; } else throw new InvalidOperationException(); }
private static void ProcessNode(XmlDataAccessor.NodeList nodeList, XmlNode node, string name) { if (name.StartsWith("@")) { if (node.NodeType == XmlNodeType.Element) { XmlElement elem = (XmlElement)node; if (elem.HasAttributes) { name = name.Substring(1); // Omit '@' prefix foreach (XmlAttribute attr in elem.Attributes) if (attr.Name.Equals(name)) { nodeList.Add(attr); break; } } } } else foreach (XmlNode child in node.ChildNodes) if (child.Name.Equals(name)) nodeList.Add(child); }